primer_view_components 0.0.51 → 0.0.55

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +159 -0
  3. data/app/components/primer/alpha/tab_nav.html.erb +11 -0
  4. data/app/components/primer/alpha/tab_nav.rb +130 -0
  5. data/app/components/primer/{tab_nav_component.html.erb → alpha/tab_panels.html.erb} +3 -8
  6. data/app/components/primer/alpha/tab_panels.rb +82 -0
  7. data/app/components/primer/alpha/underline_nav.html.erb +15 -0
  8. data/app/components/primer/alpha/underline_nav.rb +137 -0
  9. data/app/components/primer/{underline_nav_component.html.erb → alpha/underline_panels.html.erb} +3 -8
  10. data/app/components/primer/alpha/underline_panels.rb +86 -0
  11. data/app/components/primer/base_component.rb +1 -1
  12. data/app/components/primer/beta/avatar_stack.rb +9 -9
  13. data/app/components/primer/{breadcrumb_component.html.erb → beta/breadcrumbs.html.erb} +2 -1
  14. data/app/components/primer/beta/breadcrumbs.rb +61 -0
  15. data/app/components/primer/beta/truncate.html.erb +5 -0
  16. data/app/components/primer/beta/truncate.rb +110 -0
  17. data/app/components/primer/border_box_component.rb +27 -1
  18. data/app/components/primer/clipboard_copy.rb +1 -1
  19. data/app/components/primer/dropdown.rb +7 -7
  20. data/app/components/primer/icon_button.rb +1 -1
  21. data/app/components/primer/navigation/tab_component.rb +8 -6
  22. data/app/components/primer/octicon_component.rb +6 -1
  23. data/app/components/primer/progress_bar_component.rb +0 -3
  24. data/app/components/primer/tab_container_component.rb +1 -1
  25. data/app/lib/primer/class_name_helper.rb +14 -13
  26. data/app/lib/primer/fetch_or_fallback_helper.rb +2 -0
  27. data/app/lib/primer/octicon/cache.rb +10 -2
  28. data/app/lib/primer/tab_nav_helper.rb +35 -0
  29. data/app/lib/primer/tabbed_component_helper.rb +5 -5
  30. data/app/lib/primer/underline_nav_helper.rb +44 -0
  31. data/app/lib/primer/view_helper.rb +1 -0
  32. data/lib/primer/classify/cache.rb +0 -6
  33. data/lib/primer/classify/flex.rb +1 -1
  34. data/lib/primer/classify/functional_colors.rb +1 -1
  35. data/lib/primer/classify/utilities.rb +17 -2
  36. data/lib/primer/classify/utilities.yml +35 -0
  37. data/lib/primer/classify/validation.rb +18 -0
  38. data/lib/primer/classify.rb +4 -13
  39. data/lib/primer/view_components/constants.rb +1 -1
  40. data/lib/primer/view_components/linters/argument_mappers/base.rb +34 -8
  41. data/lib/primer/view_components/linters/argument_mappers/button.rb +5 -6
  42. data/lib/primer/view_components/linters/argument_mappers/clipboard_copy.rb +4 -3
  43. data/lib/primer/view_components/linters/argument_mappers/close_button.rb +43 -0
  44. data/lib/primer/view_components/linters/argument_mappers/flash.rb +32 -0
  45. data/lib/primer/view_components/linters/argument_mappers/helpers/erb_block.rb +48 -5
  46. data/lib/primer/view_components/linters/argument_mappers/label.rb +3 -4
  47. data/lib/primer/view_components/linters/argument_mappers/system_arguments.rb +5 -7
  48. data/lib/primer/view_components/linters/autocorrectable.rb +6 -4
  49. data/lib/primer/view_components/linters/{helpers.rb → base_linter.rb} +69 -29
  50. data/lib/primer/view_components/linters/button_component_migration_counter.rb +4 -3
  51. data/lib/primer/view_components/linters/clipboard_copy_component_migration_counter.rb +3 -4
  52. data/lib/primer/view_components/linters/close_button_component_migration_counter.rb +110 -3
  53. data/lib/primer/view_components/linters/flash_component_migration_counter.rb +18 -3
  54. data/lib/primer/view_components/linters/label_component_migration_counter.rb +2 -3
  55. data/lib/primer/view_components/version.rb +1 -1
  56. data/lib/rubocop/config/default.yml +5 -0
  57. data/lib/rubocop/cop/primer/base_cop.rb +28 -0
  58. data/lib/rubocop/cop/primer/deprecated_arguments.rb +263 -0
  59. data/lib/rubocop/cop/primer/no_tag_memoize.rb +1 -0
  60. data/lib/rubocop/cop/primer/primer_octicon.rb +178 -0
  61. data/lib/rubocop/cop/primer/system_argument_instead_of_class.rb +4 -32
  62. data/lib/rubocop/cop/primer.rb +1 -2
  63. data/lib/tasks/coverage.rake +4 -0
  64. data/lib/tasks/docs.rake +10 -8
  65. data/lib/tasks/utilities.rake +7 -3
  66. data/lib/yard/docs_helper.rb +6 -3
  67. data/static/arguments.yml +82 -64
  68. data/static/classes.yml +10 -0
  69. data/static/constants.json +44 -30
  70. data/static/statuses.json +10 -6
  71. metadata +57 -18
  72. data/app/components/primer/auto_complete/auto_component.d.ts +0 -1
  73. data/app/components/primer/auto_complete/auto_component.js +0 -1
  74. data/app/components/primer/breadcrumb_component.rb +0 -57
  75. data/app/components/primer/tab_nav_component.rb +0 -151
  76. data/app/components/primer/underline_nav_component.rb +0 -187
  77. data/lib/primer/classify/functional_text_colors.rb +0 -64
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ module Alpha
5
+ # Use `UnderlineNav` to style navigation links with a minimal
6
+ # underlined selected state, typically placed at the top
7
+ # of the page.
8
+ # For panel navigation, use <%= link_to_component(Primer::Alpha::UnderlinePanels) %> instead.
9
+ #
10
+ # @accessibility
11
+ # - By default, `UnderlineNav` renders links within a `<nav>` element. `<nav>` has an
12
+ # implicit landmark role of `navigation` which should be reserved for main links.
13
+ # For all other set of links, set tag to `:div`.
14
+ # - See <%= link_to_component(Primer::Navigation::TabComponent) %> for additional
15
+ # accessibility considerations.
16
+ class UnderlineNav < Primer::Component
17
+ include Primer::TabbedComponentHelper
18
+ include Primer::UnderlineNavHelper
19
+
20
+ BODY_TAG_DEFAULT = :ul
21
+
22
+ TAG_DEFAULT = :nav
23
+ TAG_OPTIONS = [TAG_DEFAULT, :div].freeze
24
+
25
+ # Use the tabs to list page links.
26
+ #
27
+ # @param selected [Boolean] Whether the tab is selected.
28
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
29
+ renders_many :tabs, lambda { |selected: false, **system_arguments|
30
+ system_arguments[:classes] = underline_nav_tab_classes(system_arguments[:classes])
31
+ Primer::Navigation::TabComponent.new(
32
+ list: true,
33
+ selected: selected,
34
+ icon_classes: "UnderlineNav-octicon",
35
+ **system_arguments
36
+ )
37
+ }
38
+
39
+ # Use actions for a call to action.
40
+ #
41
+ # @param tag [Symbol] (Primer::UnderlineNavHelper::ACTIONS_TAG_DEFAULT) <%= one_of(Primer::UnderlineNavHelper::ACTIONS_TAG_OPTIONS) %>
42
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
43
+ renders_one :actions, lambda { |tag: ACTIONS_TAG_DEFAULT, **system_arguments|
44
+ system_arguments[:tag] = fetch_or_fallback(ACTIONS_TAG_OPTIONS, tag, ACTIONS_TAG_DEFAULT)
45
+ system_arguments[:classes] = underline_nav_action_classes(system_arguments[:classes])
46
+
47
+ Primer::BaseComponent.new(**system_arguments)
48
+ }
49
+
50
+ # @example Default with `<nav>`
51
+ # @description
52
+ # `<nav>` is a landmark and should be reserved for main navigation links. See <%= link_to_accessibility %>.
53
+ # @code
54
+ # <%= render(Primer::Alpha::UnderlineNav.new(label: "Default with nav element")) do |component| %>
55
+ # <% component.tab(href: "#", selected: true) { "Item 1" } %>
56
+ # <% component.tab(href: "#") { "Item 2" } %>
57
+ # <% component.actions do %>
58
+ # <%= render(Primer::ButtonComponent.new) { "Button!" } %>
59
+ # <% end %>
60
+ # <% end %>
61
+ #
62
+ # @example With `<div>`
63
+ # <%= render(Primer::Alpha::UnderlineNav.new(tag: :div, label: "With div element")) do |component| %>
64
+ # <% component.tab(href: "#", selected: true) { "Item 1" } %>
65
+ # <% component.tab(href: "#") { "Item 2" } %>
66
+ # <% component.actions do %>
67
+ # <%= render(Primer::ButtonComponent.new) { "Button!" } %>
68
+ # <% end %>
69
+ # <% end %>
70
+ #
71
+ # @example With icons and counters
72
+ # <%= render(Primer::Alpha::UnderlineNav.new(label: "With icons and counters")) do |component| %>
73
+ # <% component.tab(href: "#", selected: true) do |t| %>
74
+ # <% t.icon(icon: :star) %>
75
+ # <% t.text { "Item 1" } %>
76
+ # <% end %>
77
+ # <% component.tab(href: "#") do |t| %>
78
+ # <% t.icon(icon: :star) %>
79
+ # <% t.text { "Item 2" } %>
80
+ # <% t.counter(count: 10) %>
81
+ # <% end %>
82
+ # <% component.tab(href: "#") do |t| %>
83
+ # <% t.text { "Item 3" } %>
84
+ # <% t.counter(count: 10) %>
85
+ # <% end %>
86
+ # <% component.actions do %>
87
+ # <%= render(Primer::ButtonComponent.new) { "Button!" } %>
88
+ # <% end %>
89
+ # <% end %>
90
+ #
91
+ # @example Align right
92
+ # <%= render(Primer::Alpha::UnderlineNav.new(label: "Align right", align: :right)) do |component| %>
93
+ # <% component.tab(href: "#", selected: true) do |t| %>
94
+ # <% t.text { "Item 1" } %>
95
+ # <% end %>
96
+ # <% component.tab(href: "#") do |t| %>
97
+ # <% t.text { "Item 2" } %>
98
+ # <% end %>
99
+ # <% component.actions do %>
100
+ # <%= render(Primer::ButtonComponent.new) { "Button!" } %>
101
+ # <% end %>
102
+ # <% end %>
103
+ #
104
+ # @example Customizing the body
105
+ # <%= render(Primer::Alpha::UnderlineNav.new(label: "Default", body_arguments: { classes: "custom-class", border: true, border_color: :info })) do |c| %>
106
+ # <% c.tab(selected: true, href: "#") { "Tab 1" }%>
107
+ # <% c.tab(href: "#") { "Tab 2" } %>
108
+ # <% c.tab(href: "#") { "Tab 3" } %>
109
+ # <% end %>
110
+ #
111
+ # @param tag [Symbol] <%= one_of(Primer::Alpha::UnderlineNav::TAG_OPTIONS) %>
112
+ # @param label [String] Sets an `aria-label` that helps assistive technology users understand the purpose of the links, and distinguish it from similar elements.
113
+ # @param align [Symbol] <%= one_of(Primer::UnderlineNavHelper::ALIGN_OPTIONS) %> - Defaults to <%= Primer::UnderlineNavHelper::ALIGN_DEFAULT %>
114
+ # @param body_arguments [Hash] <%= link_to_system_arguments_docs %> for the body wrapper.
115
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
116
+ def initialize(label:, tag: TAG_DEFAULT, align: ALIGN_DEFAULT, body_arguments: {}, **system_arguments)
117
+ @align = fetch_or_fallback(ALIGN_OPTIONS, align, ALIGN_DEFAULT)
118
+
119
+ @system_arguments = system_arguments
120
+ @system_arguments[:tag] = fetch_or_fallback(TAG_OPTIONS, tag, TAG_DEFAULT)
121
+ @system_arguments[:classes] = underline_nav_classes(@system_arguments[:classes], @align)
122
+
123
+ @body_arguments = body_arguments
124
+ @body_arguments[:tag] = :ul
125
+ @body_arguments[:classes] = underline_nav_body_classes(@body_arguments[:classes])
126
+
127
+ aria_label_for_page_nav(label)
128
+ end
129
+
130
+ private
131
+
132
+ def body
133
+ Primer::BaseComponent.new(**@body_arguments)
134
+ end
135
+ end
136
+ end
137
+ end
@@ -1,23 +1,18 @@
1
- <%= wrapper(**@wrapper_arguments) do %>
1
+ <%= tab_container_wrapper(with_panel: true, **@wrapper_arguments) do %>
2
2
  <%= render Primer::BaseComponent.new(**@system_arguments) do %>
