Switch
Turns a setting on or off, with an effect that applies as soon as it is flipped.
Installation
pnpm dlx shadcn@latest add @silkware/switchTo copy the component by hand, install its dependencies:
pnpm add react-aria-components cnThen copy the source into components/ui/switch.tsx:
"use client"
import { cn } from "cn"import { composeRenderProps, Switch as SwitchPrimitive, type SwitchProps as SwitchPrimitiveProps,} from "@animate-ui/react/primitives/react-aria"
function Switch({ className, size = "default", children, ...props}: SwitchPrimitiveProps & { size?: "sm" | "default"}) { return ( <SwitchPrimitive data-slot="switch" data-size={size} className={cn( "peer group/switch relative inline-flex shrink-0 items-center rounded-full border border-transparent shadow-xs transition-all outline-none not-data-selected:bg-input group-has-[:focus-visible]/field-label:border-transparent group-has-[:focus-visible]/field-label:ring-0 after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 data-focus-visible:border-ring data-focus-visible:ring-3 data-focus-visible:ring-ring/50 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/20 data-[size=default]:h-[18.4px] data-[size=default]:w-[32px] data-[size=sm]:h-[14px] data-[size=sm]:w-[24px] dark:not-data-selected:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 dark:data-invalid:border-destructive/50 dark:data-invalid:ring-destructive/40 data-checked:bg-primary data-unchecked:bg-input dark:data-unchecked:bg-input/80 data-selected:bg-primary data-disabled:cursor-not-allowed data-disabled:opacity-50", className )} {...props} > {composeRenderProps(children, (children, { isSelected }) => ( <> <span data-slot="switch-thumb" data-selected={isSelected || undefined} className="pointer-events-none block rounded-full bg-background ring-0 group-data-[size=default]/switch:size-4 group-data-[size=default]/switch:not-data-selected:translate-x-0 group-data-[size=sm]/switch:size-3 group-data-[size=sm]/switch:not-data-selected:translate-x-0 dark:not-data-selected:bg-foreground group-data-[size=default]/switch:data-checked:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-checked:translate-x-[calc(100%-2px)] dark:data-checked:bg-primary-foreground group-data-[size=default]/switch:data-unchecked:translate-x-0 group-data-[size=sm]/switch:data-unchecked:translate-x-0 dark:data-unchecked:bg-foreground group-data-[size=default]/switch:data-selected:translate-x-[calc(100%-2px)] group-data-[size=sm]/switch:data-selected:translate-x-[calc(100%-2px)] dark:data-selected:bg-primary-foreground" /> {children} </> ))} </SwitchPrimitive> )}
export { Switch }Usage
import { Field, FieldLabel } from "@/components/ui/field"import { Switch } from "@/components/ui/switch"<Field orientation="horizontal"> <Switch id="notify" name="notify" /> <FieldLabel htmlFor="notify">Notify me when an order ships</FieldLabel></Field>The switch draws only the track and the thumb. Place a FieldLabel beside it and match htmlFor to the id. Use isSelected and onChange for state, or defaultSelected for an initial value. onChange receives a boolean.
Examples
With a description
Wrap the label and a description in FieldContent and put the switch last, so the switches of a list line up at the end.
Email the buyer when a supplier misses the promised date.
Choice card
Wrap a Field in a FieldLabel to make the whole card the target.
Hear about urgent orders on Saturday and Sunday.
Controlled
Hold the state yourself, and react to it as soon as it changes.
New orders go straight to the supplier.
Size
sm and default set the size of the track. Use the small one in dense lists, and keep one size per group.
Disabled
isDisabled removes the switch from the tab order and ignores presses. Put data-disabled on the Field so the label dims too. Say why the setting is locked in the description.
Read-only
isReadOnly keeps the switch focusable and announced, but the value cannot change. Use it for a setting that someone else controls and that people still need to read.
Set by your administrator.
Settings list
A list of settings, each with a label, a description and a switch. Each switch applies the moment it is flipped, so the list has no save button.
Accessibility
| Key | Action |
|---|---|
| Space | Turns the focused switch on or off. |
| Tab | Moves focus to the next control. A disabled switch is skipped. |
| ShiftTab | Moves focus to the previous control. |
React Aria provides:
- A native checkbox input with
role="switch", which screen readers announce as on or off. - Press behaviour that is the same for mouse, touch and keyboard.
- A focus ring for keyboard focus only.
isDisabled,isReadOnlyandisRequiredstates.
You provide:
- A visible label that matches the
id, or anaria-label. - A label that names the setting, not the state: "Late delivery alerts", not "On". The switch says whether it is on.
- A description for what the switch changes, when the label alone is not enough.
The installed switch has no isInvalid state. When a choice must be confirmed before a form is sent, use a Checkbox.
Guidelines
When to use
- For a setting that is on or off and takes effect at once: notifications, a view option, a feature flag.
- In a settings page, where each row is one setting.
When not to use
- For a choice that is saved with a form and needs a button. Use Checkbox.
- For one option from several. Use Radio Group.
- For an action, such as sending or approving. Use Button.
- For a tool that stays pressed, such as bold in an editor. Use Toggle.
Content
- Label the setting with a noun phrase: "Weekend alerts". Avoid verbs that sound like actions.
- Do not write "Enable" or "Turn on" in the label. The switch already says it.
- Describe the effect in one sentence when it is not obvious.
- Keep the wording the same when the switch is on and when it is off.
Timing
A switch applies at once. If the change waits for a save, the switch lies about its state.
Use a switch for a setting that applies as soon as it is flipped.
Don’t use a switch for a choice that waits for a button. Use a checkbox.
API reference
Switch
Renders the React Aria Switch as a track and a thumb. It accepts every React Aria Switch prop and the prop below. It does not render its label.
| Prop | Type | Default | Description |
|---|---|---|---|
size? | "sm" | "default" | "default" | The size of the track and the thumb. |
isSelected? | boolean | None | Whether the switch is on, for a controlled switch. |
defaultSelected? | boolean | None | Whether the switch starts on, for an uncontrolled switch. |
onChange? | (isSelected: boolean) => void | None | Called when the switch is flipped. |
isDisabled? | boolean | false | Removes the switch from the tab order and ignores presses. |
isReadOnly? | boolean | false | Keeps the switch focusable but stops it from changing. |
name? | string | None | The name used when the form is submitted. |
value? | string | None | The value submitted when the switch is on. |
className? | string | None | Classes merged after the base classes. |