shadcn.io is not affiliated with official shadcn/ui
Switch examples · Cards
Switch in Item for React
An outlined item with a title, a description and a switch that starts on.
A Switch in an outlined Item, with Auto-save as the title and a line of description. Item gives the row its padding, border and text styles, so a settings list keeps one look without custom layout.
Preview
Installation
FAQ
Which Item parts wrap the copy?
Item sets variant="outline" and bg-background. ItemContent holds ItemTitle (Auto-save) and ItemDescription, and ItemActions holds the Switch.
Does the title toggle the switch?
No. ItemTitle is text, not a label, so the switch has aria-label (Auto-save). When clicking the title should toggle it, use a Field with a FieldLabel as in Switch with Description.
How do I control this switch in React?
The switch starts on through its default prop, which only sets the starting state; after that it keeps its own state. To control it, keep the on or off state in your own state and pass it back.
The prop is defaultChecked.
Pass checked; onCheckedChange receives the new boolean.
Does this switch submit with a form?
Give it a name. When on, it submits its value, which defaults to on. When off, it submits nothing.
With Base UI, uncheckedValue sets what it submits when off.
Related
- Settings Card SwitchA bordered settings card with a bell, a
- Feature Card SwitchA bordered card with an icon, a label, a
- Settings List SwitchTwo settings rows, each with a label, a
- Basic ItemAn outline item with a title, a one-line
- Icon with Label SwitchA switch with a bell icon and a
- Theme Toggle with Icons SwitchA switch between a sun and a moon icon