Class: Shadcn::Toggle::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/toggle/component.rb

Overview

Port of registry/new-york-v4/ui/toggle.tsx

Constant Summary

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

#call, #css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #tag_name

Constructor Details

#initialize(variant: :default, size: :default, pressed: false, disabled: false, **attributes) ⇒ Component

Returns a new instance of Component.



53
54
55
56
57
58
59
60
# File 'app/components/shadcn/toggle/component.rb', line 53

def initialize(variant: :default, size: :default, pressed: false, disabled: false,
               **attributes)
  @variant = variant&.to_sym || :default
  @size = size&.to_sym || :default
  @pressed = pressed
  @disabled = disabled
  super(**attributes)
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



51
52
53
# File 'app/components/shadcn/toggle/component.rb', line 51

def disabled
  @disabled
end

#pressedObject (readonly)

Returns the value of attribute pressed.



51
52
53
# File 'app/components/shadcn/toggle/component.rb', line 51

def pressed
  @pressed
end

#sizeObject (readonly)

Returns the value of attribute size.



51
52
53
# File 'app/components/shadcn/toggle/component.rb', line 51

def size
  @size
end

#variantObject (readonly)

Returns the value of attribute variant.



51
52
53
# File 'app/components/shadcn/toggle/component.rb', line 51

def variant
  @variant
end

Class Method Details

.variant_classes(variant: :default, size: :default, class: nil) ⇒ Object

Exposed the way shadcn exports toggleVariants, for ToggleGroupItem.



42
43
44
45
46
47
48
49
# File 'app/components/shadcn/toggle/component.rb', line 42

def self.variant_classes(variant: :default, size: :default, class: nil)
  style_config.compile(
    default_style_name.to_sym,
    variant: variant&.to_sym || :default,
    size: size&.to_sym || :default,
    class: binding.local_variable_get(:class)
  )
end

Instance Method Details

#element_attributes(**defaults) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/components/shadcn/toggle/component.rb', line 66

def element_attributes(**defaults)
  super(**{
    type: "button",
    disabled: (true if disabled),
    "aria-pressed" => pressed.to_s,
    "data-state" => (pressed ? "on" : "off"),
    "data-disabled" => (true if disabled),
    "data-controller" => "shadcn--toggle",
    "data-shadcn--toggle-pressed-value" => pressed,
    "data-shadcn--toggle-disabled-value" => disabled,
    "data-action" => "shadcn--toggle#toggle"
  }.merge(defaults))
end

#style_variantsObject



62
63
64
# File 'app/components/shadcn/toggle/component.rb', line 62

def style_variants
  { variant:, size: }
end