Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Switch examples · Standard

Default Switch for React

A switch in its default off state, with no label.

Framework
UI library

The plain Switch: a track and a thumb, off until someone turns it on. Start here for any on or off setting that takes effect at once, then add a label, a description or a card around it as the other examples do.

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

What does this switch render?

One Switch with only an aria-label. It starts off and keeps its own state, so nothing else in the file runs.

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.

What do screen readers call this switch?

It has no visible label, so the demo passes aria-label (Airplane mode). Change it to the setting the switch controls, or use a visible FieldLabel instead.

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.

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.

Which keys toggle the switch?

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