Forms
Switch
Turns a setting on or off right away. 48 × 28 px, primary blue when on, grey when off.
import { Switch } from '@statix/ui/switch';Playground
<Switch id="updates" checked />
<Label for="updates">Automatic updates</Label>Examples
With a label
Bind checked and pair the switch with a Label.
<script lang="ts">
import { Label } from '@statix/ui/label';
import { Switch } from '@statix/ui/switch';
let updates = $state(true);
</script>
<div class="flex items-center gap-3">
<Switch id="sw-updates" bind:checked={updates} />
<Label for="sw-updates">Automatic updates</Label>
</div>With a description
Put the switch on the right of a horizontal Field. Let the description say what the current state means.
On. We update WordPress and plugins every night after the backup.
<script lang="ts">
import * as Field from '@statix/ui/field';
import { Switch } from '@statix/ui/switch';
let updates = $state(true);
</script>
<Field.Field orientation="horizontal" class="w-full max-w-md">
<Field.Content>
<Field.Label for="swd-updates">Automatic updates</Field.Label>
<Field.Description>
{updates
? 'On. We update WordPress and plugins every night after the backup.'
: 'Off. You get an email when updates are waiting.'}
</Field.Description>
</Field.Content>
<Switch id="swd-updates" bind:checked={updates} />
</Field.Field>Settings list
Several switches in a card, separated by hairlines.
Every night at 03:10. We keep them for 14 days.
We check yoga-berger.de every minute and email you if it is down.
Makes pages load faster. Turn it off while you edit the design.
<script lang="ts">
import * as Field from '@statix/ui/field';
import { Switch } from '@statix/ui/switch';
const settings = [
{
id: 'backup',
label: 'Daily backups',
description: 'Every night at 03:10. We keep them for 14 days.',
checked: true
},
{
id: 'uptime',
label: 'Uptime alerts',
description: 'We check yoga-berger.de every minute and email you if it is down.',
checked: true
},
{
id: 'cache',
label: 'Page cache',
description: 'Makes pages load faster. Turn it off while you edit the design.',
checked: false
}
];
</script>
<div class="w-full max-w-lg rounded-card bg-card p-2 shadow-card">
<Field.Group class="gap-0">
{#each settings as setting, i (setting.id)}
{#if i > 0}<div class="mx-4 h-px bg-border" role="presentation"></div>{/if}
<Field.Field orientation="horizontal" class="p-4">
<Field.Content>
<Field.Label for="set-{setting.id}">{setting.label}</Field.Label>
<Field.Description>{setting.description}</Field.Description>
</Field.Content>
<Switch id="set-{setting.id}" checked={setting.checked} />
</Field.Field>
{/each}
</Field.Group>
</div>Sizes
md (48 × 28, default) and sm (36 × 20) for dense tables.
<script lang="ts">
import { Switch } from '@statix/ui/switch';
</script>
<Switch size="sm" aria-label="Small switch" checked />
<Switch aria-label="Default switch" checked />Disabled
Set data-disabled on the Field too, so the label fades with the switch.
Always on. We renew it for you before it expires.
<script lang="ts">
import * as Field from '@statix/ui/field';
import { Switch } from '@statix/ui/switch';
</script>
<Field.Field orientation="horizontal" data-disabled="true" class="w-full max-w-md">
<Field.Content>
<Field.Label for="sw-ssl">SSL certificate</Field.Label>
<Field.Description>Always on. We renew it for you before it expires.</Field.Description>
</Field.Content>
<Switch id="sw-ssl" checked disabled />
</Field.Field>Guidelines
Do
- Use a switch when the change applies immediately, such as turning on automatic updates.
- Name the setting in the label, not the action: "Automatic updates", not "Turn on updates".
- Show feedback (a toast or the description) when switching has a visible effect.
Don't
- Don't use a switch inside a form that needs a Save button. Use Checkbox there.
- Don't put "On" and "Off" text next to the switch. The fill already shows the state.
Accessibility
- Renders a <button role="switch"> with aria-checked. Space and Enter toggle it.
- Label it with <Label for> or aria-label.
- A hidden native input submits the value when name is set.
- The hit area extends 12px left and right and 8px up and down beyond the track.
API reference
Switch
renders<button role="switch"> and accepts its attributes| Prop | Type | Default |
|---|---|---|
checked bind | boolean Whether it is on. | false |
onCheckedChange | (checked: boolean) => void Called when the user toggles it. | – |
size | 'sm' | 'md' md is 48 × 28, sm is 36 × 20. default is an alias of md. | 'md' |
disabled | boolean Disables the switch. | false |
required | boolean Marks it as required in a form. | false |
name | string Name of the hidden input for form submission. | – |
value | string Value submitted when on. | 'on' |
ref bind | HTMLButtonElement | null The rendered element. | – |
class | ClassValue Merged with the default classes; yours win. | – |
| Data attribute | Meaning |
|---|---|
data-slot="switch" | On the track. The thumb has data-slot="switch-thumb". |
data-state | "checked" or "unchecked". |
data-size | "md" or "sm". |
data-disabled | Present when disabled. |