3
3
  <% if @align == :right %>
4
4
  <%= actions %>
5
5
  <% end %>
6
-
7
6
  <%= render body do %>
8
7
  <% tabs.each do |tab| %>
9
8
  <%= tab %>
10
9
  <% end %>
11
10
  <% end %>
12
-
13
11
  <% if @align == :left %>
14
12
  <%= actions %>
15
13
  <% end %>
16
14
  <% end %>
17
-
18
- <% if @with_panel %>
19
- <% tabs.each do |tab| %>
20
- <%= tab.panel %>
21
- <% end %>
15
+ <% tabs.each do |tab| %>
16
+ <%= tab.panel %>
22
17
  <% end %>
23
18
  <% end %>
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ module Alpha
5
+ # Use `UnderlinePanels` to style tabs with an associated panel and an underlined selected state.
6
+ class UnderlinePanels < Primer::Component
7
+ include Primer::TabbedComponentHelper
8
+ include Primer::UnderlineNavHelper
9
+ # Use to render a button and an associated panel slot. See the example below or refer to <%= link_to_component(Primer::Navigation::TabComponent) %>.
10
+ #
11
+ # @param id [String] Unique ID of tab.
12
+ # @param selected [Boolean] Whether the tab is selected.
13
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
14
+ renders_many :tabs, lambda { |id:, selected: false, **system_arguments|
15
+ system_arguments[:id] = id
16
+ system_arguments[:classes] = underline_nav_tab_classes(system_arguments[:classes])
17
+
18
+ Primer::Navigation::TabComponent.new(
19
+ selected: selected,
20
+ with_panel: true,
21
+ list: true,
22
+ icon_classes: "UnderlineNav-octicon",
23
+ panel_id: "panel-#{id}",
24
+ **system_arguments
25
+ )
26
+ }
27
+
28
+ # Use actions for a call to action.
29
+ #
30
+ # @param tag [Symbol] (Primer::UnderlineNavHelper::ACTIONS_TAG_DEFAULT) <%= one_of(Primer::UnderlineNavHelper::ACTIONS_TAG_OPTIONS) %>
31
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
32
+ renders_one :actions, lambda { |tag: ACTIONS_TAG_DEFAULT, **system_arguments|
33
+ system_arguments[:tag] = fetch_or_fallback(ACTIONS_TAG_OPTIONS, tag, ACTIONS_TAG_DEFAULT)
34
+ system_arguments[:classes] = underline_nav_action_classes(system_arguments[:classes])
35
+
36
+ Primer::BaseComponent.new(**system_arguments)
37
+ }
38
+
39
+ # @example Default
40
+ # <%= render(Primer::Alpha::UnderlinePanels.new(label: "With panels")) do |component| %>
41
+ # <% component.tab(id: "tab-1", selected: true) do |t| %>
42
+ # <% t.text { "Tab 1" } %>
43
+ # <% t.panel do %>
44
+ # Panel 1
45
+ # <% end %>
46
+ # <% end %>
47
+ # <% component.tab(id: "tab-2") do |t| %>
48
+ # <% t.text { "Tab 2" } %>
49
+ # <% t.panel do %>
50
+ # Panel 2
51
+ # <% end %>
52
+ # <% end %>
53
+ # <% component.actions do %>
54
+ # <%= render(Primer::ButtonComponent.new) { "Button!" } %>
55
+ # <% end %>
56
+ # <% end %>
57
+ #
58
+ # @param label [String] Sets an `aria-label` that helps assistive technology users understand the purpose of the tabs.
59
+ # @param align [Symbol] <%= one_of(Primer::UnderlineNavHelper::ALIGN_OPTIONS) %> - Defaults to <%= Primer::UnderlineNavHelper::ALIGN_DEFAULT %>
60
+ # @param body_arguments [Hash] <%= link_to_system_arguments_docs %> for the body wrapper.
61
+ # @param wrapper_arguments [Hash] <%= link_to_system_arguments_docs %> for the `TabContainer` wrapper.
62
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
63
+ def initialize(label:, align: ALIGN_DEFAULT, body_arguments: {}, wrapper_arguments: {}, **system_arguments)
64
+ @align = fetch_or_fallback(ALIGN_OPTIONS, align, ALIGN_DEFAULT)
65
+ @wrapper_arguments = wrapper_arguments
66
+
67
+ @system_arguments = system_arguments
68
+ @system_arguments[:tag] = :div
69
+ @system_arguments[:classes] = underline_nav_classes(@system_arguments[:classes], @align)
70
+
71
+ @body_arguments = body_arguments
72
+ @body_arguments[:tag] = :ul
73
+ @body_arguments[:classes] = underline_nav_body_classes(@body_arguments[:classes])
74
+
75
+ @body_arguments[:role] = :tablist
76
+ @body_arguments[:"aria-label"] = label
77
+ end
78
+
79
+ private
80
+
81
+ def body
82
+ Primer::BaseComponent.new(**@body_arguments)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -65,7 +65,7 @@ module Primer
65
65
  # | :- | :- | :- |
