Switch
Turns a setting on or off. The change applies right away.
When to use
- A setting that applies the moment it changes: notifications, single sign-on, a source's sync.
When not to use
- An option inside a form that's saved with a button: use Checkbox.
- A pressed state on a toolbar button, such as a filter: use Toggle.
- A choice between two named options, such as English and French: use Radio Group or Toggle Group.
Install
npx shadcn@latest add @clinia/switchAnatomy
- 1Track
data-slot="switch"bg-input · data-checked:bg-primary - 2Thumb
data-slot="switch-thumb"bg-background · shadow-sm - 3Label
data-slot="label"text-sm · font-medium
Clinia rules
- Off is an
inputtrack (3:1) and on is aprimarytrack. Trust never fills the track. - The label names the setting (
Email notifications), not the state or the action (On,Turn on). size="sm"(16px tall) is for dense settings lists and table rows. Usedefaulteverywhere else.
Do and don't
States
off
on
focus
invalid
disabled
disabled · on
size sm
Focus cells apply the component's focus-visible classes statically. Toggle the site theme to review both themes.
Accessibility
Built in
- Focus-visible shows a solid Trust ring (
border-ring ring-3 ring-ring/30);aria-invalidswaps it to a destructive ring. - Base UI renders a
spanwithrole="switch"andaria-checked, plus a hidden native checkbox so the value submits with the form. In tests, find it withgetByRole("switch").
Your responsibility
- Pair it with a
Label(htmlFor= the switchid), or put both in a horizontalField. - If the change can fail, say so right away and put the switch back. Don't leave it showing a state that didn't save.
How to test
- Press Tab to reach the switch: the Trust ring appears.
- Press Space: it toggles and the setting applies with no Save button.
- Click the label: it toggles too.
- With a screen reader, it's announced as its label, then switch, then on or off.
API
| Prop | Values | Default | Description |
|---|---|---|---|
| size | smdefault | default |
|
All other props come from Base UI Switch.