Toggle
Used to toggle between two states.
When To Use
- If you need to represent the switching between two states or on-off state.
- The difference between Toggle and Checkbox is that Toggle will trigger a state change directly when you toggle it, while Checkbox is generally used for state marking, which should work in conjunction with submit operation.
Basic
The most basic usage.
astro
<Toggle id="toggle1" /> Text & icon
With text and icon.
astro
<Toggle id="toggle2" label="Enable notifications" />
<br />
<Toggle id="toggle3" label="Custom thumb">
<span slot="thumb" class="h-3 w-3 bg-zinc-900 dark:bg-zinc-100 rounded-full"></span>
</Toggle> Disabled
Disabled state of Toggle.
astro
<Toggle id="toggle-disabled" disabled label="Disabled toggle" /> Error State
Add an error state to Toggle, which could display a message.
Network error
astro
<Toggle id="toggle-error" error="Network error" label="Sync data" /> Success State
Add a success state to Toggle.
Saved successfully!
astro
<Toggle id="toggle-success" success="Saved successfully!" label="Auto-save" /> API
| Property | Description | Type | Default |
|---|---|---|---|
| label | Text label to show next to the toggle | string | '' |
| variant | Visual style of the toggle | 'default' | 'default' |
| checked | Initial checked state (uncontrolled) | boolean | false |
| error | Indicates an error state or displays an error message | string | boolean | |
| success | Indicates a success state or displays a success message | string | boolean | |
| disabled | Whether the toggle is disabled | boolean | false |