Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Switch examples · Standard

Checked Switch for React

A switch that starts on and keeps its own state after that.

Framework
UI library

A Switch that starts on, for settings that are enabled by default: a remembered preference, or an opt-out feature. The default prop sets only the first state, so no state of your own is needed until something has to read the value.

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 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.

What do screen readers call this switch?

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

How do I stop people from toggling it?

Disable it. The switch then matches data-disabled, which fades it to half opacity and sets cursor-not-allowed. Pass disabled.

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.