Switch
Turns a setting on or off immediately.
switch-basic-demo.tsx
Installation
The CLI adds the files, packages, theme variables, and Tailwind plugin that this component uses. Before you run it, set up your project.
npm
yarn
pnpm
bun
Composition
SwitchFieldOwns the on and off state and validation.
SwitchButtonIts text becomes the accessible name.
SwitchLabelSwitchControlPut it before SwitchLabel to show the control first.
SwitchThumb
SwitchDescriptionSwitchError
Switch Renders all of these parts from children, description, and errorMessage. Use the parts only for a custom layout.
Description
Others can find your profile.
switch-description-demo.tsx
Disabled
switch-disabled-demo.tsx
Control first
Put SwitchControl before SwitchLabel.
Save changes as you type.
switch-control-first-demo.tsx
Custom layout
Keep SwitchDescription outside SwitchButton so it is read as a description. Also install Badge.
Others can find your profile.
Show recent work on your profile.
switch-demo.tsx
API reference
vip/ui adds or changes the props below. For all other props, see React Aria Switch (opens in a new tab).
- children: ReactNode
- The label. Without it, pass aria-label so the switch has a name.
- description: ReactNode
- Help text below the label. Linked with aria-describedby, so it is not part of the name.
- errorMessage: string | ((validation: ValidationResult) => string)
- Shown below the label when isRequired, validate, or isInvalid makes the switch invalid. Without it, the browser validation message shows.
- checked: boolean
- Alias for isSelected.
- defaultChecked: boolean
- Alias for defaultSelected.
- onCheckedChange: (checked: boolean) => void
- Alias for onChange. Both run when both are set.