Class: NitroKit::Tabs

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

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#builder, from_template

Constructor Details

#initialize(default: nil, **attrs) ⇒ Tabs



5
6
7
8
9
10
11
12
13
14
# File 'app/components/nitro_kit/tabs.rb', line 5

def initialize(default: nil, **attrs)
  @default = default
  @id = attrs[:id] || SecureRandom.hex(6)

  super(
    attrs,
    data: { controller: "nk--tabs", nk__tabs_active_value: default },
    class: base_class
  )
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



16
17
18
# File 'app/components/nitro_kit/tabs.rb', line 16

def default
  @default
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'app/components/nitro_kit/tabs.rb', line 16

def id
  @id
end

Instance Method Details

#panel(key, **attrs) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/components/nitro_kit/tabs.rb', line 58

def panel(key, **attrs)
  builder do
    div(
      **mattr(
        attrs,
        aria: {
          hidden: (default != key).to_s,
          labelledby: tab_id(key, :tab)
        },
        class: panel_class,
        data: {
          key:,
          nk__tabs_target: "panel"
        },
        id: tab_id(key, :panel),
        name: key,
        role: "tabpanel"
      )
    ) do
      yield
    end
  end
end

#tab(key, text = nil, **attrs, &block) ⇒ Object



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

def tab(key, text = nil, **attrs, &block)
  builder do
    button(
      **mattr(
        attrs,
        aria: {
          selected: (default == key).to_s,
          controls: tab_id(key, :panel)
        },
        class: tab_class,
        data: {
          action: "nk--tabs#setActiveTab keydown.left->nk--tabs#prevTab keydown.right->nk--tabs#nextTab",
          key:,
          nk__tabs_key_param: key,
          nk__tabs_target: "tab"
        },
        id: tab_id(key, :tab),
        role: "tab",
        tabindex: default == key ? 0 : -1
      )
    ) do
      text_or_block(text, &block)
    end
  end
end

#tabs(**attrs) ⇒ Object



24
25
26
27
28
29
30
# File 'app/components/nitro_kit/tabs.rb', line 24

def tabs(**attrs)
  builder do
    div(**mattr, role: "tabtabs", class: tabs_class) do
      yield
    end
  end
end

#view_templateObject



18
19
20
21
22
# File 'app/components/nitro_kit/tabs.rb', line 18

def view_template
  div(**attrs) do
    yield
  end
end