A theme switch revealed by a 700 ms clip-path wipe.
Installation
Install the packages
npm install @prequelui/react motion@prequelui/react reuses the primitive library your components already depend on.
Add the component
npx shadcn@latest add https://animate.prequelui.terravidhal.me/r/theme-toggler.jsonnpx shadcn@latest add https://animate.prequelui.terravidhal.me/r/theme-toggler.json
Turn on the motion
npx @prequelui/cli@latest animate theme-togglerThe CLI finds the component through components.json, swaps the import and removes the CSS animation classes that Motion replaces.
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 { ThemeToggler } from "@/components/prequelui/effects/theme-toggler";
import { useTheme } from "@/components/site/theme-provider";
export default function Demo() {
const { theme, resolvedTheme, setTheme } = useTheme();
return (
<ThemeToggler theme={theme} resolvedTheme={resolvedTheme} setTheme={setTheme}>
{({ resolved, toggleTheme }) => (
<button onClick={() => toggleTheme(resolved === "dark" ? "light" : "dark")}>
Toggle theme
</button>
)}
</ThemeToggler>
);
}import { ThemeToggler } from "@/components/prequelui/theme-toggler";
export function Variant() {
return (
<ThemeToggler tempo={1.2}>
…
</ThemeToggler>
);
}Animation
The theme change is performed inside a View Transition, and the new snapshot is revealed by a clip-path wipe. The button, the preview and the theme provider all read the same resolved value, so no element flashes the wrong colour.
- Wipe: 700 ms ease-in-out, scaled by the motion tempo.
- Four directions: left-to-right, right-to-left, top-to-bottom and bottom-to-top.
- The old and new roots never crossfade; the new one is clipped over the old one.
- Without View Transitions or with reduced motion, the theme changes instantly.
API Reference
Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.
ThemeToggler
| Prop | Type | Default |
|---|---|---|
| theme | "light" | "dark" | "system" | — |
| resolvedTheme | "light" | "dark" | — |
| setTheme | (theme) => void | — |
| direction | "ltr" | "rtl" | "ttb" | "btt" | "ltr" |
| onImmediateChange | (theme) => void | — |
| children | ReactNode | (state) => ReactNode | — |