Class: NitroKit::SettingsLayout

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

Defined Under Namespace

Classes: Item

Constant Summary

Constants inherited from Component

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SettingsLayout.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/components/nitro_kit/settings_layout.rb', line 7

def initialize(id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil)
  @label = nil
  @items = []
  @content = nil
  @phase = nil
  @navigation_declared = false
  @current_item = false

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

Instance Method Details

#content(&block) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
# File 'app/components/nitro_kit/settings_layout.rb', line 61

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

  @content = block
  nil
end

#item(text, href:, icon: nil, current: false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/components/nitro_kit/settings_layout.rb', line 45

def item(text, href:, icon: nil, current: false)
  ensure_phase!(:navigation, :item)
  text = validate_text!(:text, text)
  href = validate_text!(:href, href)
  icon = item_icon(icon)
  current = validate_boolean!(:current, current)

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

  @current_item = true if current
  @items << Item.new(text:, href:, icon:, current:)
  nil
end

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
# File 'app/components/nitro_kit/settings_layout.rb', line 34

def navigation(label:, &declarations)
  ensure_phase!(:structure, :navigation)
  raise ArgumentError, "SettingsLayout accepts exactly one navigation region" if @navigation_declared
  raise ArgumentError, "SettingsLayout navigation requires a block" unless declarations

  @label = validate_text!(:label, label)
  @navigation_declared = true
  collect_items(&declarations)
  nil
end

#view_template(&declarations) ⇒ Object



25
26
27
28
29
30
31
32
# File 'app/components/nitro_kit/settings_layout.rb', line 25

def view_template(&declarations)
  collect_regions(&declarations)

  div(**root_attributes) do
    render_navigation
    render_content
  end
end