Class: LightningUiKit::ToggleComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/lightning_ui_kit/toggle_component.rb

Constant Summary collapse

VARIANTS =
%i[default outline].freeze
SIZES =
%i[default sm lg].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pressed: false, variant: :default, size: :default, disabled: false, **options) ⇒ ToggleComponent

Returns a new instance of ToggleComponent.



5
6
7
8
9
10
11
# File 'app/components/lightning_ui_kit/toggle_component.rb', line 5

def initialize(pressed: false, variant: :default, size: :default, disabled: false, **options)
  @pressed = pressed
  @variant = VARIANTS.include?(variant) ? variant : :default
  @size = SIZES.include?(size) ? size : :default
  @disabled = disabled
  @options = options
end

Instance Attribute Details

#pressedObject (readonly)

Returns the value of attribute pressed.



30
31
32
# File 'app/components/lightning_ui_kit/toggle_component.rb', line 30

def pressed
  @pressed
end

Instance Method Details

#classesObject



13
14
15
16
17
18
19
20
# File 'app/components/lightning_ui_kit/toggle_component.rb', line 13

def classes
  base = "lui:inline-flex lui:items-center lui:justify-center lui:gap-2 lui:rounded-md lui:text-sm lui:font-medium lui:whitespace-nowrap lui:cursor-pointer lui:select-none lui:transition-colors " \
    "lui:text-foreground-muted lui:hover:bg-surface-hover lui:hover:text-foreground " \
    "lui:data-[state=on]:bg-surface-hover lui:data-[state=on]:text-foreground " \
    "lui:focus-visible:outline-2 lui:focus-visible:outline-offset-2 lui:focus-visible:outline-focus " \
    "lui:disabled:pointer-events-none lui:disabled:opacity-50"
  merge_classes([base, variant_classes, size_classes, @options[:class]].compact.join(" "))
end

#dataObject



32
33
34
35
36
37
38
# File 'app/components/lightning_ui_kit/toggle_component.rb', line 32

def data
  {
    controller: "lui-toggle",
    lui_toggle_pressed_value: @pressed,
    action: "click->lui-toggle#toggle"
  }.merge(@options[:data] || {})
end

#disabled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/components/lightning_ui_kit/toggle_component.rb', line 26

def disabled?
  @disabled
end

#stateObject



22
23
24
# File 'app/components/lightning_ui_kit/toggle_component.rb', line 22

def state
  @pressed ? "on" : "off"
end