Class: Shadcn::Switch::Component
- Inherits:
-
ApplicationViewComponent
- Object
- ViewComponent::Base
- ApplicationViewComponent
- Shadcn::Switch::Component
- Defined in:
- app/components/shadcn/switch/component.rb
Overview
Port of registry/new-york-v4/ui/switch.tsx
Constant Summary collapse
- THUMB_CLASSES =
"pointer-events-none block rounded-full bg-background ring-0 " \ "transition-transform group-data-[size=default]/switch:size-4 " \ "group-data-[size=sm]/switch:size-3 " \ "data-[state=checked]:translate-x-[calc(100%-2px)] " \ "data-[state=unchecked]:translate-x-0 " \ "dark:data-[state=checked]:bg-primary-foreground " \ "dark:data-[state=unchecked]:bg-foreground"
- HIDDEN_INPUT_STYLE =
"transform:translateX(-100%);position:absolute;" \ "pointer-events:none;opacity:0;margin:0"
Constants inherited from ApplicationViewComponent
ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS
Instance Attribute Summary collapse
-
#checked ⇒ Object
readonly
Returns the value of attribute checked.
-
#disabled ⇒ Object
readonly
Returns the value of attribute disabled.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Attributes inherited from ApplicationViewComponent
Instance Method Summary collapse
- #call ⇒ Object
- #element_attributes(**defaults) ⇒ Object
-
#initialize(size: :default, name: nil, value: "on", checked: false, disabled: false, **attributes) ⇒ Component
constructor
A new instance of Component.
- #input_id ⇒ Object
- #state ⇒ Object
Methods inherited from ApplicationViewComponent
#css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name
Constructor Details
#initialize(size: :default, name: nil, value: "on", checked: false, disabled: false, **attributes) ⇒ Component
Returns a new instance of Component.
36 37 38 39 40 41 42 43 44 |
# File 'app/components/shadcn/switch/component.rb', line 36 def initialize(size: :default, name: nil, value: "on", checked: false, disabled: false, **attributes) @size = size&.to_sym || :default @name = name @value = value @checked = checked @disabled = disabled super(**attributes) end |
Instance Attribute Details
#checked ⇒ Object (readonly)
Returns the value of attribute checked.
34 35 36 |
# File 'app/components/shadcn/switch/component.rb', line 34 def checked @checked end |
#disabled ⇒ Object (readonly)
Returns the value of attribute disabled.
34 35 36 |
# File 'app/components/shadcn/switch/component.rb', line 34 def disabled @disabled end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
34 35 36 |
# File 'app/components/shadcn/switch/component.rb', line 34 def name @name end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
34 35 36 |
# File 'app/components/shadcn/switch/component.rb', line 34 def size @size end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
34 35 36 |
# File 'app/components/shadcn/switch/component.rb', line 34 def value @value end |
Instance Method Details
#call ⇒ Object
72 73 74 |
# File 'app/components/shadcn/switch/component.rb', line 72 def call safe_join([ render_element(body: thumb), hidden_input ].compact) end |
#element_attributes(**defaults) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/components/shadcn/switch/component.rb', line 54 def element_attributes(**defaults) super(**{ type: "button", role: "switch", value: value, disabled: (true if disabled), "aria-checked" => checked.to_s, "data-state" => state, "data-size" => size, "data-disabled" => (true if disabled), "data-controller" => "shadcn--switch", "data-shadcn--switch-checked-value" => checked, "data-shadcn--switch-disabled-value" => disabled, "data-shadcn--switch-input-id-value" => input_id, "data-action" => "click->shadcn--switch#toggle keydown->shadcn--switch#keydown" }.merge(defaults)) end |
#input_id ⇒ Object
50 51 52 |
# File 'app/components/shadcn/switch/component.rb', line 50 def input_id @input_id ||= "shadcn-switch-#{SecureRandom.hex(4)}" if name end |
#state ⇒ Object
46 47 48 |
# File 'app/components/shadcn/switch/component.rb', line 46 def state checked ? "checked" : "unchecked" end |