Installation
Add the component
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/switch.jsonFirst component? Set up your project first
Frameworks
The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.
"use client";
import * as React from "react";
import { motion, useReducedMotion } from "motion/react";
import { cn } from "@/lib/utils";
export type SwitchProps = {
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
label?: React.ReactNode;
disabled?: boolean;
className?: string;
};
const thumb = { type: "spring", stiffness: 300, damping: 25 } as const;
export function Switch({
checked,
defaultChecked = false,
onCheckedChange,
label = "Enable notifications",
disabled = false,
className,
}: SwitchProps) {
const [internal, setInternal] = React.useState(defaultChecked);
const value = checked ?? internal;
const [pressed, setPressed] = React.useState(false);
const reduce = useReducedMotion();
const toggle = () => {
if (disabled) return;
const next = !value;
if (checked === undefined) setInternal(next);
onCheckedChange?.(next);
};
return (
<label className={cn("flex items-center gap-3 text-[13px]", className)}>
<button
type="button"
role="switch"
aria-checked={value}
aria-label={typeof label === "string" ? label : undefined}
disabled={disabled}
onClick={toggle}
onPointerDown={() => setPressed(true)}
onPointerUp={() => setPressed(false)}
onPointerLeave={() => setPressed(false)}
className={cn(
"relative flex h-6 w-11 shrink-0 items-center rounded-full p-0.5 outline-none transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50",
value ? "bg-primary" : "bg-input",
)}
>
<motion.span
aria-hidden="true"
animate={{ x: value ? 20 : 0, width: pressed && !reduce ? 24 : 20 }}
transition={reduce ? { duration: 0 } : thumb}
className="block h-5 rounded-full bg-background shadow-sm"
/>
</button>
{label && <span>{label}</span>}
</label>
);
}Install
npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/switch.jsonDependencies
motion lucide-reactThe registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.
Usage
Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.
import { Switch } from "@/components/prequelui/switch";
export default function Demo() {
return (
<label className="flex items-center gap-3 text-[13.5px]">
<Switch defaultChecked onCheckedChange={(checked) => console.log(checked)} />
Enable notifications
</label>
);
}import { Switch } from "@/components/prequelui/switch";
export function Variant() {
return (
<Switch tempo={1.2}>
…
</Switch>
);
}Animation
The thumb does not simply travel: it stretches in the direction of the press and settles back to its resting width as it lands. The track changes colour in the same 200 ms so the state is never ambiguous.
- Thumb spring: 300 / 25; travel 20 px on a 44 px track.
- The thumb widens to 26 px while pressed, then returns to 20 px.
- The track colour crossfades in 200 ms.
- Reduced motion jumps the thumb and keeps the colour change.
API Reference
Every part takes exactly the props of the Switch primitive. The animated surface adds only motion props, so the accessibility tree and keyboard behaviour stay the same.
Switch
| Prop | Type | Default |
|---|---|---|
| checked | boolean | — |
| defaultChecked | boolean | false |
| onCheckedChange | (checked: boolean) => void | — |
| disabled | boolean | false |
| className | string | — |