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
|
# File 'app/helpers/layered/ui/navigation_helper.rb', line 20
def l_ui_navigation_section(heading = nil, icon: nil, icon_path: nil, icon_html: nil, collapsible: false, expanded: true, storage_key: nil, separated: false, &block)
stack = (Thread.current[:l_ui_navigation_section_stack] ||= [])
frame = { active: false }
stack.push(frame)
begin
children = block_given? ? capture(&block) : "".html_safe
ensure
stack.pop
end
has_active_descendant = frame[:active]
if (parent = stack.last) && has_active_descendant
parent[:active] = true
end
is_open = has_active_descendant || expanded
is_collapsible = collapsible && heading.present?
panel_id = is_collapsible ? "l-ui-navigation-section-#{SecureRandom.hex(4)}" : nil
section_classes = ["l-ui-navigation__section"]
section_classes << "l-ui-navigation__section--has-heading" if heading.present?
section_classes << "l-ui-navigation__section--collapsible" if is_collapsible
section_classes << "l-ui-navigation__section--separated" if separated
data = {}
if is_collapsible
data["controller"] = "l-ui--navigation-section"
data["l-ui--navigation-section-storage-key-value"] = storage_key.to_s if storage_key.present?
data["l-ui--navigation-section-force-open-value"] = "true" if has_active_descendant
end
content_tag(:li, class: section_classes.join(" "), data: data) do
parts = []
if heading.present?
heading_inner = l_ui_navigation_item_inner(heading, icon, icon_path, icon_html)
if is_collapsible
chevron = content_tag(:span, "", class: "l-ui-navigation__section-chevron", "aria-hidden": "true")
parts << content_tag(:button,
heading_inner + chevron,
type: "button",
class: "l-ui-navigation__section-toggle",
"aria-expanded": is_open.to_s,
"aria-controls": panel_id,
data: { "l-ui--navigation-section-target": "toggle", action: "click->l-ui--navigation-section#toggle" }
)
else
parts << content_tag(:div, heading_inner, class: "l-ui-navigation__section-heading")
end
end
panel_attrs = { class: "l-ui-navigation__section-items", role: "list" }
if is_collapsible
panel_attrs[:id] = panel_id
panel_attrs[:data] = { "l-ui--navigation-section-target": "panel" }
panel_attrs[:hidden] = true unless is_open
end
parts << content_tag(:ul, children, **panel_attrs)
safe_join(parts)
end
end
|