Module: Layered::Ui::NavigationHelper

Defined in:
app/helpers/layered/ui/navigation_helper.rb

Instance Method Summary collapse

Instance Method Details

#l_ui_navigation_item(label, path, active: nil, match: :exact, icon: nil, icon_path: nil, icon_html: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/layered/ui/navigation_helper.rb', line 6

def l_ui_navigation_item(label, path, active: nil, match: :exact, icon: nil, icon_path: nil, icon_html: nil)
  is_active = active.nil? ? l_ui_navigation_item_self_active?(path, match) : active

  css_class = is_active ? "l-ui-navigation__item--active" : "l-ui-navigation__item"
  options = { class: css_class }
  options["aria-current"] = "page" if current_page?(path)

  l_ui_navigation_register_active if is_active

  link_content = l_ui_navigation_item_inner(label, icon, icon_path, icon_html)

  (:li) { link_to(link_content, path, options) }
end

#l_ui_navigation_section(heading = nil, icon: nil, icon_path: nil, icon_html: nil, collapsible: false, expanded: true, storage_key: nil, separated: false, &block) ⇒ Object



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

  (: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 = (:span, "", class: "l-ui-navigation__section-chevron", "aria-hidden": "true")
        parts << (: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 << (: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 << (:ul, children, **panel_attrs)

    safe_join(parts)
  end
end