Skip to content
shadcn.io

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.

Framework
UI library

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

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

What do screen readers call this switch?

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

Which keys toggle the switch?

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