Selection and input · Material 3: Switch
Switch
Turns a setting on or off at once. The thumb stretches toward its side, then snaps round.
Examples and variants
Basic
Hold the thumb to see it squash wider.
function SwitchBasic() {
return (
<div className="flex flex-col gap-3">
<Label className="flex items-center gap-3">
<Switch defaultChecked />
Quiet chrome
</Label>
<Label className="flex items-center gap-3">
<Switch />
Notifications
</Label>
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/switch.json
npx shadcn@latest add https://isotopi.com/r/label.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install @base-ui/react cnCopy these 2 files to the same paths in your project. Imports use the
@/alias for your project root.components/ui/switch.tsxswitch
components/ui/switch.tsx"use client" import { Switch as SwitchPrimitive } from "@base-ui/react/switch" import { cn } from "cn" function Switch({ className, size = "default", ...props }: SwitchPrimitive.Root.Props & { size?: "sm" | "default" }) { return ( <SwitchPrimitive.Root data-slot="switch" data-size={size} className={cn( "hit-target group/switch relative inline-flex shrink-0 items-center rounded-(--switch-corner) border-0 p-inline-xs outline-none", "transition-colors duration-(--duration-fast) ease-(--ease-out)", "focus-visible:shadow-(--focus-ring)", "data-unchecked:bg-(--switch-track) data-checked:bg-(--switch-track-checked) data-unchecked:hover:bg-(--switch-track-hover) data-checked:hover:bg-(--switch-track-checked-hover)", "data-disabled:cursor-not-allowed data-disabled:data-unchecked:bg-(--switch-track-disabled) data-disabled:data-checked:bg-(--switch-track-checked-disabled)", "data-[size=default]:h-(--size-switch-h) data-[size=default]:w-(--size-switch-w)", "data-[size=sm]:h-icon-md data-[size=sm]:w-[calc(var(--size-icon-md)*2)]", className )} {...props} > <SwitchPrimitive.Thumb data-slot="switch-thumb" className={cn( "switch-thumb pointer-events-none block rounded-(--radius-pill) shadow-(--switch-thumb-elevation)", "data-unchecked:bg-(--color-fg-default) data-checked:bg-(--color-fg-on-accent)", "data-checked:translate-x-(--size-icon-md) data-[size=sm]:data-checked:translate-x-(--size-icon-md)", "group-data-[size=default]/switch:size-[calc(var(--size-switch-h)-var(--space-gap-inline-xs)*2)]", "group-data-[size=sm]/switch:size-[calc(var(--size-icon-md)-var(--space-gap-inline-xs))]", "dark:data-checked:bg-primary-foreground" )} /> </SwitchPrimitive.Root> ) } export { Switch }components/ui/label.tsxlabel
components/ui/label.tsx"use client" import * as React from "react" import { cn } from "cn" function Label({ className, ...props }: React.ComponentProps<"label">) { return ( <label data-slot="label" className={cn( "flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:text-(--field-label-disabled) group-data-[invalid=true]/field:text-(--color-danger-fg) peer-disabled:cursor-not-allowed peer-disabled:text-(--field-label-disabled)", className )} {...props} /> ) } export { Label }
Imports
What the examples on this page import:
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"Anatomy
- 1Track · --switch-width x --switch-height, --switch-track; on: --switch-track-checked.
- 2Thumb · --switch-thumb with --switch-thumb-elevation.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
checked / defaultChecked | boolean | — | On state. |
onCheckedChange | (checked: boolean) => void | — | Called on change. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--switch-width | var(--size-switch-w) | 36px | same |
--switch-height | var(--size-switch-h) | 20px | same |
--switch-track | var(--surface-container-high) | oklch(0.94 0.007 95) | oklch(0.27 0 0) |
--switch-track-checked | var(--color-brand) | oklch(0.18 0.008 260) | oklch(0.98 0 0) |
--switch-track-hover | color-mix(in oklch, var(--switch-track), var(--on-surface) var(--state-hover-mix)) | color-mix(in oklch, var(--switch-track), var(--on-surface) var(--state-hover-mix)) | same |
--switch-track-checked-hover | var(--color-brand-hover) | oklch(0.28 0.008 260) | oklch(0.9 0 0) |
--switch-track-disabled | var(--color-bg-disabled) | color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent) | same |
--switch-track-checked-disabled | color-mix(in oklch, var(--color-fg-disabled) 55%, transparent) | color-mix(in oklch, var(--color-fg-disabled) 55%, transparent) | same |
--switch-thumb | var(--surface-container-lowest) | oklch(1 0 0) | oklch(0.14 0 0) |
--switch-thumb-elevation | var(--elevation-1) | 0 1px 1px -0.5px oklch(0.22 0.01 260 / 7%), 0 2px 3px -1px oklch(0.22 0.01 260 / 10%), 0 4px 8px -4px oklch(0.22 0.01 260 / 6%) | 0 1px 2px oklch(0 0 0 / 35%), 0 2px 10px oklch(0 0 0 / 25%) |
--switch-corner | var(--shape-full) | 999px | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Toggle | Thumb stretches toward the other side, then snaps round. | --spring-snappy |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- role=switch with aria-checked.
- Space toggles.
- Label says what turns on, not 'On/Off'.
Usage
Do
- Use for settings that apply immediately.
Don’t
- Don't use inside a form that needs a Save button (use Checkbox).
Code
"use client"
import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
import { cn } from "cn"
function Switch({
className,
size = "default",
...props
}: SwitchPrimitive.Root.Props & {
size?: "sm" | "default"
}) {
return (
<SwitchPrimitive.Root
data-slot="switch"
data-size={size}
className={cn(
"hit-target group/switch relative inline-flex shrink-0 items-center rounded-(--switch-corner) border-0 p-inline-xs outline-none",
"transition-colors duration-(--duration-fast) ease-(--ease-out)",
"focus-visible:shadow-(--focus-ring)",
"data-unchecked:bg-(--switch-track) data-checked:bg-(--switch-track-checked) data-unchecked:hover:bg-(--switch-track-hover) data-checked:hover:bg-(--switch-track-checked-hover)",
"data-disabled:cursor-not-allowed data-disabled:data-unchecked:bg-(--switch-track-disabled) data-disabled:data-checked:bg-(--switch-track-checked-disabled)",
"data-[size=default]:h-(--size-switch-h) data-[size=default]:w-(--size-switch-w)",
"data-[size=sm]:h-icon-md data-[size=sm]:w-[calc(var(--size-icon-md)*2)]",
className
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
className={cn(
"switch-thumb pointer-events-none block rounded-(--radius-pill) shadow-(--switch-thumb-elevation)",
"data-unchecked:bg-(--color-fg-default) data-checked:bg-(--color-fg-on-accent)",
"data-checked:translate-x-(--size-icon-md) data-[size=sm]:data-checked:translate-x-(--size-icon-md)",
"group-data-[size=default]/switch:size-[calc(var(--size-switch-h)-var(--space-gap-inline-xs)*2)]",
"group-data-[size=sm]/switch:size-[calc(var(--size-icon-md)-var(--space-gap-inline-xs))]",
"dark:data-checked:bg-primary-foreground"
)}
/>
</SwitchPrimitive.Root>
)
}
export { Switch }