Skip to content

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.

hero.tsx

Usage guidelines

  • Use Switch for an on/off setting that takes effect immediately. Use Checkbox for a boolean value saved with its form, and Toggle for the pressed state of a button.
  • Label the switch with a visible FieldLabel that wraps it. Use aria-label only when no visible label exists.
  • When the change is saved remotely, control checked and set loading while the request is pending. loading blocks interaction, sets aria-busy and shows a spinner at md and lg sizes; update checked once the save succeeds.
  • While the saved value itself is still loading, render SwitchSkeleton at the same size instead of a switch with a guessed value.
  • Use disabled for a setting the user cannot change, and say why in FieldDescription.
  • Switch takes either checked or defaultChecked, not both.

Anatomy

tsx
tsx

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.

sizes.tsx

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.

loading.tsx

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.

Speech to text

Loading saved setting…

skeleton.tsx

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.

disabled.tsx

Keyboard

KeyAction
SpaceToggles the focused switch.
EnterToggles the focused switch.
TabMoves 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.

Base UI Switch.Root docs

Props
PropTypeDefaultDetails
size'lg' | 'md' | 'sm' | 'xs''md'
defaultCheckedbooleanfalse
checkedbooleanNo default
loadingbooleanfalse
Base UI Switch.Root docs

Valid prop combinations:

  • defaultChecked?: boolean, checked?: undefined
  • checked: boolean, defaultChecked?: undefined
Data attributes
AttributeDescription
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

Props
PropTypeDefaultDetails
size'lg' | 'md' | 'sm' | 'xs''md'