forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbutton_group.rb
More file actions
126 lines (108 loc) · 4.67 KB
/
Copy pathbutton_group.rb
File metadata and controls
126 lines (108 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
module Primer
module Beta
# Use `ButtonGroup` to render a series of buttons.
class ButtonGroup < Primer::Component
status :beta
# @!parse
# # Adds a button.
# #
# # @param icon [Symbol] If included, adds a <%= link_to_component(Primer::Beta::IconButton) %> with the given <%= link_to_octicons %>. Otherwise, a <%= link_to_component(Primer::Beta::Button) %> is added instead.
# # @param system_arguments [Hash] The arguments accepted by <%= link_to_component(Primer::Beta::Button) %> or <%= link_to_component(Primer::Beta::IconButton) %>, depending on the value of the `icon:` argument.
# def with_button(icon: nil, **system_arguments)
# end
# @!parse
# # Adds a button that activates a menu when clicked.
# #
# # @param system_arguments [Hash] The arguments accepted by <%= link_to_component(Primer::Beta::ButtonGroup::MenuButton) %>.
# def with_menu_button(**system_arguments)
# end
# @!parse
# # Adds a <%= link_to_component(Primer::Beta::ClipboardCopyButton) %>.
# #
# # @param system_arguments [Hash] The arguments accepted by <%= link_to_component(Primer::Beta::ClipboardCopyButton) %>.
# def with_clipboard_copy_button(**system_arguments)
# end
# List of buttons to be rendered. Add buttons via the `#with_button`, `#with_menu_button`, and `#with_clipboard_copy_button` methods (see below).
renders_many :buttons, types: {
button: {
renders: lambda { |icon: nil, **kwargs|
kwargs[:size] = @size
kwargs[:scheme] = @scheme
if icon
Primer::Beta::IconButton.new(icon: icon, **kwargs)
else
Primer::Beta::Button.new(**kwargs)
end
},
as: :button
},
menu_button: {
renders: lambda { |menu_arguments: {}, button_arguments: {}|
button_arguments[:size] = @size
button_arguments[:scheme] = @scheme
MenuButton.new(menu_arguments: menu_arguments, button_arguments: button_arguments)
},
as: :menu_button
},
clipboard_copy_button: {
renders: lambda { |**kwargs|
kwargs[:size] = @size
kwargs[:scheme] = @scheme
Primer::Beta::ClipboardCopyButton.new(**kwargs)
},
as: :clipboard_copy_button
}
}
# @param scheme [Symbol] DEPRECATED. <%= one_of(Primer::Beta::Button::SCHEME_OPTIONS) %>
# @param size [Symbol] <%= one_of(Primer::Beta::Button::SIZE_OPTIONS) %>
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(scheme: Primer::Beta::Button::DEFAULT_SCHEME, size: Primer::Beta::Button::DEFAULT_SIZE, **system_arguments)
@size = size
@scheme = scheme
@system_arguments = deny_tag_argument(**system_arguments)
@system_arguments[:tag] = :div
@system_arguments[:classes] = class_names(
"ButtonGroup",
system_arguments[:classes]
)
end
def render?
buttons.any?
end
# Renders a button in a <%= link_to_component(Primer::Beta::ButtonGroup) %> that displays an <%= link_to_component(Primer::Alpha::ActionMenu) %> when clicked.
# This component should not be used outside of a `ButtonGroup` context.
#
# This component yields both the button and the list to the block when rendered.
#
# ```erb
# <%= render(Primer::Beta::ButtonGroup.new) do |group| %>
# <% group.with_menu_button do |menu, button| %>
# <% menu.with_item(label: "Item 1") %>
# <% button.with_trailing_visual_icon(icon: "triangle-down") %>
# <% end %>
# <% end %>
# ```
#
class MenuButton < Primer::Component
# @param menu_arguments [Hash] The arguments accepted by <%= link_to_component(Primer::Alpha::ActionMenu) %>.
# @param button_arguments [Hash] The arguments accepted by <%= link_to_component(Primer::Beta::Button) %> or <%= link_to_component(Primer::Beta::IconButton) %>, depending on the value of the `icon:` argument.
def initialize(menu_arguments: {}, button_arguments: {})
@menu = Primer::Alpha::ActionMenu.new(**menu_arguments)
@button = @menu.with_show_button(icon: "triangle-down", **button_arguments)
end
def render_in(view_context, **_kwargs, &block)
super(view_context) do
block.call(@menu, @button)
end
end
def before_render
content
end
def call
render(@menu)
end
end
end
end
end