Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Switch examples · Cards

Settings List Switch for React

Two settings rows, each with a label, a description and a switch, in a divided list.

Framework
UI library

A bordered list of settings separated by lines, each row with a label, a description and a Switch: Marketing emails off, Security emails on. Use it for a group of related settings without a card around each one.

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 two settings stacked?

The wrapper is flex flex-col divide-y rounded-lg border bg-background. Each row is a horizontal Field with p-4, holding a FieldContent and a Switch.

Which switch starts on?

Marketing emails starts off. Security emails starts on through its default prop. The prop is defaultChecked.

How is each label tied to its switch?

Each row is its own Field, so each label belongs to its own switch. The Switch has an id, and the FieldLabel points at it with htmlFor.

How do I control this switch in React?

Each switch keeps its own state here. To control them, keep each switch's on or off state in your own state, such as one object with a key per setting, and pass it back. 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.

Which keys toggle the switch?

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