shadcn.io is not affiliated with official shadcn/ui
Switch examples · Standard
Small Switch for React
A small switch for dense layouts such as tables and toolbars.
The Switch at its small size, for dense rows where the default track feels large: table cells, toolbars and compact settings panels. The size is a prop, so the track and the thumb shrink together.
Preview
Installation
FAQ
How is this switch made smaller?
It sets size="sm", the built-in small size. The default size is default. Both sizes keep the thumb in proportion with the track.
Can I pick my own track size?
size sets data-size on the Switch, and the track and thumb sizes for each value are classes in the Switch file in your ui folder. Edit them there, or add another size, rather than overriding the height and width on one switch.
Is the small switch big enough to tap?
The track is small, but the Switch adds an invisible hit area around it with an after: pseudo-element, so the target is larger than what you see. Prefer the default size on touch-first screens.
How do I control this switch in React?
Nothing in this file sets the on state, so the switch starts off and keeps its own state. To control it, keep the on or off state in your own state and pass it back. To start it on, use the default prop.
The prop is defaultChecked.
Pass checked; onCheckedChange receives the new boolean.
Related
- Default SwitchA switch in its default off state, with
- Checked SwitchA switch that starts on and keeps its own
- Disabled SwitchA disabled switch that shows its state
- Square Theme Toggle SwitchA compact square switch between a moon
- Simple CheckboxA checkbox with a label for a single
- Feature Card SwitchA bordered card with an icon, a label, a