66
66
  # | `bg` | String, Symbol | Background color. Accepts either a hex value as a String or <%= one_of(Primer::Classify::FunctionalBackgroundColors::OPTIONS, lower: true) %> |
67
67
  # | `border_color` | Symbol | Border color. <%= one_of(Primer::Classify::FunctionalBorderColors::OPTIONS) %> |
68
- # | `color` | Symbol | Text color. <%= one_of(Primer::Classify::FunctionalTextColors::OPTIONS) %> |
68
+ # | `color` | Symbol | Text color. <%= one_of(Primer::Classify::Utilities.mappings(:color)) %> |
69
69
  #
70
70
  # ## Flex
71
71
  #
@@ -21,23 +21,23 @@ module Primer
21
21
 
22
22
  # @example Default
23
23
  # <%= render(Primer::Beta::AvatarStack.new) do |c| %>
24
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
25
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
26
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
24
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
25
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
26
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
27
27
  # <% end %>
28
28
  #
29
29
  # @example Align right
30
30
  # <%= render(Primer::Beta::AvatarStack.new(align: :right)) do |c| %>
31
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
32
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
33
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
31
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
32
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
33
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
34
34
  # <% end %>
35
35
  #
36
36
  # @example With tooltip
37
37
  # <%= render(Primer::Beta::AvatarStack.new(tooltipped: true, body_arguments: { label: 'This is a tooltip!' })) do |c| %>
