forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsub_header.rb
More file actions
277 lines (223 loc) · 11.5 KB
/
Copy pathsub_header.rb
File metadata and controls
277 lines (223 loc) · 11.5 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# frozen_string_literal: true
module Primer
module OpenProject
# The SubHeader contains specific actions to modify the page content below, e.g a filter button or a create button
# It should not be used stand alone, but in combination with a PageHeader, either as a direct sibling or as part of a tab content
class SubHeader < Primer::Component
status :open_project
SHOWN_FILTER_TARGET_SELECTOR = "sub-header.shownItemsOnExpandedFilter"
FILTER_EXPAND_BUTTON_TARGET_SELECTOR = "sub-header.filterExpandButton"
MOBILE_ACTIONS_DISPLAY = [:flex, :flex, :none].freeze
DESKTOP_ACTIONS_DISPLAY = [:none, :none, :flex].freeze
# A button or custom content that will render on the right-hand side of the component.
#
# To render a button, call the `with_button` method, which accepts the arguments accepted by <%= link_to_component(Primer::Beta::Button) %>.
#
# To render custom content, call the `with_button_component` method and pass a block that returns HTML.
renders_many :actions, types: {
button: {
renders: lambda { |icon_only: false, leading_icon:, label:, **kwargs, &block|
if label.nil? || label.empty?
raise ArgumentError, "You need to provide a valid label."
end
kwargs[:icon] = leading_icon
kwargs[:aria] ||= merge_aria(
kwargs,
{ aria: { label: label } }
)
icon_args = kwargs.deep_dup
if icon_only
Primer::Beta::IconButton.new(**icon_args)
else
@mobile_actions ||= []
mobile_component = Primer::Beta::IconButton.new(display: MOBILE_ACTIONS_DISPLAY,
**icon_args)
@mobile_actions.push({ component: mobile_component, block: block })
Primer::OpenProject::SubHeader::Button.new(display: DESKTOP_ACTIONS_DISPLAY, **kwargs)
end
},
},
button_group: {
renders: lambda { |**kwargs|
Primer::OpenProject::SubHeader::ButtonGroup.new(**kwargs)
},
},
menu: {
renders: lambda { |icon_only: false, leading_icon:, label:, button_arguments: {}, **kwargs, &block|
if label.nil? || label.empty?
raise ArgumentError, "You need to provide a valid label."
end
kwargs[:leading_icon] = leading_icon
kwargs[:label] = label
kwargs[:button_arguments] = button_arguments
@mobile_actions ||= []
mobile_component = Primer::OpenProject::SubHeader::Menu.new(icon_only: true,
display: MOBILE_ACTIONS_DISPLAY,
**kwargs)
@mobile_actions.push({ component: mobile_component, block: block })
Primer::OpenProject::SubHeader::Menu.new(icon_only: icon_only, display: DESKTOP_ACTIONS_DISPLAY, **kwargs)
},
}
}
renders_one :filter_input, lambda { |name:, label:, **system_arguments|
if label.nil? || label.empty?
raise ArgumentError, "You need to provide a valid label."
end
system_arguments[:classes] = class_names(
system_arguments[:classes],
"SubHeader-filterInput",
"SubHeader-filterInput_hiddenClearButton"
)
system_arguments[:placeholder] ||= I18n.t("button_filter")
system_arguments[:leading_visual] ||= { icon: :search }
system_arguments[:visually_hide_label] ||= true
system_arguments[:input_width] ||= :medium
system_arguments[:clear_button_label] ||= I18n.t(:button_clear)
system_arguments[:data] ||= {}
system_arguments[:data] = merge_data(
system_arguments,
{ data: { target: "sub-header.filterInput" } }
)
system_arguments[:show_clear_button] = true if system_arguments[:show_clear_button].nil?
if system_arguments[:show_clear_button]
system_arguments[:data][:action] ||= ""
system_arguments[:data][:action] += " input:sub-header#toggleFilterInputClearButton focus:sub-header#toggleFilterInputClearButton"
end
trigger_display = @collapsed_search ? :inline_flex : [:inline_flex, :inline_flex, :none]
@collapsed_filter_trigger = Primer::Beta::IconButton.new(icon: system_arguments[:leading_visual][:icon],
display: trigger_display,
aria: { label: label },
mr: 2,
"data-action": "click:sub-header#expandFilterInput",
"data-targets": FILTER_EXPAND_BUTTON_TARGET_SELECTOR)
@collapsed_filter_cancel = Primer::Beta::IconButton.new(icon: :x,
"aria-label": I18n.t(:button_cancel),
scheme: :invisible,
display: :none,
data: {
targets: SHOWN_FILTER_TARGET_SELECTOR,
action: "click:sub-header#collapseFilterInput"
})
Primer::Alpha::TextField.new(name: name, label: label, **system_arguments)
}
# A button or custom content that will render on the left-hand side of the component, next to the filter input.
#
# To render a button, call the `with_filter_button` method, which accepts the arguments accepted by <%= link_to_component(Primer::Beta::Button) %>.
#
# To render custom content, call the `with_filter_component` method and pass a block that returns HTML.
renders_one :filter_button, types: {
button: {
renders: lambda { |icon_only: false, leading_icon: :filter, mobile_label: I18n.t("button_filter"), **kwargs|
kwargs[:mr] ||= 2
kwargs[:icon] = leading_icon
kwargs[:aria] ||= merge_aria(
kwargs,
{ aria: { label: mobile_label } }
)
icon_args = kwargs.deep_dup
icon_args = set_as_hidden_filter_target(icon_args)
if icon_only
Primer::Beta::IconButton.new(**icon_args)
else
@mobile_filter_button = Primer::Beta::IconButton.new(display: MOBILE_ACTIONS_DISPLAY,
**icon_args)
Primer::OpenProject::SubHeader::Button.new(display: DESKTOP_ACTIONS_DISPLAY, **kwargs)
end
},
as: :filter_button
},
component: {
# A generic slot to render a custom filter component
renders: lambda { |**kwargs|
deny_tag_argument(**kwargs)
kwargs[:tag] = :div
kwargs[:mr] ||= 2
kwargs = set_as_hidden_filter_target(kwargs)
Primer::BaseComponent.new(**kwargs)
},
as: :filter_component
}
}
# A slot for a generic sorting component. Will be rendered next to the search input
renders_one :quick_sort, lambda { |**kwargs|
deny_tag_argument(**kwargs)
QuickActionComponent.new(**kwargs)
}
# A slot for a generic sorting component. Will be rendered next to the sorting button
renders_one :quick_group, lambda { |**kwargs|
deny_tag_argument(**kwargs)
QuickActionComponent.new(**kwargs)
}
# Quick filters shown in the left pane next to the search bar (0–5 items total across all types).
# Hidden on smaller screens when more than one `quick_filter` is provided. Requires `filter_button` when using more than one `quick_filter`.
# Supports ActionMenus, Buttons, IconButtons, SelectPanels, and SegmentedControls inside the block.
renders_many :quick_filters, lambda { |**kwargs|
deny_tag_argument(**kwargs)
QuickActionComponent.new(**kwargs)
}
renders_one :segmented_control, lambda { |**system_arguments, &block|
deny_tag_argument(**system_arguments)
system_arguments[:mr] ||= 2
system_arguments = set_as_hidden_filter_target(system_arguments)
@segmented_control_block = block
@mobile_segmented_control = Primer::OpenProject::SubHeader::SegmentedControl.new(
hide_labels: true,
display: MOBILE_ACTIONS_DISPLAY,
**system_arguments
)
Primer::OpenProject::SubHeader::SegmentedControl.new(display: DESKTOP_ACTIONS_DISPLAY, **system_arguments)
}
renders_one :text, lambda { |**system_arguments|
system_arguments[:font_weight] ||= :bold
system_arguments[:mx] ||= 2
Primer::Beta::Text.new(**system_arguments)
}
# A slot for a generic component which will be shown in a second row below the rest, spanning the whole width
renders_one :bottom_pane_component, lambda { |**system_arguments|
deny_tag_argument(**system_arguments)
system_arguments[:tag] = :div
system_arguments[:mt] ||= 3
Primer::BaseComponent.new(**system_arguments)
}
# @param collapsed_search [Boolean] When true, the search bar starts collapsed as an icon button on all screen sizes. Clicking expands it.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(collapsed_search: false, **system_arguments)
@collapsed_search = collapsed_search
@system_arguments = system_arguments
@system_arguments[:tag] = :"sub-header"
filter_container_display = collapsed_search ? :none : DESKTOP_ACTIONS_DISPLAY
@filter_container = Primer::BaseComponent.new(tag: :div,
classes: "SubHeader-filterContainer",
display: filter_container_display,
mr: 2,
data: { targets: SHOWN_FILTER_TARGET_SELECTOR })
@system_arguments[:classes] = class_names(
"SubHeader",
system_arguments[:classes]
)
end
def before_render
all_quick_actions = [quick_sort, quick_group].compact + quick_filters
if quick_filters.size > 1 && filter_button.nil?
raise ArgumentError, "You must provide a filter_button when using quick_filters."
end
if quick_filters.size > 5
raise ArgumentError, "SubHeader supports a maximum of 5 quick_filters, got #{all_quick_actions.size}."
end
if quick_filters.size > 1
quick_filters.each do |qf|
qf.merge_system_arguments!(display: DESKTOP_ACTIONS_DISPLAY)
end
end
@system_arguments[:classes] = class_names(
@system_arguments[:classes],
"SubHeader--emptyLeftPane" => !segmented_control? && !filter_button && !filter_input && all_quick_actions.empty?
)
end
def set_as_hidden_filter_target(system_arguments)
system_arguments[:classes] = class_names(system_arguments[:classes], "SubHeader-hiddenOnExpand")
system_arguments
end
end
end
end