Class: NitroKit::AppNavigation

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/app_navigation.rb

Defined Under Namespace

Classes: Entry, Item, Section

Constant Summary

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ AppNavigation

Returns a new instance of AppNavigation.



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
# File 'app/components/nitro_kit/app_navigation.rb', line 12

def initialize(
  label:,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @label = validate_text!(:label, label)
  @entries = []
  @entry_target = nil
  @phase = nil
  @header = nil
  @body = nil
  @footer = nil
  @spacer = false
  @current_item = false
  @item_count = 0

  super(
    component: :app_navigation,
    attributes: { id:, aria: { label: @label } }.compact,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



41
42
43
# File 'app/components/nitro_kit/app_navigation.rb', line 41

def label
  @label
end

Instance Method Details

#body(&content) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
# File 'app/components/nitro_kit/app_navigation.rb', line 63

def body(&content)
  ensure_phase!(:structure, :body)
  raise ArgumentError, "AppNavigation accepts exactly one body" if @body
  raise ArgumentError, "AppNavigation body requires a block" unless content

  @body = content
  nil
end

#dividerObject



144
145
146
147
148
# File 'app/components/nitro_kit/app_navigation.rb', line 144

def divider
  ensure_entry_phase!(:divider)
  @entry_target << Entry.new(kind: :divider)
  nil
end

Raises:

  • (ArgumentError)


72
73
74
75
76
77
78
79
# File 'app/components/nitro_kit/app_navigation.rb', line 72

def footer(&content)
  ensure_phase!(:structure, :footer)
  raise ArgumentError, "AppNavigation accepts at most one footer" if @footer
  raise ArgumentError, "AppNavigation footer requires a block" unless content

  @footer = content
  nil
end

#header(&content) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
# File 'app/components/nitro_kit/app_navigation.rb', line 54

def header(&content)
  ensure_phase!(:structure, :header)
  raise ArgumentError, "AppNavigation accepts at most one header" if @header
  raise ArgumentError, "AppNavigation header requires a block" unless content

  @header = content
  nil
end


6
# File 'app/components/nitro_kit/app_navigation.rb', line 6

alias_method :html_footer, :footer

#html_headerObject



5
# File 'app/components/nitro_kit/app_navigation.rb', line 5

alias_method :html_header, :header

#item(text, href:, icon: nil, badge: nil, badge_color: :neutral, current: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object



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
# File 'app/components/nitro_kit/app_navigation.rb', line 105

def item(
  text,
  href:,
  icon: nil,
  badge: nil,
  badge_color: :neutral,
  current: false,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  ensure_entry_phase!(:item)
  text = validate_text!(:text, text)
  href = validate_text!(:href, href)
  icon = item_icon(icon)
  badge = item_badge(badge, badge_color)
  current = validate_boolean!(:current, current)

  if current && @current_item
    raise ArgumentError, "AppNavigation accepts at most one current item"
  end

  @current_item = true if current
  @entry_target << Item.new(
    text:,
    href:,
    icon:,
    badge:,
    current:,
    html:,
    aria:,
    data:,
    css_class: desperately_need_a_class
  )
  @item_count += 1
  nil
end

#section(label: nil, &content) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/components/nitro_kit/app_navigation.rb', line 81

def section(label: nil, &content)
  ensure_phase!(:body, :section)
  raise ArgumentError, "AppNavigation section requires a block" unless content

  entries = []
  previous_entries = @entry_target
  previous_phase = @phase

  begin
    @entry_target = entries
    @phase = :section
    output = capture(self, &content)
    reject_rendered_output!(:section, output)
    unless entries.any?(Item)
      raise ArgumentError, "AppNavigation section requires at least one item"
    end
    previous_entries << Section.new(label: validate_optional_text!(:label, label), entries:)
    nil
  ensure
    @entry_target = previous_entries
    @phase = previous_phase
  end
end

#spacerObject

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
157
# File 'app/components/nitro_kit/app_navigation.rb', line 150

def spacer
  ensure_phase!(:body, :spacer)
  raise ArgumentError, "AppNavigation accepts at most one spacer" if @spacer

  @spacer = true
  @entry_target << Entry.new(kind: :spacer)
  nil
end

#view_template(&declarations) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'app/components/nitro_kit/app_navigation.rb', line 43

def view_template(&declarations)
  collect_structure(&declarations)
  collect_body

  nav(**root_attributes) do
    render_header
    render_body
    render_footer
  end
end