Back to Base UIYou are on Switch.

Switch

A switch whose thumb stretches 3/16 and lands softly.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks

Libraries

Base UI

Motion

Spring + short tween

Reduced motion

Fade in place
switch.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/switch.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/switch.json
npmthe default on every machine

First 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.

5 runtimes
switch.tsx
"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.json
npmthe default on every machine

Dependencies

motion lucide-react

The 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.

02

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.

switch.tsx
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>
  );
}
03

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.
spring 300 / 25stretch 26 pxtravel 20 pxcolour 200 ms
04

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

PropTypeDefault
checkedboolean—
defaultCheckedbooleanfalse
onCheckedChange(checked: boolean) => void—
disabledbooleanfalse
classNamestring—

Next component

Tabs

Tabs with a solid indicator and panels that blur out then in.