Module: UI::SwitchBehavior

Included in:
Switch, SwitchComponent
Defined in:
app/behaviors/ui/switch_behavior.rb

Overview

UI::SwitchBehavior

Instance Method Summary collapse

Instance Method Details

#switch_classesObject

Returns combined CSS classes for the switch



51
52
53
54
55
56
57
# File 'app/behaviors/ui/switch_behavior.rb', line 51

def switch_classes
  classes_value = respond_to?(:classes, true) ? classes : @classes
  TailwindMerge::Merger.new.merge([
    switch_base_classes,
    classes_value
  ].compact.join(" "))
end

#switch_html_attributesObject

Returns HTML attributes for the switch button element



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/behaviors/ui/switch_behavior.rb', line 32

def switch_html_attributes
  {
    class: switch_classes,
    role: "switch",
    type: "button",
    tabindex: @disabled ? -1 : 0,
    "aria-checked": switch_aria_checked,
    "data-state": switch_state,
    "data-slot": "switch",
    data: switch_data_attributes
  }.tap do |attrs|
    if @disabled
      attrs[:disabled] = true
      attrs["aria-disabled"] = "true"
    end
  end
end

#switch_thumb_classesObject

Returns CSS classes for the thumb element



60
61
62
# File 'app/behaviors/ui/switch_behavior.rb', line 60

def switch_thumb_classes
  "bg-background dark:data-[state=unchecked]:bg-foreground dark:data-[state=checked]:bg-primary-foreground pointer-events-none block size-4 rounded-full ring-0 transition-transform data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:translate-x-0"
end