primer_view_components 0.0.1 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +92 -2
  3. data/README.md +149 -0
  4. data/app/components/primer/avatar_component.rb +28 -0
  5. data/app/components/primer/base_component.rb +42 -0
  6. data/app/components/primer/blankslate_component.html.erb +27 -0
  7. data/app/components/primer/blankslate_component.rb +164 -0
  8. data/app/components/primer/border_box_component.html.erb +26 -0
  9. data/app/components/primer/border_box_component.rb +77 -0
  10. data/app/components/primer/box_component.rb +14 -0
  11. data/app/components/primer/breadcrumb_component.html.erb +8 -0
  12. data/app/components/primer/breadcrumb_component.rb +52 -0
  13. data/app/components/primer/button_component.rb +58 -0
  14. data/app/components/primer/component.rb +9 -0
  15. data/app/components/primer/counter_component.rb +85 -0
  16. data/app/components/primer/details_component.html.erb +8 -0
  17. data/app/components/primer/details_component.rb +63 -0
  18. data/app/components/primer/dropdown_menu_component.html.erb +8 -0
  19. data/app/components/primer/dropdown_menu_component.rb +28 -0
  20. data/app/components/primer/flex_component.rb +81 -0
  21. data/app/components/primer/flex_item_component.rb +21 -0
  22. data/app/components/primer/heading_component.rb +14 -0
  23. data/app/components/primer/label_component.rb +48 -0
  24. data/app/components/primer/layout_component.html.erb +17 -0
  25. data/app/components/primer/layout_component.rb +28 -0
  26. data/app/components/primer/link_component.rb +19 -0
  27. data/app/components/primer/popover_component.html.erb +10 -0
  28. data/app/components/primer/popover_component.rb +75 -0
  29. data/app/components/primer/progress_bar_component.html.erb +5 -0
  30. data/app/components/primer/progress_bar_component.rb +66 -0
  31. data/app/components/primer/slot.rb +8 -0
  32. data/app/components/primer/state_component.rb +53 -0
  33. data/app/components/primer/subhead_component.html.erb +17 -0
  34. data/app/components/primer/subhead_component.rb +89 -0
  35. data/app/components/primer/text_component.rb +14 -0
  36. data/app/components/primer/timeline_item_component.html.erb +17 -0
  37. data/app/components/primer/timeline_item_component.rb +69 -0
  38. data/app/components/primer/underline_nav_component.html.erb +11 -0
  39. data/app/components/primer/underline_nav_component.rb +22 -0
  40. data/app/components/primer/view_components.rb +48 -0
  41. data/lib/primer/class_name_helper.rb +27 -0
  42. data/lib/primer/classify.rb +245 -0
  43. data/lib/primer/fetch_or_fallback_helper.rb +41 -0
  44. data/lib/primer/view_components.rb +3 -0
  45. data/lib/primer/view_components/engine.rb +11 -0
  46. data/lib/primer/view_components/version.rb +1 -1
  47. metadata +203 -4
