Class: NitroKit::Switch

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

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#builder, from_template

Constructor Details

#initialize(checked: false, size: :md, description: nil, **attrs) ⇒ Switch



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

def initialize(
  checked: false,
  size: :md,
  description: nil,
  **attrs
)
  @checked = checked
  @size = size
  @description = description

  super(**attrs)
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



18
19
20
# File 'app/components/nitro_kit/switch.rb', line 18

def checked
  @checked
end

#descriptionObject (readonly)

Returns the value of attribute description.



18
19
20
# File 'app/components/nitro_kit/switch.rb', line 18

def description
  @description
end

#sizeObject (readonly)

Returns the value of attribute size.



18
19
20
# File 'app/components/nitro_kit/switch.rb', line 18

def size
  @size
end

Instance Method Details

#view_templateObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/components/nitro_kit/switch.rb', line 20

def view_template
  button(
    **mattr(
      **attrs,
      type: "button",
      class: [ base_class, size_class ],
      data: { controller: "nk--switch", action: "nk--switch#toggle" },
      role: "switch",
      aria: { checked: checked.to_s }
    )
  ) do
    span(class: "sr-only") { description }
    handle
  end
end