Switch
An on/off setting that takes effect immediately, with a loading state.
Suggest three questions after each answer.
Show the knowledge sources behind each answer.
Let users dictate messages with their microphone.
Usage guidelines
- Use
Switchfor an on/off setting that takes effect immediately. UseCheckboxfor a boolean value saved with its form, andTogglefor the pressed state of a button. - Label the switch with a visible
FieldLabelthat wraps it. Usearia-labelonly when no visible label exists. - When the change is saved remotely, control
checkedand setloadingwhile the request is pending.loadingblocks interaction, setsaria-busyand shows a spinner atmdandlgsizes; updatecheckedonce the save succeeds. - While the saved value itself is still loading, render
SwitchSkeletonat the samesizeinstead of a switch with a guessed value. - Use
disabledfor a setting the user cannot change, and say why inFieldDescription. Switchtakes eithercheckedordefaultChecked, not both.
Anatomy
Switch renders its own thumb and loading spinner. SwitchSkeleton is a standalone placeholder with the same sizes.
Examples
Sizes
xs, sm, md (the default) and lg match the density of the surrounding row.
Loading
The switch keeps its current value and ignores further presses while the change is saved. It moves only when the save succeeds; a failed save leaves it in place and shows the error. The first attempt in this demo fails.
Anyone with the link can chat with this app.
Skeleton
Render SwitchSkeleton at the matching size while the saved preference is still loading, rather than guessing an initial value.
Show the knowledge sources behind each answer.
Loading saved setting…
Disabled
Set disabled on Field to disable the switch and its label together.
Only workspace owners can change this setting.
Available on the Team plan.
Keyboard
| Key | Action |
|---|---|
| Space | Toggles the focused switch. |
| Enter | Toggles the focused switch. |
| Tab | Moves focus to the next control. A disabled switch is skipped. |
API reference
Switch
A toggle switch control supporting controlled, uncontrolled, and loading states. Renders a <span> element and a hidden <input> beside.
Pair with a visible FieldLabel or <label>; setting loading disables interaction, sets aria-busy, and shows a spinner at md and lg sizes.
| Prop | Type | Default | Details |
|---|---|---|---|
size | 'lg' | 'md' | 'sm' | 'xs' | 'md' | |
defaultChecked | boolean | false | |
checked | boolean | No default | |
loading | boolean | false | |
className | string | ((state: SwitchRootState) => string | undefined) | No default | |
render | ComponentRenderFn<HTMLProps, SwitchRootState> | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | No default | |
style | React.CSSProperties | ((state: SwitchRootState) => React.CSSProperties | undefined) | No default | |
nativeButton | boolean | false | |
id | string | No default | |
disabled | boolean | false | |
inputRef | React.Ref<HTMLInputElement> | No default | |
name | string | No default | |
form | string | No default | |
onCheckedChange | ((checked: boolean, eventDetails: BaseSwitch.Root.ChangeEventDetails) => void) | No default | |
readOnly | boolean | false | |
required | boolean | false | |
value | string | No default | |
uncheckedValue | string | No default | |
Valid prop combinations:
defaultChecked?: boolean,checked?: undefinedchecked: boolean,defaultChecked?: undefined
| Attribute | Description |
|---|---|
data-checked | Present when the switch is checked. |
data-unchecked | Present when the switch is not checked. |
data-disabled | Present when the switch is disabled. |
data-readonly | Present when the switch is readonly. |
data-required | Present when the switch is required. |
data-valid | Present when the switch is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the switch is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the switch has been touched (when wrapped in Field.Root). |
data-dirty | Present when the switch's value has changed (when wrapped in Field.Root). |
data-filled | Present when the switch is active (when wrapped in Field.Root). |
data-focused | Present when the switch is focused (when wrapped in Field.Root). |
SwitchSkeleton
| Prop | Type | Default | Details |
|---|---|---|---|
size | 'lg' | 'md' | 'sm' | 'xs' | 'md' | |