@@ -0,0 +1,26 @@
1
+ <%= render Primer::BaseComponent.new(**@kwargs) do %>
2
+ <% if header %>
3
+ <%= render Primer::BaseComponent.new(**header.kwargs) do %>
4
+ <%= header.content %>
5
+ <% end %>
6
+ <% end %>
7
+ <% if body %>
8
+ <%= render Primer::BaseComponent.new(**body.kwargs) do %>
9
+ <%= body.content %>
10
+ <% end %>
11
+ <% end %>
12
+ <% if rows.any? %>
13
+ <ul>
14
+ <% rows.each do |row| %>
15
+ <%= render Primer::BaseComponent.new(**row.kwargs) do %>
16
+ <%= row.content %>
17
+ <% end %>
18
+ <% end %>
19
+ </ul>
20
+ <% end %>
21
+ <% if footer %>
22
+ <%= render Primer::BaseComponent.new(**footer.kwargs) do %>
23
+ <%= footer.content %>
24
+ <% end %>
25
+ <% end %>
26
+ <% end %>
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ class BorderBoxComponent < Primer::Component
5
+ include ViewComponent::Slotable
6
+
7
+ with_slot :header, class_name: "Header"
8
+ with_slot :body, class_name: "Body"
9
+ with_slot :footer, class_name: "Footer"
10
+ with_slot :row, collection: true, class_name: "Row"
11
+
12
+ def initialize(**kwargs)
13
+ @kwargs = kwargs
14
+ @kwargs[:tag] = :div
15
+ @kwargs[:classes] = class_names(
16
+ "Box",
17
+ kwargs[:classes]
18
+ )
19
+ end
20
+
21
+ def render?
22
+ rows.any? || header.present? || body.present? || footer.present?
23
+ end
24
+
25
+ class Header < Primer::Slot
26
+
27
+ attr_reader :kwargs
28
+ def initialize(**kwargs)
29
+ @kwargs = kwargs
30
+ @kwargs[:tag] = :div
31
+ @kwargs[:classes] = class_names(
32
+ "Box-header",
33
+ kwargs[:classes]
34
+ )
35
+ end
36
+ end
37
+
38
+ class Body < Primer::Slot
39
+
40
+ attr_reader :kwargs
41
+ def initialize(**kwargs)
42
+ @kwargs = kwargs
43
+ @kwargs[:tag] = :div
44
+ @kwargs[:classes] = class_names(
45
+ "Box-body",
46
+ kwargs[:classes]
47
+ )
48
+ end
49
+ end
50
+
51
+ class Footer < Primer::Slot
52
+
53
+ attr_reader :kwargs
54
+ def initialize(**kwargs)
55
+ @kwargs = kwargs
56
+ @kwargs[:tag] = :div
57
+ @kwargs[:classes] = class_names(
58
+ "Box-footer",
59
+ kwargs[:classes]
60
+ )
61
+ end
62
+ end
63
+
64
+ class Row < Primer::Slot
65
+
66
+ attr_reader :kwargs
67
+ def initialize(**kwargs)
68
+ @kwargs = kwargs
69
+ @kwargs[:tag] = :li
70
+ @kwargs[:classes] = class_names(
71
+ "Box-row",
72
+ kwargs[:classes]
73
+ )
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ class BoxComponent < Primer::Component
5
+ def initialize(**kwargs)
6
+ @kwargs = kwargs
7
+ @kwargs[:tag] = :div
8
+ end
9
+
10
+ def call
11
+ render(Primer::BaseComponent.new(**@kwargs)) { content }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ <%= render Primer::BaseComponent.new(**@kwargs) do %>
2
+ <ol>
3
+ <% items.each do |item| %>
4
+ <%# The <li> and <a> need to be on the same line to prevent unwanted whitespace %>
5
+ <%= render Primer::BaseComponent.new(**item.kwargs) do %><%- if item.href.present? %><a href="<%= item.href %>"><%= item.content %></a><%- else %><%= item.content %><%- end %><%- end %>
6
+ <% end %>
7
+ </ol>
8
+ <% end %>
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # Breadcrumbs are used to display page hierarchy within a section of the site. All of the items in the breadcrumb "trail" are links except for the final item which is a plain string indicating the current page.
5
+ #
6
+ # ## Example
7
+ #
8
+ # The `Primer::BreadcrumbComponent` uses the [Slots API](https://github.com/github/view_component#slots-experimental) and at least one slot is required for the component to render. Each slot can accept the following parameters:
9
+ #
10
+ # 1. `href` (string). The URL to link to.
11
+ # 2. `selected` (boolean, default=false). Flag indicating whether or not the item is selected and not rendered as a link.
12
+ #
13
+ # Note that if if both `href` and `selected: true` are passed in, `href` will be ignored and the item will not be rendered as a link.
14
+ #
15
+ # ```ruby
16
+ # <%= render(Primer::BreadcrumbComponent.new) do |component| %>
17
+ # <% component.slot(:item, href: "/") do %>Home<% end %>
18
+ # <% component.slot(:item, href: "/about") do %>About<% end %>
19
+ # <% component.slot(:item, selected: true) do %>Team<% end %>
20
+ # <% end %>
21
+ # ```
22
+ ##
23
+ module Primer
24
+ class BreadcrumbComponent < Primer::Component
25
+ include ViewComponent::Slotable
26
+
27
+ with_slot :item, collection: true, class_name: "BreadcrumbItem"
28
+
29
+ def initialize(**kwargs)
30
+ @kwargs = kwargs
31
+ @kwargs[:tag] = :nav
32
+ @kwargs[:aria] = { label: "Breadcrumb" }
33
+ end
34
+
35
+ def render?
36
+ items.any?
37
+ end
38
+
39
+ class BreadcrumbItem < Primer::Slot
40
+ attr_reader :href, :kwargs
41
+
42
+ def initialize(href: nil, selected: false, **kwargs)
43
+ @href, @kwargs = href, kwargs
44
+
45
+ @href = nil if selected
46
+ @kwargs[:tag] = :li
47
+ @kwargs[:"aria-current"] = "page" if selected
48
+ @kwargs[:classes] = "breadcrumb-item #{@kwargs[:classes]}"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ class ButtonComponent < Primer::Component
5
+ DEFAULT_BUTTON_TYPE = :default
6
+ BUTTON_TYPE_MAPPINGS = {
7
+ DEFAULT_BUTTON_TYPE => "",
8
+ :primary => "btn-primary",
9
+ :danger => "btn-danger",
10
+ :outline => "btn-outline"
11
+ }.freeze
12
+ BUTTON_TYPE_OPTIONS = BUTTON_TYPE_MAPPINGS.keys
13
+
14
+ DEFAULT_VARIANT = :medium
15
+ VARIANT_MAPPINGS = {
16
+ :small => "btn-sm",
17
+ DEFAULT_VARIANT => "",
18
+ :large => "btn-large",
19
+ }.freeze
20
+ VARIANT_OPTIONS = VARIANT_MAPPINGS.keys
21
+
22
+ DEFAULT_TAG = :button
23
+ TAG_OPTIONS = [DEFAULT_TAG, :a, :summary].freeze
24
+
25
+ DEFAULT_TYPE = :button
26
+ TYPE_OPTIONS = [DEFAULT_TYPE, :reset, :submit].freeze
27
+
28
+ def initialize(
29
+ button_type: DEFAULT_BUTTON_TYPE,
30
+ variant: DEFAULT_VARIANT,
31
+ tag: DEFAULT_TAG,
32
+ type: DEFAULT_TYPE,
33
+ group_item: false,
34
+ **kwargs
35
+ )
36
+ @kwargs = kwargs
37
+ @kwargs[:tag] = fetch_or_fallback(TAG_OPTIONS, tag.to_sym, DEFAULT_TAG)
38
+
39
+ if @kwargs[:tag] == :a
40
+ @kwargs[:role] = :button
41
+ else
42
+ @kwargs[:type] = type.to_sym
43
+ end
44
+
45
+ @kwargs[:classes] = class_names(
46
+ "btn",
47
+ kwargs[:classes],
48
+ BUTTON_TYPE_MAPPINGS[fetch_or_fallback(BUTTON_TYPE_OPTIONS, button_type.to_sym, DEFAULT_BUTTON_TYPE)],
49
+ VARIANT_MAPPINGS[fetch_or_fallback(VARIANT_OPTIONS, variant.to_sym, DEFAULT_VARIANT)],
50
+ "BtnGroup-item" => group_item
51
+ )
52
+ end
53
+
54
+ def call
55
+ render(Primer::BaseComponent.new(**@kwargs)) { content }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ class Component < ViewComponent::Base
5
+ include ClassNameHelper
6
+ include FetchOrFallbackHelper
7
+ include OcticonsHelper
8
+ end
9
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ class CounterComponent < Primer::Component
5
+ DEFAULT_SCHEME = :default
6
+ SCHEME_MAPPINGS = {
7
+ DEFAULT_SCHEME => "Counter",
8
+ :gray => "Counter Counter--gray",
9
+ :light_gray => "Counter Counter--gray-light",
10
+ }.freeze
11
+
12
+ # @param count [Integer, Float::INFINITY, nil] The number to be displayed (e.x. # of issues, pull requests)
13
+ # @param scheme [Symbol] Color scheme. One of `SCHEME_MAPPINGS.keys`.
14
+ # @param limit [Integer] Maximum value to display. (e.x. if count == 6,000 and limit == 5000, counter will display "5,000+")
15
+ # @param hide_if_zero [Boolean] If true, a `hidden` attribute is added to the counter if `count` is zero.
16
+ # @param text [String] Text to display instead of count.
17
+ # @param round [Boolean] Whether to apply our standard rounding logic to value.
18
+ # @param kwargs [Hash] Style arguments to be passed to Primer::Classify
19
+ def initialize(
20
+ count: 0,
21
+ scheme: DEFAULT_SCHEME,
22
+ limit: 5_000,
23
+ hide_if_zero: false,
24
+ text: "",
25
+ round: false,
26
+ **kwargs
27
+ )
28
+ @count, @limit, @hide_if_zero, @text, @round, @kwargs = count, limit, hide_if_zero, text, round, kwargs
29
+
30
+ @kwargs[:title] = title
31
+ @kwargs[:tag] = :span
32
+ @kwargs[:classes] = class_names(
33
+ @kwargs[:classes],
34
+ SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_MAPPINGS.keys, scheme, DEFAULT_SCHEME)]
35
+ )
36
+ if count == 0 && hide_if_zero
37
+ @kwargs[:hidden] = true
38
+ end
39
+ end
40
+
41
+ def call
42
+ render(Primer::BaseComponent.new(**@kwargs)) { value }
43
+ end
44
+
45
+ private
46
+
47
+ def title
48
+ if @text.present?
49
+ @text
50
+ elsif @count.nil?
51
+ "Not available"
52
+ elsif @count == Float::INFINITY
53
+ "Infinity"
54
+ else
55
+ count = @count.to_i
56
+ str = number_with_delimiter([count, @limit].min)
57
+ str += "+" if count > @limit
58
+ str
59
+ end
60
+ end
61
+
62
+ def value
63
+ if @text.present?
64
+ @text
65
+ elsif @count.nil?
66
+ "" # CSS will hide it
67
+ elsif @count == Float::INFINITY
68
+ "∞"
69
+ else
70
+ if @round
71
+ count = [@count.to_i, @limit].min
72
+ precision = count.between?(100_000, 999_999) ? 0 : 1
73
+ units = {thousand: "k", million: "m", billion: "b"}
74
+ str = number_to_human(count, precision: precision, significant: false, units: units, format: "%n%u")
75
+ else
76
+ @count = @count.to_i
77
+ str = number_with_delimiter([@count, @limit].min)
78
+ end
79
+
80
+ str += "+" if @count.to_i > @limit
81
+ str
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,8 @@
1
+ <%= render Primer::BaseComponent.new(**@kwargs) do %>
2
+ <%= render summary.component do %>
3
+ <%= summary.content %>
4
+ <% end %>
5
+ <%= render body.component do %>
6
+ <%= body.content %>
7
+ <% end %>
8
+ <% end %>
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ #
5
+ # overlay - options are `none`, `default` and `dark`. Dictates the type of overlay to render with.
6
+ # button - options are `default` and `reset`. default will make the target a default primer ``.btn`
7
+ # reset will remove all styles from the <summary> element.
8
+ #
9
+ class DetailsComponent < Primer::Component
10
+ include ViewComponent::Slotable
11
+
12
+ OVERLAY_DEFAULT = :none
13
+ OVERLAY_MAPPINGS = {
14
+ OVERLAY_DEFAULT => "",
15
+ :default => "details-overlay",
16
+ :dark => "details-overlay details-overlay-dark",
17
+ }.freeze
18
+
19
+ with_slot :body, class_name: "Body"
20
+ with_slot :summary, class_name: "Summary"
21
+
22
+ def initialize(overlay: OVERLAY_DEFAULT, reset: false, **kwargs)
23
+ @kwargs = kwargs
24
+ @kwargs[:tag] = :details
25
+ @kwargs[:classes] = class_names(
26
+ kwargs[:classes],
27
+ OVERLAY_MAPPINGS[fetch_or_fallback(OVERLAY_MAPPINGS.keys, overlay.to_sym, OVERLAY_DEFAULT)],
28
+ "details-reset" => reset
29
+ )
30
+ end
31
+
32
+ def render?
33
+ summary.present? && body.present?
34
+ end
35
+
36
+ class Summary < Primer::Slot
37
+ def initialize(button: true, **kwargs)
38
+ @button = button
39
+
40
+ @kwargs = kwargs
41
+ @kwargs[:tag] = :summary
42
+ @kwargs[:role] = "button"
43
+ end
44
+
45
+ def component
46
+ return Primer::BaseComponent.new(**@kwargs) unless @button
47
+
48
+ Primer::ButtonComponent.new(**@kwargs)
49
+ end
50
+ end
51
+
52
+ class Body < Primer::Slot
53
+ def initialize(**kwargs)
54
+ @kwargs = kwargs
55
+ @kwargs[:tag] ||= :div
56
+ end
57
+
58
+ def component
59
+ Primer::BaseComponent.new(**@kwargs)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,8 @@
1
+ <%= render Primer::BaseComponent.new(**@kwargs) do %>
2
+ <% if @header.present? %>
3
+ <div class="dropdown-header">
4
+ <%= @header %>
5
+ </div>
6
+ <% end %>
7
+ <%= content %>
8
+ <% end %>
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Primer
4
+ class DropdownMenuComponent < Primer::Component
5
+ SCHEME_DEFAULT = :default
6
+ SCHEME_MAPPINGS = {
7
+ SCHEME_DEFAULT => "",
8
+ :dark => "dropdown-menu-dark",
9
+ }.freeze
10
+
11
+ DIRECTION_DEFAULT = :se
12
+ DIRECTION_OPTIONS = [DIRECTION_DEFAULT, :sw, :w, :e, :ne, :s]
13
+
14
+ def initialize(direction: DIRECTION_DEFAULT, scheme: SCHEME_DEFAULT, header: nil, **kwargs)
15
+ @header, @direction, @kwargs = header, direction, kwargs
16
+
17
+ @kwargs[:tag] = "details-menu"
18
+ @kwargs[:role] = "menu"
19
+
20
+ @kwargs[:classes] = class_names(
21
+ @kwargs[:classes],
22
+ "dropdown-menu",
23
+ "dropdown-menu-#{fetch_or_fallback(DIRECTION_OPTIONS, direction, DIRECTION_DEFAULT)}",
24
+ SCHEME_MAPPINGS[fetch_or_fallback(SCHEME_MAPPINGS.keys, scheme, SCHEME_DEFAULT)]
25
+ )
26
+ end
27
+ end
28
+ end