shadcn.io is not affiliated with official shadcn/ui
Switch examples · Labeled
Switch with On Label for React
A switch that starts on, with the word On beside it.
A Switch that starts on with the word On after it, for a setting whose enabled state should be spelled out. The word is plain text: it reads On whatever the switch does, so keep it for read-mostly settings or wire it to the state.
Preview
Installation
FAQ
Does the On text change when the switch turns off?
No. The span always reads On, with font-medium text-sm, in a flex items-center gap-2 row. The switch starts on through its default prop. See Switch with State Labels for words that follow the state.
How do I control this switch in React?
To change that word with the switch, keep the on or off state in your own state, render the word from it, and pass the state back to the Switch.
Pass checked; onCheckedChange receives the new boolean.
What do screen readers call this switch?
It has no visible label, so the demo passes aria-label (Notifications). Change it to the setting the switch controls, or use a visible FieldLabel instead.
How do I add a visible label to this switch?
Put the Switch in a horizontal Field next to a FieldLabel. Clicking the label toggles the switch, and screen readers read the label with it.
Give the Switch an id and set the same value as htmlFor on the FieldLabel.
Related
- Switch with State LabelsA switch with On and Off text on both
- Switch with LabelA switch with a label, Airplane Mode
- Switch with DescriptionA switch with a label and a line of
- Default SwitchA switch in its default off state, with
- Feature Card SwitchA bordered card with an icon, a label, a
- Switch in ItemAn outlined item with a title, a