Switch
Data inputAn on/off switch with bind:checked, for a setting that applies at once. With name it submits with a form. Inside a Field it takes its id, label, description, error and disabled state from the Field; use orientation="horizontal" to put the label beside it.
import { Switch } from '@limerdev/zest'; Use for
A setting that takes effect immediately: notifications on, dark rows, show archived.
Don't
A choice that only counts when a form is submitted: use
Checkbox. A Switch without a label.Examples
Basic
A Switch applies at once.
<script lang="ts">
import { Field, Switch } from '@limerdev/zest';
let archived = $state(false);
</script>
<Field.Root orientation="horizontal" class="w-auto">
<Switch bind:checked={archived} />
<Field.Label>Show archived projects</Field.Label>
</Field.Root> Settings
A list of settings: label and description first, the switch at the end.
One email per approved week.
If the week has days without hours.
Turned off by your organisation.
<script lang="ts">
import { Field, Switch } from '@limerdev/zest';
</script>
<Field.Group class="max-w-md">
<Field.Root orientation="horizontal">
<Field.Content>
<Field.Label>Email me when a week is approved</Field.Label>
<Field.Description>One email per approved week.</Field.Description>
</Field.Content>
<Switch checked />
</Field.Root>
<Field.Root orientation="horizontal">
<Field.Content>
<Field.Label>Remind me on Fridays</Field.Label>
<Field.Description>If the week has days without hours.</Field.Description>
</Field.Content>
<Switch />
</Field.Root>
<Field.Root orientation="horizontal" disabled>
<Field.Content>
<Field.Label>Weekly summary</Field.Label>
<Field.Description>Turned off by your organisation.</Field.Description>
</Field.Content>
<Switch />
</Field.Root>
</Field.Group> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
Switch
| Prop | Type | Default | Description |
|---|---|---|---|
| checked bindable | boolean | false | The checked state of the switch. |
| disabled | null | false | true | false | Whether the switch is disabled. |
| name | string | undefined | The name of the switch used in form submission. If not provided, the hidden input will not be rendered. |
| onCheckedChange | OnChangeFn<boolean> | – | A callback function called when the checked state changes. |
| required | boolean | false | Whether the switch is required (for form validation). |
| value | any | undefined | The value of the switch used in form submission. |