When to use
- Apply a setting at once, with no Save button, such as a notification preference.
- Get a complete settings row from one component: label and description on the left, switch on the right edge.
- Line up several settings so every switch sits on the same right edge.
When not to use
- Options that only take effect when a form is submitted: use a Checkbox.
- Picking one of several modes, such as light, dark or system: use Segmented or a Radio Group.
- Toggling formatting in a toolbar: use a Toggle Group.
Installation
pnpm dlx shadcn add https://eased.dev/r/switch.jsonnpx shadcn add https://eased.dev/r/switch.jsonyarn dlx shadcn add https://eased.dev/r/switch.jsonbunx --bun shadcn add https://eased.dev/r/switch.jsonThe command adds Switch, everything it builds on and Eased’s tokens to your project.
Install the dependencies
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/reactAdd the Eased pieces it builds on
Copy each item’s files the same way, or install them with the CLI:
Copy the source into your project
components/eased/switch/switch.tsxTSX "use client";import { Switch as BaseSwitch } from "@base-ui/react/switch";import { useId, type ReactNode } from "react";export type SwitchProps = { /** On (controlled). */ checked?: boolean; /** Initially on (uncontrolled). */ defaultChecked?: boolean; onCheckedChange?: (checked: boolean) => void; /** What a form submits under `name` while on. */ value?: string; name?: string; disabled?: boolean; required?: boolean; readOnly?: boolean; /** * The label. With one, Switch renders a settings row: label (and description) on the left, * the switch on the right, and the whole row toggles it. Leave it out for a bare switch, and * pass `aria-label`. */ children?: ReactNode; /** Muted text under the label. */ description?: ReactNode; "aria-label"?: string; id?: string; className?: string;};/* * The thumb is a 20px box clipped to a 16px circle. At rest the clip hides the 4px on the * side it travels toward; pressing opens the clip, so the thumb stretches in the direction it * is about to move, then the box translates 12px while the clip closes behind it. Visually the * circle travels 16px. Only transform and clip-path animate, on the snappy spring. */const THUMB_CLIP = "[clip-path:inset(0_4px_0_0_round_8px)] group-data-checked:[clip-path:inset(0_0_0_4px_round_8px)] group-active:[clip-path:inset(0_0_0_0_round_8px)] group-data-disabled:group-active:[clip-path:inset(0_4px_0_0_round_8px)] group-data-disabled:group-data-checked:group-active:[clip-path:inset(0_0_0_4px_round_8px)] group-data-readonly:group-active:[clip-path:inset(0_4px_0_0_round_8px)] group-data-readonly:group-data-checked:group-active:[clip-path:inset(0_0_0_4px_round_8px)]";const SPRING = "[transition:translate_var(--eased-duration-spring-snappy)_var(--eased-spring-snappy),clip-path_var(--eased-duration-spring-snappy)_var(--eased-spring-snappy),background-color_150ms_var(--eased-ease-standard)] reduced:[transition:background-color_100ms_linear]";function SwitchTrack() { return ( <> {/* Drop shadows on the unclipped wrapper, so the clip does not cut them off. Tinted from the shadow token. */} <span aria-hidden className={`pointer-events-none absolute top-0.5 left-0.5 h-4 w-5 translate-x-0 group-data-checked:translate-x-3 [filter:drop-shadow(0_1px_1.5px_oklch(var(--eased-shadow-tint)/0.2))_drop-shadow(0_0_0.5px_oklch(var(--eased-shadow-tint)/0.18))] ${SPRING}`} > <BaseSwitch.Thumb className={`block size-full group-data-unchecked:bg-surface-raised dark:group-data-unchecked:bg-text-secondary group-data-checked:bg-text-on-brand ${THUMB_CLIP} ${SPRING}`} /> </span> </> );}const TRACK = "group relative inline-flex h-5 w-9 shrink-0 rounded-pill inset-shadow-well transition-colors duration-150 ease-standard focus-ring data-unchecked:bg-surface-active data-checked:bg-brand hover-fine:data-unchecked:not-data-disabled:bg-line hover-fine:data-checked:not-data-disabled:bg-brand-hover data-disabled:cursor-not-allowed data-disabled:opacity-50";/** * A switch on Base UI: Space and Enter toggle it, forms submit it, Field validates it. The * thumb springs across and, while pressed, stretches toward where it is going. */export function Switch({ children, description, className = "", ...props }: SwitchProps) { const id = useId(); if (children == null) { return ( <BaseSwitch.Root {...props} className={(s) => `${TRACK} ${s.readOnly ? "cursor-default" : "cursor-pointer"} ${className}`}> <SwitchTrack /> </BaseSwitch.Root> ); } return ( <div className={`grid min-w-0 grid-cols-[minmax(0,1fr)_auto] items-start gap-x-4 ${className}`}> <span className="flex min-w-0 flex-col gap-0.5 py-px"> <label htmlFor={`${id}-switch`} id={`${id}-label`} className={`text-ui ${props.disabled ? "cursor-not-allowed text-text-disabled" : "cursor-pointer text-text"}`} > {children} </label> {description && ( <span id={`${id}-desc`} className="text-ui-sm text-text-muted text-pretty"> {description} </span> )} </span> {/* 20px switch on the label's 20px first line (rule 107). */} <BaseSwitch.Root {...props} id={props.id ?? `${id}-switch`} aria-labelledby={`${id}-label`} aria-describedby={description ? `${id}-desc` : undefined} className={(s) => `${TRACK} mt-px ${s.readOnly ? "cursor-default" : "cursor-pointer"}`} > <SwitchTrack /> </BaseSwitch.Root> </div> );}Keep the folder layout
The files import each other by relative path (
../lib/motion), so keep each one at the path shown, undercomponents/eased/.
Usage
import { Switch } from "@/components/eased/switch/switch";export function NotificationSettings() { return ( <div className="flex flex-col divide-y divide-line-subtle"> <Switch defaultChecked description="Replies and @mentions in threads you follow." className="py-3.5"> Comments and mentions </Switch> <Switch description="New features and changelog posts, about twice a month." className="py-3.5"> Product updates </Switch> </div> );}Examples
Controlled
Control it with checked and onCheckedChange when other UI reflects the setting.
const [digest, setDigest] = useState(true);<Switch checked={digest} onCheckedChange={setDigest} description="A summary of the week, every Monday at 9:00."> Weekly digest</Switch>Disabled, with the reason
Say why a setting is locked in description.
<Switch defaultChecked disabled description="Required for workspace owners."> Sign-in alerts</Switch>Bare switch
Leave out children when the layout already shows the label, and name it with aria-label.
<Switch aria-label="Dark mode" checked={dark} onCheckedChange={setDark} />API reference
Switch
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | On (controlled). | |
defaultChecked | boolean | Initially on (uncontrolled). | |
onCheckedChange | (checked: boolean) => void | ||
value | string | What a form submits under name while on. | |
name | string | Form field name. | |
disabled | boolean | ||
required | boolean | ||
readOnly | boolean | Shows the state but doesn't change on click. | |
children | ReactNode | The label. With one, Switch renders a settings row. | |
description | ReactNode | Muted text under the label. | |
aria-label | string | Names a bare switch with no children. | |
id | string | The switch's id. | |
className | string | "" | On the row when there is a label, on the switch when there isn't. |
The switch is a 20×36px track with a 16px thumb inset 2px.
Keyboard
| Key | Action |
|---|---|
Tab | Moves focus to the switch. |
Space / Enter | Toggles it. |
Accessibility
- The switch has
role="switch"andaria-checked. A hidden input submits it with forms and works with Field validation. - With
children, a native<label>names it and clicking the label toggles it;descriptiondescribes it througharia-describedby. Withoutchildren, passaria-label. - The focus ring draws around the track. A disabled switch drops to 50% opacity and its label dims.
- The bare track is 20px tall, so on touch screens use the labelled row, where the label is part of the target.
Motion
The thumb springs across on the snappy spring (350ms). While pressed, it stretches 4px toward where it's about to go, then the stretch closes behind it as it travels; only translate and clip-path animate. The track colour changes over 150ms on the standard curve. Disabled and read-only switches don't stretch. Reduced motion: the thumb jumps to its new side without a spring or a stretch, and only colours fade.
Notes for AI
Pick Switch for a setting that applies immediately; pick Checkbox when the choice waits for a submit. Pass the label as children and helper text as description, and put row padding on className, which lands on the row.
Don't wrap it in your own label or flex row, and don't put a Label next to it: the row already handles naming, layout and the click target. Don't animate the thumb yourself.
Responsive
The label column takes the remaining width and wraps; the switch keeps its size and stays on the label's first line, so it never squeezes on a narrow screen.
Related components
- Animated Checkbox: an option that takes effect on submit.
- Animated Segmented Control: one of a few modes, shown side by side.
- Form Field with Validation: adds validation messages around a control.