Class: RubyUI::Toggle

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

Direct Known Subclasses

ToggleGroupItem

Constant Summary collapse

BASE_CLASSES =
[
  "inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium whitespace-nowrap",
  "transition-[color,box-shadow] outline-none",
  "hover:bg-muted hover:text-muted-foreground",
  "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50",
  "disabled:pointer-events-none disabled:opacity-50",
  "aria-invalid:border-destructive aria-invalid:ring-destructive/20",
  "data-[state=on]:bg-accent data-[state=on]:text-accent-foreground",
  "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
].freeze
VARIANT_CLASSES =
{
  default: "bg-transparent",
  outline: "border border-input bg-transparent shadow-xs hover:bg-accent hover:text-accent-foreground"
}.freeze
SIZE_CLASSES =
{
  sm: "h-8 min-w-8 px-1.5",
  default: "h-9 min-w-9 px-2",
  lg: "h-10 min-w-10 px-2.5"
}.freeze

Constants inherited from Base

Base::TAILWIND_MERGER

Instance Attribute Summary

Attributes inherited from Base

#attrs

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pressed: false, name: nil, value: "1", unpressed_value: nil, variant: :default, size: :default, disabled: false, wrapper: {}, **attrs) ⇒ Toggle

Returns a new instance of Toggle.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_ui/toggle/toggle.rb', line 31

def initialize(
  pressed: false,
  name: nil,
  value: "1",
  unpressed_value: nil,
  variant: :default,
  size: :default,
  disabled: false,
  wrapper: {},
  **attrs
)
  @pressed = pressed
  @name = name
  @value = value
  @unpressed_value = unpressed_value
  @variant = variant.to_sym
  @size = size.to_sym
  @disabled = disabled
  @wrapper = wrapper
  super(**attrs)
end

Class Method Details

.classes_for(variant:, size:) ⇒ Object



27
28
29
# File 'lib/ruby_ui/toggle/toggle.rb', line 27

def self.classes_for(variant:, size:)
  [BASE_CLASSES, VARIANT_CLASSES.fetch(variant, VARIANT_CLASSES[:default]), SIZE_CLASSES.fetch(size, SIZE_CLASSES[:default])]
end

Instance Method Details

#view_template(&block) ⇒ Object



53
54
55
56
57
58
# File 'lib/ruby_ui/toggle/toggle.rb', line 53

def view_template(&block)
  span(**wrapper_attrs) do
    button(**attrs, &block)
    render_hidden_input if @name
  end
end