Switch
On/off control that applies immediately. Two values (Off, On), two sizes and five states. Includes a label and optional supporting text.
Caption text
Usage
Import the component and render it.
import { Switch } from 'src/components/Switch/Switch';
<Switch size="md" label="Switch label" caption="Caption text" />Accessibility
Role: switch (Radix Switch: <button role="switch"> with aria-checked).
| Key | Action |
|---|---|
| Space | Toggles between on and off |
| Enter | Toggles between on and off |
| Tab | Moves focus (shows the Focus state via :focus-visible) |
- Use a Switch for settings that apply immediately; use a Checkbox when the choice is submitted with a form.
- The label is a <label for> the switch: clicking it toggles the value.
- The caption is linked with aria-describedby and is not part of the accessible name.
- Hover/Pressed states also apply when pointing at the label, not only the track.
Size
Set the size prop: Small is "sm", Medium is "md".
Caption text
Caption text
<Switch size="sm" label="Switch label" />
<Switch size="md" label="Switch label" />Toggled
Set the checked prop: Off is {false}, On is {true}.
Caption text
Caption text
<Switch label="Switch label" />
<Switch checked label="Switch label" />States
Disabled comes from the disabled prop. Hover, Focus and Pressed follow the pointer and the keyboard: try them in the playground.
Caption text
Caption text
<Switch disabled label="Switch label" />Used in
Switch is part of ListItem.
Actions
Pass callbacks to respond to the component.
import { Switch } from 'src/components/Switch/Switch';
<Switch
size="md"
label="Switch label"
caption="Caption text"
onCheckedChange={(checked) => console.log(checked)}
/>| Callback | When |
|---|---|
onCheckedChange | Fires when the value changes. |
