Forms
Switch
A setting that takes effect immediately.
Settings
A role="switch" button for a setting that takes effect the moment it is flipped. Add label and description for a settings row; labelPosition="start" pushes the switch to the far side.
Get an email when a deployment succeeds or fails.
A summary of activity every Monday at 9 am.
Product news and offers, at most once a month.
When to use
- On/off settings that apply immediately, with no Save button.
- Enabling or disabling a feature, integration or preference.
Not for: a choice that is submitted later with a form (use a checkbox), or more than two states (use a radio group or a segmented control).
Sizes
Controlled and disabled
Pass name and the switch submits value (default "on") through a hidden input while it is on, like a checkbox.
Drafts are saved every 30 seconds.
Managed by your administrator.
API
Switch
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | — | Controlled state. |
| defaultChecked | boolean | false | Initial state when uncontrolled. |
| onCheckedChange | (checked: boolean) => void | — | Called when toggled. |
| size | 'sm' | 'md' | 'md' | Track 32 × 18 or 40 × 24px. |
| name / value | string | value: 'on' | Form field; a hidden input is rendered while on. |
| label | ReactNode | — | Visible label (renders a row). Without one, pass aria-label. |
| description | ReactNode | — | Secondary text; wired with aria-describedby. |
| labelPosition | 'start' | 'end' | 'end' | Label before or after the switch. |
| fieldClassName | string | — | Classes for the row; className goes to the button. |
| ...props | ButtonHTMLAttributes | — | Forwarded to the <button>, including ref, id, disabled. |
Keyboard
| Key | Action |
|---|---|
| Space | Toggles the switch. |
| Enter | Toggles the switch. |
| Tab | Moves focus to the next control. |
Accessibility
- WAI-ARIA switch pattern: a button with role="switch" and aria-checked="true|false".
- The label is a real <label for> so clicking the text toggles it; with no label, give the switch an aria-label.
- The thumb is decorative (aria-hidden). Keyboard focus shows a 3px ring; on touch screens the tap area grows to 44px.
Motion
- The thumb slides on the smooth spring, which never overshoots, so the switch never appears to flicker past its state.
- While pressed, the thumb widens slightly and keeps its edge against the track.
- Reduced motion: the thumb jumps; the track colour still changes.