38
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
39
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
40
- # <%= c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
38
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
39
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
40
+ # <% c.avatar(src: "http://placekitten.com/200/200", alt: "@kittenuser") %>
41
41
  # <% end %>
42
42
  #
43
43
  # @param tag [Symbol] <%= one_of(Primer::Beta::AvatarStack::TAG_OPTIONS) %>
@@ -1,6 +1,7 @@
1
1
  <%= render Primer::BaseComponent.new(**@system_arguments) do %>
2
2
  <ol>
3
- <% items.each do |item| %>
3
+ <% items.each_with_index do |item, index| %>
4
+ <% item.selected = index == items.length - 1 %>
4
5
  <%= item %>
5
6
  <% end %>
6
7
  </ol>
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ module Beta
5
+ # Use `Breadcrumbs` to display page hierarchy.
6
+ class Breadcrumbs < Primer::Component
7
+ status :beta
8
+
9
+ # @param href [String] The URL to link to.
10
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
11
+ renders_many :items, "Item"
12
+
13
+ # @example Basic
14
+ # <%= render(Primer::Beta::Breadcrumbs.new) do |component| %>
15
+ # <% component.item(href: "/") do %>Home<% end %>
16
+ # <% component.item(href: "/about") do %>About<% end %>
17
+ # <% component.item(href: "/about/team") do %>Team<% end %>
18
+ # <% end %>
19
+ #
20
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
21
+ def initialize(**system_arguments)
22
+ @system_arguments = system_arguments
23
+ @system_arguments[:tag] = :nav
24
+ @system_arguments[:aria] = { label: "Breadcrumb" }
25
+ end
26
+
27
+ def render?
28
+ items.any?
29
+ end
30
+
31
+ # This component is part of `Primer::Beta::Breadcrumbs` and should not be
32
+ # used as a standalone component.
33
+ class Item < Primer::Component
34
+ attr_accessor :selected, :href
35
+
36
+ def initialize(href:, **system_arguments)
37
+ @href = href
38
+ @system_arguments = system_arguments
39
+ @selected = false
40
+
41
+ @system_arguments[:tag] = :li
42
+ @system_arguments[:classes] = "breadcrumb-item #{@system_arguments[:classes]}"
43
+ end
44
+
45
+ def call
46
+ link_arguments = { href: @href }
47
+
48
+ if selected
49
+ link_arguments[:"aria-current"] = "page"
50
+ link_arguments[:classes] = "breadcrumb-item-selected"
51
+ @system_arguments[:classes] = "#{@system_arguments[:classes]} breadcrumb-item-selected"
52
+ end
53
+
54
+ render(Primer::BaseComponent.new(**@system_arguments)) do
55
+ render(Primer::LinkComponent.new(**link_arguments)) { content }
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ <%= render Primer::BaseComponent.new(**@system_arguments) do %>
2
+ <% items.each do |item| %>
3
+ <%= item %>
4
+ <% end %>
5
+ <% end %>
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ module Beta
5
+ # Use `Truncate` to shorten overflowing text with an ellipsis.
6
+ class Truncate < Primer::Component
7
+ status :beta
8
+
9
+ # Text slot used for the truncated text.
10
+ #
11
+ # @param priority [Boolean] if true, the text will be given priority
12
+ # @param expandable [Boolean] if true, the text will expand on hover or focus
13
+ # @param max_width [Integer] if provided, the text will be truncated at a maximum width
14
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
15
+ renders_many :items, "TruncateText"
16
+
17
+ # @example Default
18
+ # <%= render(Primer::Beta::Truncate.new) { "branch-name-that-is-really-long" } %>
19
+ #
20
+ # @example Multiple items
21
+ # <%= render(Primer::Beta::Truncate.new) do |component| %>
22
+ # <% component.item do %>really-long-repository-owner-name<% end %>
23
+ # <% component.item(font_weight: :bold) do %>
24
+ # <%= render(Primer::BaseComponent.new(tag: :span, font_weight: :normal)) { "/" } %> really-long-repository-name
25
+ # <% end %>
26
+ # <% end %>
27
+ #
28
+ # @example Advanced multiple items
29
+ # <%= render(Primer::Beta::Truncate.new(tag: :ol)) do |component| %>
30
+ # <% component.item(tag: :li) do %>primer<% end %>
31
+ # <% component.item(tag: :li, priority: true) do %>/ css<% end %>
32
+ # <% component.item(tag: :li) do %>/ Issues<% end %>
33
+ # <% component.item(tag: :li) do %>/ #123 —<% end %>
34
+ # <% component.item(tag: :li, priority: true) do %>
35
+ # Visual bug on primer.style found in lists
36
+ # <% end %>
37
+ # <% end %>
38
+ #
39
+ # @example Expand on hover or focus
40
+ # <%= render(Primer::Beta::Truncate.new) do |component| %>
41
+ # <% component.item(tag: :a, href: "#", expandable: true) do %>really-long-repository-owner-name<% end %>
42
+ # <% component.item(tag: :a, href: "#", expandable: true) do %>really-long-repository-owner-name<% end %>
43
+ # <% component.item(tag: :a, href: "#", expandable: true) do %>really-long-repository-owner-name<% end %>
44
+ # <% component.item(tag: :a, href: "#", expandable: true) do %>really-long-repository-owner-name<% end %>
45
+ # <% end %>
46
+ #
47
+ # @example Max widths
48
+ # <%= render(Primer::Beta::Truncate.new) do |component| %>
49
+ # <% component.item(max_width: 300, expandable: true) do %>branch-name-that-is-really-long-branch-name-that-is-really-long-branch-name-that-is-really-long<% end %>
50
+ # <% component.item(max_width: 200, expandable: true) do %>branch-name-that-is-really-long-branch-name-that-is-really-long-branch-name-that-is-really-long<% end %>
51
+ # <% component.item(max_width: 100, expandable: true) do %>branch-name-that-is-really-long-branch-name-that-is-really-long-branch-name-that-is-really-long<% end %>
52
+ # <% end %>
53
+ #
54
+ # @example Max widths on new lines
55
+ # <%= render(Primer::Beta::Truncate.new) do |component| %>
56
+ # <% component.item(max_width: 300, expandable: true) do %>branch-name-that-is-really-long-branch-name-that-is-really-long-branch-name-that-is-really-long<% end %>
57
+ # <% end %>
58
+ # <br/>
59
+ # <%= render(Primer::Beta::Truncate.new) do |component| %>
60
+ # <% component.item(max_width: 200, expandable: true) do %>branch-name-that-is-really-long-branch-name-that-is-really-long-branch-name-that-is-really-long<% end %>
61
+ # <% end %>
62
+ # <br/>
63
+ # <%= render(Primer::Beta::Truncate.new) do |component| %>
64
+ # <% component.item(max_width: 100, expandable: true) do %>branch-name-that-is-really-long-branch-name-that-is-really-long-branch-name-that-is-really-long<% end %>
65
+ # <% end %>
66
+ #
67
+ # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
68
+ def initialize(**system_arguments)
69
+ @system_arguments = system_arguments
70
+ @system_arguments[:tag] = system_arguments[:tag] || :span
71
+ @system_arguments[:classes] = class_names(
72
+ "Truncate",
73
+ system_arguments[:classes]
74
+ )
75
+ end
76
+
77
+ def before_render
78
+ return unless content.present? && items.empty?
79
+
80
+ item { content }
81
+ end
82
+
83
+ def render?
84
+ items.any?
85
+ end
86
+
87
+ # This component is part of `Primer::Beta::Truncate` and should not be
88
+ # used as a standalone component.
89
+ class TruncateText < Primer::Component
90
+ def initialize(priority: false, expandable: false, max_width: nil, **system_arguments)
91
+ @system_arguments = system_arguments
92
+ @system_arguments[:tag] = system_arguments[:tag] || :span
93
+ @system_arguments[:classes] = class_names(
94
+ "Truncate-text",
95
+ "Truncate-text--primary": priority,
96
+ "Truncate-text--expandable": expandable
97
+ )
98
+
99
+ @system_arguments[:style] = join_style_arguments(@system_arguments[:style], "max-width: #{max_width}px;") unless max_width.nil?
100
+ end
101
+
102
+ def call
103
+ render(Primer::BaseComponent.new(**@system_arguments)) do
104
+ content
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end