Switch
On/off settings that take effect immediately. Four switches with different ways of showing their state.
Standard
An on/off setting with a label and a one-line description. The thumb travels without overshoot.
A summary every Monday morning.
Only when something ships.
Always on for admins.
Installation
$npx shadcn@latest add @stillui/switchOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/switch
import { motion, useReducedMotion } from "motion/react";
import { useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
// A short travel with no overshoot: switches get flipped often, so they stay quick.
const THUMB = { type: "spring", stiffness: 600, damping: 38, mass: 0.7 } as const;
type SwitchProps = {
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
disabled?: boolean;
/** Visible label. Clicking it toggles the switch. */
label?: ReactNode;
/** One line under the label explaining the effect. */
description?: ReactNode;
className?: string;
id?: string;
};
export function Switch({
checked,
defaultChecked = false,
onCheckedChange,
disabled,
label,
description,
className,
id,
}: SwitchProps) {
const reduce = useReducedMotion();
const [internal, setInternal] = useState(defaultChecked);
const controlled = checked !== undefined;
const on = controlled ? checked : internal;
const autoId = useId();
const switchId = id ?? autoId;
const descId = description ? `${switchId}-desc` : undefined;
const toggle = () => {
if (disabled) return;
if (!controlled) setInternal(!on);
onCheckedChange?.(!on);
};
return (
<div className={cn("flex items-start gap-3", disabled && "opacity-50", className)}>
<button
id={switchId}
type="button"
role="switch"
aria-checked={on}
aria-describedby={descId}
disabled={disabled}
onClick={toggle}
className={cn(
"group relative inline-flex h-6 w-10 shrink-0 items-center rounded-full p-0.5",
"transition-colors duration-200 ease-out disabled:cursor-not-allowed",
on ? "bg-primary" : "bg-input",
)}
>
<motion.span
className={cn(
"block size-5 rounded-full shadow-raised transition-colors duration-200",
// The thumb always contrasts with its track, in both themes.
on ? "bg-primary-foreground" : "bg-white dark:bg-foreground",
)}
animate={{ x: on ? 16 : 0 }}
transition={reduce ? { duration: 0 } : THUMB}
// A slight squash on press tells the hand it was heard.
whileTap={reduce || disabled ? undefined : { scaleX: 1.12 }}
style={{ originX: on ? 1 : 0 }}
/>
</button>
{label || description ? (
<div className="grid gap-0.5 pt-0.5">
{label ? (
<label htmlFor={switchId} className="cursor-pointer text-sm font-medium leading-5">
{label}
</label>
) : null}
{description ? (
<p id={descId} className="text-[13px] leading-5 text-muted-foreground">
{description}
</p>
) : null}
</div>
) : null}
</div>
);
}States
- Off / On
- Track color carries the state; thumb position confirms it for color-blind users.
- Pressed
- Thumb stretches slightly toward where it is going.
- Focus
- Outline on the track. Space toggles.
- Disabled
- The whole row dims so the label reads as unavailable too.
Notes
Use a switch for settings that apply immediately. If the change waits for a Save button, use a checkbox.
Stretch
The thumb stretches toward the other side while you press, then slides there when you let go.
Installation
$npx shadcn@latest add @stillui/switch-stretchOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/switch#stretch
import { motion, useReducedMotion } from "motion/react";
import { useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const SPRING = { type: "spring", stiffness: 520, damping: 34, mass: 0.7 } as const;
type SwitchStretchProps = {
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
label?: ReactNode;
className?: string;
};
/** A switch whose thumb stretches toward the other side while pressed, then slides there on release. */
export function SwitchStretch({ checked, defaultChecked = false, onCheckedChange, label, className }: SwitchStretchProps) {
const reduce = useReducedMotion();
const id = useId();
const [internal, setInternal] = useState(defaultChecked);
const [pressed, setPressed] = useState(false);
const on = checked ?? internal;
const toggle = () => {
if (checked === undefined) setInternal(!on);
onCheckedChange?.(!on);
};
return (
<div className={cn("flex items-center gap-3", className)}>
<button
id={id}
type="button"
role="switch"
aria-checked={on}
onClick={toggle}
onPointerDown={() => setPressed(true)}
onPointerUp={() => setPressed(false)}
onPointerLeave={() => setPressed(false)}
className={cn(
"flex h-7 w-12 shrink-0 items-center rounded-full p-[3px] transition-colors duration-200",
on ? "justify-end bg-primary" : "justify-start bg-input",
)}
>
{/* Flex alignment decides the side; layout animation carries the thumb between them. */}
<motion.span
layout={!reduce}
transition={SPRING}
className={cn("block h-[22px] rounded-full shadow-raised", on ? "bg-primary-foreground" : "bg-white dark:bg-foreground")}
style={{ borderRadius: 999 }}
animate={{ width: pressed && !reduce ? 30 : 22 }}
/>
</button>
{label ? (
<label htmlFor={id} className="cursor-pointer text-sm font-medium">
{label}
</label>
) : null}
</div>
);
}States
- Off / on
- A round thumb on the left or the right; the track fills with the primary color when on.
- Pressed
- The thumb widens into a pill, like a finger pushing it.
- Released
- The thumb slides to the other side and returns to a circle in the same spring.
- Keyboard
- role switch with aria-checked; Space and Enter toggle it.
- Reduced motion
- No stretch or slide; the thumb jumps to its side.
Notes
Bigger than the standard switch, so it suits touch-first settings screens.
Labelled
A switch that writes its state inside the track, so on and off never depend on color alone.
Installation
$npx shadcn@latest add @stillui/switch-labelledOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/switch#labelled
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const THUMB = { type: "spring", stiffness: 560, damping: 38, mass: 0.7 } as const;
type SwitchLabelledProps = {
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
label?: ReactNode;
onText?: string;
offText?: string;
className?: string;
};
/** A switch that says its state inside the track, for places where color alone isn’t enough. */
export function SwitchLabelled({
checked,
defaultChecked = false,
onCheckedChange,
label,
onText = "On",
offText = "Off",
className,
}: SwitchLabelledProps) {
const reduce = useReducedMotion();
const id = useId();
const [internal, setInternal] = useState(defaultChecked);
const on = checked ?? internal;
const toggle = () => {
if (checked === undefined) setInternal(!on);
onCheckedChange?.(!on);
};
return (
<div className={cn("flex items-center gap-3", className)}>
<button
id={id}
type="button"
role="switch"
aria-checked={on}
onClick={toggle}
className={cn(
"relative inline-flex h-7 w-[60px] shrink-0 items-center rounded-full p-[3px] transition-[color,background-color,box-shadow,scale] active:scale-[0.95] duration-200",
on ? "bg-primary text-primary-foreground" : "bg-input text-muted-foreground",
)}
>
{/* The word sits on the side the thumb is not covering. */}
<AnimatePresence initial={false} mode="popLayout">
<motion.span
key={on ? "on" : "off"}
aria-hidden
className={cn("absolute text-[11px] font-semibold uppercase tracking-wide", on ? "left-2.5" : "right-2")}
initial={{ opacity: 0, x: reduce ? 0 : on ? -6 : 6 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, transition: { duration: 0.08 } }}
transition={{ duration: reduce ? 0 : 0.18, delay: reduce ? 0 : 0.06 }}
>
{on ? onText : offText}
</motion.span>
</AnimatePresence>
<motion.span
className={cn("relative block size-[22px] rounded-full shadow-raised", on ? "bg-primary-foreground" : "bg-white dark:bg-foreground")}
animate={{ x: on ? 32 : 0 }}
transition={reduce ? { duration: 0 } : THUMB}
/>
</button>
{label ? (
<label htmlFor={id} className="cursor-pointer text-sm font-medium">
{label}
</label>
) : null}
</div>
);
}States
- Off
- “Off” on the right, thumb on the left, neutral track.
- On
- The thumb slides right; “On” fades in on the left over the primary track.
- Custom words
- Pass onText and offText, e.g. Yes and No. Keep them to three letters or so.
- Keyboard
- role switch with aria-checked. The words are hidden from screen readers, which announce the state already.
- Reduced motion
- Thumb and words swap without sliding.
Notes
Helpful for people who can’t tell the track colors apart, and in forms printed or screenshotted for review.
Icon thumb
A switch with a check or a cross inside the thumb. The icon turns as the thumb travels.
Save drafts every few seconds.
Underline words that might be misspelled.
Installation
$npx shadcn@latest add @stillui/switch-iconOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/switch#icon
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const THUMB = { type: "spring", stiffness: 600, damping: 38, mass: 0.7 } as const;
type SwitchIconProps = {
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
label?: ReactNode;
description?: ReactNode;
className?: string;
};
/** A switch with a check or a cross inside the thumb. The icon turns as the thumb travels. */
export function SwitchIcon({ checked, defaultChecked = false, onCheckedChange, label, description, className }: SwitchIconProps) {
const reduce = useReducedMotion();
const id = useId();
const [internal, setInternal] = useState(defaultChecked);
const on = checked ?? internal;
const descId = description ? `${id}-desc` : undefined;
const toggle = () => {
if (checked === undefined) setInternal(!on);
onCheckedChange?.(!on);
};
return (
<div className={cn("flex items-start gap-3", className)}>
<button
id={id}
type="button"
role="switch"
aria-checked={on}
aria-describedby={descId}
onClick={toggle}
className={cn("inline-flex h-6 w-10 shrink-0 items-center rounded-full p-0.5 transition-[color,background-color,box-shadow,scale] active:scale-[0.95] duration-200", on ? "bg-primary" : "bg-input")}
>
<motion.span
className={cn(
"grid size-5 place-items-center rounded-full shadow-raised",
on ? "bg-primary-foreground text-primary" : "bg-white text-muted-foreground dark:bg-foreground dark:text-background",
)}
animate={{ x: on ? 16 : 0, rotate: on ? 0 : -90 }}
transition={reduce ? { duration: 0 } : THUMB}
>
<AnimatePresence initial={false} mode="popLayout">
<motion.svg
key={on ? "on" : "off"}
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="size-3"
initial={{ opacity: 0, scale: reduce ? 1 : 0.5 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: reduce ? 1 : 0.5 }}
transition={{ duration: reduce ? 0 : 0.15 }}
aria-hidden
>
{on ? <path d="M3.5 8.5l3 3 6-7" /> : <path d="M5 5l6 6m0-6l-6 6" />}
</motion.svg>
</AnimatePresence>
</motion.span>
</button>
{label || description ? (
<div className="grid gap-0.5 pt-0.5">
{label ? (
<label htmlFor={id} className="cursor-pointer text-sm font-medium leading-5">
{label}
</label>
) : null}
{description ? (
<p id={descId} className="text-[13px] leading-5 text-muted-foreground">
{description}
</p>
) : null}
</div>
) : null}
</div>
);
}States
- Off
- A cross in the thumb on the left.
- On
- The thumb rolls right and the cross becomes a check in the primary color.
- Description
- An optional line under the label, linked with aria-describedby.
- Keyboard
- role switch with aria-checked; Space and Enter toggle it.
- Reduced motion
- The thumb and icon change without rolling.
Notes
The icon gives a second cue besides color, which helps people with color vision deficiency.