Switch
A control for turning an option on or off.
Installation
Shadwire follows shadcn/ui's open code model: the component's source is copied into your application and becomes yours. Install it with the CLI:
The switch registry item installs these files:
-
app/components/ui_component.rb -
app/components/ui/switch_component.rb -
app/helpers/ui/switch_helper.rb -
vendor/shadwire/shadwire.css
Haven't installed the CLI yet? gem install shadwire, then shadwire init.
Usage
The component styles a native <input type="checkbox">
with role="switch". The sliding thumb is drawn by the
.shadwire-switch utility.
Or render the component
Ui::SwitchComponent
directly:
Examples
Default
A native checkbox with role="switch", so screen readers announce it as a switch.
On
Use checked: true to start it on; pair it with supporting text.
Get notified about activity on your account.
Disabled
disabled: true disables the native input.
With form_with
Use form.field_name/form.field_id. There is no hidden input, so treat a missing parameter as off.
API reference
ui_switch(**options)
wraps
Ui::SwitchComponent.new(**options).
| Argument | Type | Default | Description |
|---|---|---|---|
checked |
Boolean | false |
Renders the switch on. |
disabled |
Boolean | false |
Disables the native input. |
class / class_name |
String | nil |
Extra classes, merged after the base classes. |
**attrs |
Hash | {} |
Free HTML attributes (name, id, value, data:, aria:, …). |