← Gallery
Switch
UI primitiveswitchTurns one setting on or off, and the change applies immediately: notifications, dark mode, auto-renew.
Live preview
Turns one setting on or off, and the change applies immediately: notifications, dark mode,
auto-renew. A choice that only applies after Save is a Checkbox.
Live examples
On, with its label
Off
Disabled
Anatomy
- Track — the
Switchroot; filled withprimarywhen on. - Thumb — the circle that moves right when on.
- Label — a separate
Labelnext to it, linked withhtmlFor/id.
From the code
- Tokens consumed
cardprimaryringswitch-background- Radius
- fixed
rounded-full(not the recipe control radius) - Focus
- focus-visible:border-ring · focus-visible:ring-[3px] · focus-visible:ring-ring/50
- Motion
- transition-[background-color,border-color,box-shadow,opacity] · transition-transform
States
| State | Looks | Note |
|---|---|---|
| On | primary track, thumb on the right. | — |
| Off | switch-background track, thumb on the left. | — |
| Focus | Border in ring, 3px ring at 50%. | Space toggles it. |
| Disabled | 50% opacity, not-allowed cursor. | — |
Do
- Always pair it with a visible label that names the setting, not the action ("Email notifications", not "Turn on").
- Apply the change immediately and show the result.
- Make the label clickable too, so the whole row is a target.
Don't
- Don't use a switch inside a form that needs a submit; use a checkbox.
- Don't use a switch without a label, or with a label that changes between on and off.
Known gaps
- The track is about 18px × 32px, far under the 44px touch minimum (DESIGN-LANGUAGE §41). Make the label and row the target, or wrap the switch in a 44px area.
- On uses
primary, notbrand, so it may not match a primary button in skins where the two roles differ.
Machine checks
- Height under the 44px touch minimum (DESIGN-LANGUAGE §41):
switch18.4px × 32px. On touch surfaces, extend the hit area or use a taller size. - Radius is fixed (rounded-full) instead of the recipe control radius used by Button and Tabs.
Sources: DESIGN-LANGUAGE §24 Interaction rules; §41 Touch targets · Rule catalog: detector SS003 (transition-all)
Size
1.2 KB
Exports
1
Imports
3
Type
ui
Exports
SwitchImports
- react
- @radix-ui/react-switch
- ./utils
Source
View on GitHub →"use client";
import * as React from "react";
import * as SwitchPrimitive from "@radix-ui/react-switch";
import { cn } from "./utils";
function Switch({
className,
...props
}: React.ComponentProps<typeof SwitchPrimitive.Root>) {
return (
<SwitchPrimitive.Root
data-slot="switch"
className={cn(
"peer data-[state=checked]:bg-primary data-[state=unchecked]:bg-switch-background focus-visible:border-ring focus-visible:ring-ring/50 dark:data-[state=unchecked]:bg-input/80 inline-flex h-[1.15rem] w-8 shrink-0 items-center rounded-full border border-transparent transition-[background-color,border-color,box-shadow,opacity] outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50",
className,
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
className={cn(
"bg-card dark:data-[state=unchecked]:bg-card-foreground dark:data-[state=checked]:bg-primary-foreground pointer-events-none block size-4 rounded-full ring-0 transition-transform data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:translate-x-0",
)}
/>
</SwitchPrimitive.Root>
);
}
export { Switch };