Components
Theme switch
A light and dark toggle that changes at once or reveals the new theme with a circle, seam or wipe.
Free
pnpm dlx shadcn@latest add @caliper/theme-switchWhen to use
Use it for
- A header or settings control that flips light and dark.
- You want the change to feel like one deliberate transition.
Skip it when
- The app needs a three-way System/Light/Dark choice; use a select or segmented control.
Installation
terminal
pnpm dlx shadcn@latest add @caliper/theme-switch
Adds its 3 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.
Usage
This is the code behind the preview above.
example.tsx
import { useEffect, useState, type ReactNode } from "react" import { cn } from "@/components/ui/lib/cn" import { ThemeSwitch, type ThemeName, type ThemeReveal } from "@/components/ui/theme-switch" function AppWindow({ title, className, children }: { title: string; className?: string; children: ReactNode }) { return ( <section aria-label={title} className={cn("w-full max-w-2xl overflow-hidden rounded-overlay border border-border bg-background shadow-resting", className)}> <div className="flex h-10 items-center gap-3 border-b border-border px-4"> <span aria-hidden="true" className="flex gap-1.5"> <span className="size-2.5 rounded-full bg-border-strong" /> <span className="size-2.5 rounded-full bg-border-strong" /> <span className="size-2.5 rounded-full bg-border-strong" /> </span> <span className="truncate text-xs text-muted-foreground">{title}</span> </div> {children} </section> ); } const reveals: { variant: ThemeReveal; caption: string; detail: string }[] = [ { variant: "instant", caption: "Instant", detail: "Changes at once" }, { variant: "circle", caption: "Circle", detail: "Grows out from the switch" }, { variant: "seam", caption: "Seam", detail: "Opens down the middle" }, { variant: "wipe", caption: "Wipe", detail: "Sweeps across the page" }, ]; function applyTheme(next: ThemeName) { const root = document.documentElement; root.dataset.theme = next; root.dataset.themePreference = next; try { localStorage.setItem("caliper-theme", next); } catch { /* The appearance still changes for this visit. */ } window.dispatchEvent(new Event("caliper-theme")); } export function ThemeToggle() { const [theme, setTheme] = useState<ThemeName>("light"); useEffect(() => { const read = () => setTheme(document.documentElement.dataset.theme === "dark" ? "dark" : "light"); read(); window.addEventListener("caliper-theme", read); return () => window.removeEventListener("caliper-theme", read); }, []); return ( <AppWindow title="Settings · Appearance" className="max-w-md"> <div className="grid gap-4 p-6"> <div className="grid gap-1"> <p className="font-medium">Appearance</p> <p role="status" className="text-sm text-muted-foreground">Appearance is {theme}.</p> </div> <ul className="grid divide-y divide-border"> {reveals.map((item) => ( <li key={item.variant} className="flex items-center justify-between gap-4 py-3 last:pb-0"> <span className="grid text-sm"> <span className="font-medium">{item.caption}</span> <span className="text-muted-foreground">{item.detail}</span> </span> <ThemeSwitch theme={theme} variant={item.variant} onThemeChange={applyTheme} /> </li> ))} </ul> </div> </AppWindow> ); }
API reference
ThemeSwitch
component
PropTypeDefault
theme *ThemeName—
variantThemeReveal"circle"
onThemeChange *(next: ThemeName) => void—
classNamestring—
Accessibility
It is built on native elements and keeps aria-pressed and aria-label in step with state.
No custom key handling; Tab and the native activation keys do the work.
Motion
It checks prefers-reduced-motion itself and settles immediately instead of travelling. Every state is still reachable.