Theme Toggler
Install: npx shadcn@latest add https://jiguang132.com/r/magic-ui/animated-theme-toggler.json
Use case
Switch theme with a View Transition shape.
Native props used by the examples
Extracted from all local examples for this entry, grouped by imported module and native owner, including other Magic components used by compound demos. This is not an exhaustive API or a universal host variant. Inspect spread object declarations and the source types/defaults below.
| Owning component prop | Explanation | Example source |
|---|---|---|
AnimatedThemeToggler.variant (animated-theme-toggler) | Owning component's style/shape union; never substitute a universal variant API. | examples/animated-theme-toggler-square-demo.tsx、examples/animated-theme-toggler-diamond-demo.tsx、examples/animated-theme-toggler-rectangle-demo.tsx、examples/animated-theme-toggler-hexagon-demo.tsx、examples/animated-theme-toggler-triangle-demo.tsx、examples/animated-theme-toggler-star-demo.tsx |
AnimatedThemeToggler.theme (animated-theme-toggler) | Controlled light/dark value; the host owns persistence. | examples/animated-theme-toggler-next-themes-demo.tsx |
AnimatedThemeToggler.onThemeChange (animated-theme-toggler) | Receive the requested theme; pair with controlled theme. | examples/animated-theme-toggler-next-themes-demo.tsx |
Interaction and state checks
- Animated Theme Toggler Demo —
animated-theme-toggler-demo - Animated Theme Toggler — Square —
animated-theme-toggler-square-demo - Animated Theme Toggler — Diamond —
animated-theme-toggler-diamond-demo - Animated Theme Toggler — Rectangle —
animated-theme-toggler-rectangle-demo - Animated Theme Toggler — Hexagon —
animated-theme-toggler-hexagon-demo - Animated Theme Toggler — Triangle —
animated-theme-toggler-triangle-demo - Animated Theme Toggler — Star —
animated-theme-toggler-star-demo - Animated Theme Toggler — next-themes —
animated-theme-toggler-next-themes-demo
These names identify real example entries, not new component props or extra components. Check pointer/keyboard behavior, themes, small screens and failures for the selected example. Decorative effects must not invent disabled/error/submission states. Complete example installation includes the actual dependency closure.
Business integration boundary
With theme/onThemeChange the parent owns persistence. Reuse next-themes; never mutate the platform parent from an isolated preview.
Host and motion setup
Initialize Tailwind CSS v4, semantic CSS variables and the dark variant; import the independent local Registry namespace. Reuse an existing next-themes Provider without substituting JGuang UI/Base UI/HeroUI APIs. Mount effects in view and unmount offscreen; provide static alternatives for unavailable Canvas/WebGL or failed remote media. Respect prefers-reduced-motion and verify timer/RAF/observer/listener cleanup: stopping CSS alone does not prove the engine stopped. Examples provide no backend, login, payment or real AI stream. This practical guide is bilingual; detailed upstream references and demo text retain their original language.
Available examples
animated-theme-toggler-demo, animated-theme-toggler-square-demo, animated-theme-toggler-diamond-demo, animated-theme-toggler-rectangle-demo, animated-theme-toggler-hexagon-demo, animated-theme-toggler-triangle-demo, animated-theme-toggler-star-demo, animated-theme-toggler-next-themes-demo
Demo state and mock data do not provide a backend service. Inspect the component props before wiring real data.
Installation
<Tabs defaultValue="cli"> <TabsList> <TabsTrigger value="cli">CLI</TabsTrigger> <TabsTrigger value="manual">Manual</TabsTrigger> </TabsList> <TabsContent value="cli">npx shadcn@latest add @magicui/animated-theme-toggler
</TabsContent>
<TabsContent value="manual">
<Steps>
<Step>Copy and paste the following code into your project.</Step>
<Step>Update the import paths to match your project setup.</Step>
<Step>Add the required CSS into your global CSS file</Step>
The component sets --magicui-theme-toggle-vt-duration and a data-magicui-theme-vt="active" flag on <html> only while a toggle is in flight, so the clip-path animation stays in sync with the view-transition group without affecting any other view transitions in your app (e.g. Next.js navigation transitions).
::view-transition-old(root),
::view-transition-new(root) {
animation: none;
mix-blend-mode: normal;
}
/* Scoped to AnimatedThemeToggler — the toggler sets the data-attribute and the variable only during a theme toggle. */
html[data-magicui-theme-vt="active"]::view-transition-group(root) {
animation-duration: var(--magicui-theme-toggle-vt-duration);
}
</Steps>
</TabsContent>
</Tabs>
Usage
import { AnimatedThemeToggler } from "@/components/ui/animated-theme-toggler"
<AnimatedThemeToggler />
Use variant to change the clip-path shape of the view transition (circle, square, triangle, diamond, rectangle, hexagon, or star):
<AnimatedThemeToggler variant="star" />
Expand from the viewport center instead of the button with fromCenter:
<AnimatedThemeToggler variant="hexagon" duration={600} fromCenter />
Examples
The preview at the top of this page uses the default circle reveal. Below, each variant has its own isolated preview so you can test one shape at a time.
Square
Diamond
Rectangle
Hexagon
Triangle
Star
Next.js (next-themes)
Pass theme and onThemeChange to keep useTheme() subscribers in sync on toggle. When controlled, the component skips localStorage and lets the parent own persistence.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Additional classes for the button |
duration | number | 400 | Duration of the theme transition animation in milliseconds |
variant | TransitionVariant | "circle" | Shape used for the view-transition clip-path reveal |
fromCenter | boolean | false | If true, the clip expands from the viewport center instead of the button |
theme | "light" | "dark" | — | Controlled theme value. When set, the parent owns persistence (e.g. next-themes). |
onThemeChange | (theme: "light" | "dark") => void | — | Called on toggle. Pair with theme for controlled usage. |
Export TransitionVariant from the same module when you need typed props or menus.
Credits
- Credit to Nazam Kalsi, chishiyac, dikshantgulekar20-oss
Complete example installation
The component command installs the component. Each example command also includes its actual imported components, native support UI, utilities and pinned dependencies. Helpers are not additional catalog components.
Animated Theme Toggler Demo
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Animated Theme Toggler — Square
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-square-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-square-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Animated Theme Toggler — Diamond
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-diamond-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-diamond-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Animated Theme Toggler — Rectangle
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-rectangle-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-rectangle-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Animated Theme Toggler — Hexagon
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-hexagon-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-hexagon-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Animated Theme Toggler — Triangle
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-triangle-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-triangle-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Animated Theme Toggler — Star
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-star-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-star-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, tailwind-merge@3.6.0
Animated Theme Toggler — next-themes
npx shadcn@latest add https://jiguang132.com/r/magic-ui/examples/animated-theme-toggler/animated-theme-toggler-next-themes-demo.json
Default import: @/components/magicui/examples/animated-theme-toggler-next-themes-demo
Dependencies: clsx@2.1.1, lucide-react@1.24.0, next-themes@0.4.6, tailwind-merge@3.6.0
Minimal usage and theme
import Demo from '@/components/magicui/examples/animated-theme-toggler-demo';
import { ThemeProvider } from 'next-themes';
export default function Example() {
return <ThemeProvider attribute="class" defaultTheme="system" enableSystem><Demo /></ThemeProvider>;
}
Initialize Tailwind CSS v4, semantic color variables and the dark variant first. Reuse an existing next-themes provider rather than nesting another. Next Link examples require a Next.js environment; in static Vite projects adapt the demo links to your router without changing the native component API. Some media and tweets require network access. Examples do not supply authentication or backend services.
Source-backed parameter reference
Implementation signatures and types are generated from the pinned local source. Optional props, native inherited types, union branches and never exclusions are preserved. A forwardRef implementation's ref is a React reference, not an additional business prop.
AnimatedThemeToggler({
className,
duration = 400,
variant,
fromCenter = false,
theme,
onThemeChange,
...props
}: AnimatedThemeTogglerProps)
export type TransitionVariant =
| "circle"
| "square"
| "triangle"
| "diamond"
| "hexagon"
| "rectangle"
| "star"
interface AnimatedThemeTogglerProps extends React.ComponentPropsWithoutRef<"button"> {
duration?: number
variant?: TransitionVariant
/** When true, the transition expands from the viewport center instead of the button center. */
fromCenter?: boolean
/**
* Controlled theme value. When provided, the parent owns persistence
* (e.g. `next-themes`) and this component will not write to localStorage.
*/
theme?: "light" | "dark"
/** Called on toggle. Pair with `theme` for controlled usage. */
onThemeChange?: (theme: "light" | "dark") => void
}
Destructuring defaults in source
Only defaults written directly in parameter/props destructuring are listed. Conditional expressions, runtime theme and computed values require source inspection; this is not an exhaustive behavior-default list.
// AnimatedThemeToggler
duration = 400
// AnimatedThemeToggler
fromCenter = false
Native and external types
External types remain defined by React or native dependencies, not manually expanded into guessed props. The installed version's TypeScript definitions are authoritative.
React.ComponentPropsWithoutRef<"button">