Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Switch examples · Icons

Icon with Label Switch for React

A switch with a bell icon and a Notifications label, starting on.

Framework
UI library

A settings row with a bell icon, a label and a Switch that starts on. The icon helps people scan a long list of settings; the label names the setting and toggles the switch when clicked.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How are the bell, the label and the switch arranged?

The row is flex items-center gap-3, and BellIcon is size-4 text-muted-foreground with aria-hidden. The label and the switch sit in a horizontal Field, where the label grows and pushes the switch to the end.

Why does Notifications toggle the switch?

The FieldLabel and the Switch share a Field. 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.

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.

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.

Which keys toggle the switch?

Tab moves focus to the switch. Space toggles it, and so does Enter.