Skip to content

Commit 4134e0f

Browse files
committed
simplify rendering mobile actions
1 parent c6fad7f commit 4134e0f

4 files changed

Lines changed: 45 additions & 59 deletions

File tree

app/components/primer/open_project/page_header.html.erb

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,7 @@
44
<div class="PageHeader-contextBar">
55
<%= @parent_link %>
66
<%= breadcrumbs %>
7-
<% if @mobile_segmented_control %>
8-
<%= render(@mobile_segmented_control, &@mobile_segmented_control_block) %>
9-
<% end %>
10-
<% if render_mobile_menu? %>
11-
<%= render(@mobile_action_menu) do |menu| %>
12-
<% menu.with_show_button(icon: :"kebab-horizontal", size: :small, "aria-label": @mobile_menu_label) %>
13-
<% @desktop_menu_block.call(menu) unless @desktop_menu_block.nil? %>
14-
<% end %>
15-
<% elsif actions.length == 1 && @mobile_action.present? %>
16-
<%= render(@mobile_action) { |el| @mobile_action_block.call(el) unless @mobile_action_block.nil?} %>
17-
<% end %>
7+
<%= render_mobile_actions %>
188
</div>
199
<% end %>
2010
<% end %>
@@ -30,18 +20,7 @@
3020

3121
<%# If there are no breadcrumbs, render mobile actions in the title bar instead %>
3222
<% unless breadcrumbs %>
33-
34-
<% if @mobile_segmented_control %>
35-
<%= render(@mobile_segmented_control, &@mobile_segmented_control_block) %>
36-
<% end %>
37-
<% if render_mobile_menu? %>
38-
<%= render(@mobile_action_menu) do |menu| %>
39-
<% menu.with_show_button(icon: :"kebab-horizontal", size: :small, "aria-label": @mobile_menu_label) %>
40-
<% @desktop_menu_block.call(menu) unless @desktop_menu_block.nil? %>
41-
<% end %>
42-
<% elsif actions.length == 1 && @mobile_action.present? %>
43-
<%= render(@mobile_action) { |el| @mobile_action_block.call(el) unless @mobile_action_block.nil?} %>
44-
<% end %>
23+
<%= render_mobile_actions %>
4524
<% end %>
4625
</div>
4726
<% end %>
@@ -54,4 +33,3 @@
5433
</div>
5534
<% end %>
5635
<% end %>
57-

app/components/primer/open_project/page_header.rb

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -275,43 +275,27 @@ def show_state?
275275
@state == STATE_DEFAULT
276276
end
277277

278-
def context_bar
279-
# Determine if breadcrumbs slot is present
280-
show_context_bar = breadcrumbs? || @parent_link.present?
281-
282-
# Check if there are mobile actions (single action or menu)
283-
has_mobile_actions = actions.any?
284-
285-
return unless show_context_bar || has_mobile_actions
286-
287-
display = if show_context_bar
288-
[:flex, :flex] # show on all screens
289-
else
290-
[:flex, :none] # only on mobile
291-
end
292-
293-
render Primer::BaseComponent.new(
294-
tag: :div,
295-
classes: "PageHeader-contextBar",
296-
display: display
297-
) do
298-
concat(@parent_link) if @parent_link.present?
299-
concat(breadcrumbs) if breadcrumbs?
300-
if @mobile_segmented_control
301-
concat(render(@mobile_segmented_control, &@mobile_segmented_control_block))
302-
end
278+
def render_mobile_actions
279+
safe_join([
280+
(render(@mobile_segmented_control, &@mobile_segmented_control_block) if @mobile_segmented_control),
281+
(render_mobile_action_menu if render_mobile_menu?),
282+
(render_single_mobile_action if actions.one? && @mobile_action.present?)
283+
].compact)
284+
end
303285

304-
if render_mobile_menu?
305-
concat(render(@mobile_action_menu) do |menu|
306-
menu.with_show_button(icon: :"kebab-horizontal", size: :small, "aria-label": @mobile_menu_label)
307-
@desktop_menu_block.call(menu) unless @desktop_menu_block.nil?
308-
end)
309-
elsif actions.length == 1 && @mobile_action.present?
310-
concat(render(@mobile_action) { |el| @mobile_action_block.call(el) unless @mobile_action_block.nil? })
311-
end
286+
private
287+
288+
def render_mobile_action_menu
289+
render(@mobile_action_menu) do |menu|
290+
menu.with_show_button(icon: :"kebab-horizontal", size: :small, "aria-label": @mobile_menu_label)
291+
@desktop_menu_block&.call(menu)
312292
end
313293
end
314294

295+
def render_single_mobile_action
296+
render(@mobile_action) { |el| @mobile_action_block&.call(el) }
297+
end
298+
315299
private
316300

317301
def set_action_arguments(system_arguments, scheme: nil)

previews/primer/open_project/page_header_preview.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,10 @@ def without_breadcrumbs
278278
render(Primer::OpenProject::PageHeader.new) do |header|
279279
header.with_title { "Hello" }
280280
header.with_description { "This PageHeader does not have any breadcrumbs." }
281-
282281
header.with_action_button(mobile_icon: "star", mobile_label: "Star") do |button|
283282
button.with_leading_visual_icon(icon: "star")
284283
"Star"
285284
end
286-
287285
end
288286
end
289287
end

test/components/primer/open_project/page_header_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,32 @@ def test_renders_actions_without_breadcrumbs
318318
assert_selector(".PageHeader-actions .Button, .PageHeader-actions action-menu", minimum: 1)
319319
end
320320

321+
def test_renders_with_mobile_segmented_control
322+
render_inline(Primer::OpenProject::PageHeader.new(show_state: true)) do |header|
323+
header.with_title { "Title" }
324+
header.with_action_segmented_control(
325+
"aria-label": "Segmented control",
326+
mobile_system_arguments: { hide_labels: true }
327+
) do |control|
328+
control.with_item(label: "Preview", icon: :eye, selected: true)
329+
control.with_item(label: "Raw", icon: :"file-code")
330+
end
331+
end
332+
333+
assert_selector(".PageHeader-action")
334+
assert_selector(".PageHeader-action.SegmentedControl")
335+
end
336+
337+
def test_renders_single_action_with_mobile_action
338+
render_inline(Primer::OpenProject::PageHeader.new(show_state: true)) do |header|
339+
header.with_title { "Single Action Test" }
340+
header.with_action_button(mobile_icon: "plus", mobile_label: "Add") { "Add" }
341+
end
342+
343+
assert_text("Add")
344+
end
345+
346+
321347
private
322348

323349
def breadcrumb_elements

0 commit comments

Comments
 (0)