Class: RubyUI::Switch

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_ui/switch/switch.rb

Constant Summary

Constants inherited from Base

Base::TAILWIND_MERGER

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Constructor Details

#initialize(include_hidden: true, checked_value: "1", unchecked_value: "0", **attrs) ⇒ Switch

Returns a new instance of Switch.



5
6
7
8
9
10
# File 'lib/ruby_ui/switch/switch.rb', line 5

def initialize(include_hidden: true, checked_value: "1", unchecked_value: "0", **attrs)
  @include_hidden = include_hidden
  @checked_value = checked_value
  @unchecked_value = unchecked_value
  super(**attrs)
end

Instance Method Details

#view_templateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_ui/switch/switch.rb', line 12

def view_template
  label(
    role: "switch",
    class: [
      "peer inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors bg-input",
      "has-checked:bg-primary",
      "has-disabled:cursor-not-allowed has-disabled:opacity-50",
      "has-aria-disabled:cursor-not-allowed has-aria-disabled:opacity-50 has-aria-disabled:pointer-events-none",
      "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
    ]
  ) do
    input(type: "hidden", name: attrs[:name], value: @unchecked_value) if @include_hidden

    input(**attrs.merge(type: "checkbox", class: "hidden peer", value: @checked_value))

    span(class: [
      "pointer-events-none block h-5 w-5 rounded-full bg-background shadow-lg ring-0 transition-transform translate-x-0",
      "peer-checked:translate-x-5"
    ])
  end
end