shadcn.io is not affiliated with official shadcn/ui
Switch examples · Square
Square Switch with Label for React
A square switch that starts on, with a label that toggles it.
A square Switch next to a label, Enable notifications, in a horizontal field. Clicking the text toggles the switch, and screen readers read the label with it. It starts on.
Preview
Installation
FAQ
How are the corners squared off?
The Switch sets rounded-sm for the track and **:data-[slot=switch-thumb]:rounded for the thumb. The thumb is the part with data-slot="switch-thumb", so the selector reaches it on every library.
Why does the text toggle the switch?
The Switch and a FieldLabel sit in a horizontal Field with w-fit.
The Switch has an id, and the FieldLabel points at it with htmlFor.
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.
How do I make this switch smaller?
Set size="sm". The default size is default. For a different track or thumb, edit those classes in the Switch file in your ui folder.
Related
- Square SwitchA switch with square corners on its track
- Square Switch with TextA square switch with On and Off text that
- Square Theme Toggle SwitchA compact square switch between a moon
- Settings Card SwitchA bordered settings card with a bell, a
- Simple CheckboxA checkbox with a label for a single
- Default SwitchA switch in its default off state, with