Skip to content

Switch

An on/off switch. Inside a Field it takes its id (the field’s controlId), disabled, required, state, and aria-describedby from there. An error state sets aria-invalid. Always pair it with a Label. A switch-info, switch-success, switch-warning, or switch-danger class sets the track color while it is on.

Built on bits-ui Switch.

<Field class="flex items-center gap-2">
<Switch bind:checked />
<Label>Notifications</Label>
</Field>
<Switch
  checked={false}
  disabled={false}
/>
  • Two states, on and off, driven by one bindable prop: checked.
  • Keyboard and screen reader behavior from the WAI-ARIA switch pattern, via bits-ui.
  • Submits with the form through a hidden <input> when given a name.
  • The label is static. It describes the setting and does not change when the switch flips.

bind:checked keeps your state and the switch in sync. State & binding covers both modes.

checked: false

Code
TwoWay.svelte
<script lang="ts">
import { Field, Label, Switch } from "@viamrobotics/prime-ui";
let enabled = $state(false);
</script>
<div class="not-content">
<Field class="flex items-center gap-2">
<Switch bind:checked={enabled} />
<Label>Notifications</Label>
</Field>
<p class="mt-3 text-sm text-subtle-2">checked: <code>{enabled}</code></p>
</div>

Give bind:checked a getter and a setter. Here the switch can be turned on but never off. State & binding has the rule.

armed: false

Code
Controlled.svelte
<script lang="ts">
import { Field, Label, Switch } from "@viamrobotics/prime-ui";
let armed = $state(false);
</script>
<div class="not-content">
<Field class="flex items-center gap-2">
<Switch
bind:checked={
() => armed,
(next) => {
if (next) armed = true;
}
}
/>
<Label>Arm the machine (cannot be undone)</Label>
</Field>
<p class="mt-3 text-sm text-subtle-2">armed: <code>{armed}</code></p>
</div>

Put the switch and a Label in a Field. The switch takes controlId, disabled, required, state, and aria-describedby from the field, and the label and hint wire themselves to it. name and value make it part of the enclosing form. A state of error sets aria-invalid="true" and turns the track red.

Helps us improve the platform.

Code
InField.svelte
<script lang="ts">
import { Field, Hint, Label, Switch } from "@viamrobotics/prime-ui";
let checked = $state(true);
</script>
<Field class="not-content grid grid-cols-[auto_1fr] items-center gap-x-2">
<Switch bind:checked name="telemetry" value="on" />
<Label>Send telemetry</Label>
<Hint class="col-start-2">Helps us improve the platform.</Hint>
</Field>

A switch can show more settings when it is on. Read checked and render the extra fields below the switch.

Code
Reveals.svelte
<script lang="ts">
import { Field, Input, Label, Switch } from "@viamrobotics/prime-ui";
let useProxy = $state(false);
</script>
<div class="not-content flex max-w-xs flex-col gap-3">
<Field class="flex items-center gap-2">
<Switch bind:checked={useProxy} />
<Label>Use a proxy</Label>
</Field>
{#if useProxy}
<Field class="flex flex-col gap-1">
<Label>Proxy address</Label>
<Input placeholder="proxy.example.com:8080" />
</Field>
{/if}
</div>

Default

off on disabled off disabled on

Tone, while on

info success warning danger

Error

class lands on the root button, which is the track. The thumb is inside it and is not exposed. The tone classes switch-info, switch-success, switch-warning, and switch-danger set the track color while the switch is on, and an error state still paints over them. bits-ui sets these attributes on the track, so Tailwind variants like data-[state=checked]:bg-light work without a wrapper.

Attribute Value
data-state checked or unchecked
data-disabled present when disabled
aria-invalid true when state is error
KeyAction
SpaceToggles the switch.
EnterToggles the switch.
PropTypeDefaultDescription
bind:checkedbooleanfalseChecked (on) state. Two-way bindable.
valuestringValue submitted when on and a name is given.
namestringForm field name. null is not accepted.
requiredbooleanRequire the switch to be on for form submission. Defaults to the enclosing field's required.
stateFieldStateContextual state. Defaults to the enclosing field's state.

Plus everything in Omit<HTMLButtonAttributes, 'children' | 'value'>, spread onto the element.