shadcn.io is an independent project built on shadcn/ui
On this page9 sections
A switch turns one setting on or off, and the change applies the moment it's flipped, with no save button under the row. It reads like a light switch, which makes it the control for a settings list: airplane mode, email alerts, sharing across devices.
When the choice waits for a form to be submitted, or sits in a list where several can be picked, use a Checkbox. For a button that stays pressed, like bold in a text toolbar, use a Toggle.
Installation
Anatomy

Usage
import { Field, FieldLabel } from "@/components/ui/field";
import { Switch } from "@/components/ui/switch";<Field className="w-fit" orientation="horizontal">
<Switch />
<FieldLabel>Airplane Mode</FieldLabel>
</Field>Inside one Field, FieldLabel is wired to the switch for you: clicking the words flips it, and a screen reader reads them as its name.
Accessibility
The switch is a <button> with the switch role and aria-checked, so a screen reader announces it as a switch that is on or off, and a hidden checkbox beside it carries the value for the form. Inside a Field, it takes its name from FieldLabel. Name it with aria-label where no visible label fits.
- Space
- Toggles the switch.
- Enter
- Submits the form the switch is in, if there is one, and never toggles it.
Demos
Description
A Share across devices setting with a line under its label saying that focus is shared between devices and turns off when you leave the app. The label and description sit in FieldContent on one side of the row and the switch on the other. Add the line when the label alone leaves out a consequence, and skip it when the label already says everything.
Settings list
Marketing emails and Security emails as two rows of one bordered list, each with a label, a line of description and its switch, and security emails on to start. Every row is its own Field, and the list draws a rule between them. Reach for it on a notifications or privacy page, where each setting applies as soon as it's flipped.
Choice card
Two settings drawn as cards, Share across devices and Enable notifications, each with a title, a description and its switch, with notifications on to start. A FieldLabel wraps each card's Field, so a click anywhere on the card flips its switch. Use cards when every setting needs a sentence of explanation, and a plain settings list when cards would only add bulk.
Disabled
A switch labeled Disabled that can't be flipped. Switch gets disabled and its Field gets data-disabled, so the switch fades and ignores clicks. Use it when the plan or role can't change the setting, and say why next to it, since off and disabled look alike at a glance.
Invalid
An Accept terms and conditions switch with a description saying the terms must be accepted to continue. The demo sets invalid on Field and aria-invalid on Switch, which turns the label red and gives the track a red outline. Use it for a switch that blocks the next step, never for a preference that happens to be off.
Sizes
Two switches, Small and Default, each beside its label. size takes sm or default, and leaving it out gives the default. The small one fits a dense settings table or a row of secondary text. There is no larger size, so a bigger switch means editing the size classes in components/ui/switch.
RTL
Share across devices in Arabic, with dir set to rtl on both Field and Switch. The label and description move to the right and the switch to the left, and the thumb slides from right to left when it turns on. Set the direction on the nearest wrapper when the rest of the page reads left to right.
FAQ
When should I use a switch instead of a checkbox?
Use a switch for a setting that takes effect the moment it changes, like airplane mode or email alerts. Use a Checkbox when the choice waits for a submit button, such as agreeing to terms, or when it's one of several options in a list.
A switch in a form that only saves on submit tells people the change already happened when it hasn't. For a pressed or unpressed button in a toolbar, like bold, use a Toggle.
How do I control a Switch in React?
Pass checked and update it in onCheckedChange, which receives the new boolean first and details second. Calling details.cancel() there keeps the switch as it was. Use defaultChecked instead when the switch can keep its own state, as the notifications card in the Choice card demo does.
Control it when the value lives somewhere else, such as a user's saved preferences, so the switch always shows what was last saved.
How do I save a Switch setting to the server as soon as it changes?
Set your state in onCheckedChange right away, so the switch moves under the finger, then send the request. If the request fails, set the state back and tell the person, in a toast or next to the row.
When a second flip could land before the first request finishes and the two could conflict, disable the switch until the response arrives. Otherwise leave it enabled, so a fast correction isn't blocked by the network.
How do I add a label to a Switch?
Put Switch and a FieldLabel in one Field, as the demos do, and the field connects the two: clicking the words flips the switch, and a screen reader reads them as its name. The demos' id and htmlFor pair isn't needed for that. Where no visible label fits, set aria-label on the switch.
Add a FieldDescription under the label when the setting needs a sentence, as the Description demo does.
Does the UI SDK Switch submit its value in a form?
Yes, once it has a name. A switch that is on sends that name with its value, which is on unless you set another, and a switch that is off sends nothing, the way a checkbox does.
Set form to a form's id to join a form elsewhere on the page, and required to block submitting until the switch is on. If the setting only takes effect when the form is submitted, though, a checkbox describes that better.
How do I make a smaller Switch?
Set size to sm. The other value is default, which is also what you get without the prop, and the Sizes demo shows the two side by side.
Both sizes are classes keyed on data-size in components/ui/switch: a height and width for the track, and a size and a slide distance for the thumb. A third size is one more value in the type of size, with its own set of those classes.
How do I show an error on a Switch?
Set invalid on the Field around it. The field adds aria-invalid to the switch, which a screen reader announces and the styles draw as a red outline, and the field's label turns red.
Put the reason in the description under the label, since red on its own doesn't say what to do.
API Reference
Switch
<button> data-slot="switch"
checked?boolean
Whether it's checked. Pass it to control the checkbox.
defaultChecked?boolean
Whether it's checked at first, when uncontrolled. A form reset returns to it.
- Default
false
onCheckedChange?(checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) → void
Runs when a user or a form reset wants to change it. details.reason says why, and details.cancel() stops it.
- Type
(checked: boolean, details: ChangeDetails<CheckboxCheckedChangeReason>) → void- Parameters
checked– booleandetails– ChangeDetails<CheckboxCheckedChangeReason>- Returns
void
controller?{ toggle: (details?: Details) → boolean; getChecked: () → boolean; setChecked: (checked: boolean, details?: Details) → boolean; focus: () → void; }
Toggles it from anywhere. Create one with createSwitchController().
- Type
{ toggle: (details?: Details) → boolean; getChecked: () → boolean; setChecked: (checked: boolean, details?: Details) → boolean; focus: () → void; }
debug?boolean
Logs every state change with its reason to the console, in development builds.
disabled?boolean
Disables it like a native control: it can't be focused or changed, and isn't submitted.
form?string
The id of a form to belong to, when it isn't inside that form.
name?string
The name it submits under. Without one, it submits nothing.
readOnly?boolean
Keeps it focusable and submitted, but stops changes.
required?boolean
Blocks form submission until it's checked, with the browser's own message.
size?"sm" | "default"
The size.
- Type
"sm" | "default"- Default
"default"
value?string
What it submits while checked.
- Default
"on"
render?ReactElement | ((props: object, state: object) => ReactElement)
Replace the host element or compose it with another component.
- Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string
Merged with the part's classes.
data-checked?string
Present while on.
data-disabled?string
Present when disabled, by its own prop or a <fieldset>.
data-invalid?string
Present while a Field around it shows as invalid. The thumb doesn't get it.
data-readonly?string
Present when read-only.
data-required?string
Present when required.
data-size?string
Mirrors size.
data-unchecked?string
Present while off.











