Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

feat(animated-theme-toggler): add new shape variants and examples (#947)

Matheus Sano1485027fb500
diff --git a/apps/www/content/docs/components/animated-theme-toggler.mdx b/apps/www/content/docs/components/animated-theme-toggler.mdx
index 194b1407..a3435e24 100644
--- a/apps/www/content/docs/components/animated-theme-toggler.mdx
+++ b/apps/www/content/docs/components/animated-theme-toggler.mdx
@@ -1,7 +1,7 @@
---
title: Theme Toggler
date: 2025-08-17
-description: An Animated theme toggler component, fully customizable using Tailwind CSS.
+description: Animated theme toggle using the View Transitions API with configurable clip-path shapes and origin.
author: Nazam Kalsi
published: true
---
@@ -36,12 +36,19 @@ npx shadcn@latest add @magicui/animated-theme-toggler
<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).
+
```css title="app/globals.css" showLineNumbers
::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>
@@ -60,13 +67,57 @@ 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`):
+
+```tsx showLineNumbers
+<AnimatedThemeToggler variant="star" />
+```
+
+Expand from the viewport center instead of the button with `fromCenter`:
+
+```tsx showLineNumbers
+<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
+
+<ComponentPreview name="animated-theme-toggler-square-demo" />
+
+### Diamond
+
+<ComponentPreview name="animated-theme-toggler-diamond-demo" />
+
+### Rectangle
+
+<ComponentPreview name="animated-theme-toggler-rectangle-demo" />
+
+### Hexagon
+
+<ComponentPreview name="animated-theme-toggler-hexagon-demo" />
+
+### Triangle
+
+<ComponentPreview name="animated-theme-toggler-triangle-demo" />
+
+### Star
+
+<ComponentPreview name="animated-theme-toggler-star-demo" />
+
## Props
-| Prop | Type | Default | Description |
-| ----------- | -------- | ------- | ---------------------------------------------------------- |
-| `className` | `String` | " " | Additional classes to be added to the component |
-| `duration` | `Number` | `400` | Duration of the theme transition animation in milliseconds |
+| 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 |
+
+Export `TransitionVariant` from the same module when you need typed props or menus.
## Credits
-- Credit to [Nazam Kalsi](https://nazam-kalsi-portfolio.vercel.app)
+- Credit to [Nazam Kalsi](https://nazam-kalsi-portfolio.vercel.app), [chishiyac](https://github.com/chishiyac), [dikshantgulekar20-oss](https://github.com/dikshantgulekar20-oss)
diff --git a/apps/www/package.json b/apps/www/package.json
index e185d3c0..13d1c5b8 100644
--- a/apps/www/package.json
+++ b/apps/www/package.json
@@ -153,4 +153,4 @@
"prettier-plugin-tailwindcss"
]
}
-}
+}
\ No newline at end of file
diff --git a/apps/www/public/llms-full.txt b/apps/www/public/llms-full.txt
index 756861de..832ba066 100644
--- a/apps/www/public/llms-full.txt
+++ b/apps/www/public/llms-full.txt
@@ -3066,7 +3066,7 @@ export default function AnimatedShinyTextDemo() {
===== COMPONENT: animated-theme-toggler =====
Title: Theme Toggler
-Description: A component for theme changing animation.
+Description: Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.
--- file: magicui/animated-theme-toggler.tsx ---
"use client"
@@ -3077,15 +3077,134 @@ import { flushSync } from "react-dom"
import { cn } from "@/lib/utils"
+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
+}
+
+function polygonCollapsed(cx: number, cy: number, vertexCount: number): string {
+ const pairs = Array.from(
+ { length: vertexCount },
+ () => `${cx}px ${cy}px`
+ ).join(", ")
+ return `polygon(${pairs})`
+}
+
+function getThemeTransitionClipPaths(
+ variant: TransitionVariant,
+ cx: number,
+ cy: number,
+ maxRadius: number,
+ viewportWidth: number,
+ viewportHeight: number
+): [string, string] {
+ switch (variant) {
+ case "circle":
+ return [
+ `circle(0px at ${cx}px ${cy}px)`,
+ `circle(${maxRadius}px at ${cx}px ${cy}px)`,
+ ]
+ case "square": {
+ const halfW = Math.max(cx, viewportWidth - cx)
+ const halfH = Math.max(cy, viewportHeight - cy)
+ const halfSide = Math.max(halfW, halfH) * 1.05
+ const end = [
+ `${cx - halfSide}px ${cy - halfSide}px`,
+ `${cx + halfSide}px ${cy - halfSide}px`,
+ `${cx + halfSide}px ${cy + halfSide}px`,
+ `${cx - halfSide}px ${cy + halfSide}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
+ }
+ case "triangle": {
+ const scale = maxRadius * 2.2
+ const dx = (Math.sqrt(3) / 2) * scale
+ const verts = [
+ `${cx}px ${cy - scale}px`,
+ `${cx + dx}px ${cy + 0.5 * scale}px`,
+ `${cx - dx}px ${cy + 0.5 * scale}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 3), `polygon(${verts})`]
+ }
+ case "diamond": {
+ // Slightly larger than the view-transition circle radius so axis-aligned coverage matches the circle reveal.
+ const R = maxRadius * Math.SQRT2
+ const end = [
+ `${cx}px ${cy - R}px`,
+ `${cx + R}px ${cy}px`,
+ `${cx}px ${cy + R}px`,
+ `${cx - R}px ${cy}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
+ }
+ case "hexagon": {
+ const R = maxRadius * Math.SQRT2
+ const verts: string[] = []
+ for (let i = 0; i < 6; i++) {
+ const a = -Math.PI / 2 + (i * Math.PI) / 3
+ verts.push(`${cx + R * Math.cos(a)}px ${cy + R * Math.sin(a)}px`)
+ }
+ return [polygonCollapsed(cx, cy, 6), `polygon(${verts.join(", ")})`]
+ }
+ case "rectangle": {
+ const halfW = Math.max(cx, viewportWidth - cx)
+ const halfH = Math.max(cy, viewportHeight - cy)
+ const end = [
+ `${cx - halfW}px ${cy - halfH}px`,
+ `${cx + halfW}px ${cy - halfH}px`,
+ `${cx + halfW}px ${cy + halfH}px`,
+ `${cx - halfW}px ${cy + halfH}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
+ }
+ case "star": {
+ // Small overscan so the last frames never leave a 1px seam before the transition group ends.
+ const R = maxRadius * Math.SQRT2 * 1.03
+ const innerRatio = 0.42
+ const starPolygon = (radius: number) => {
+ const verts: string[] = []
+ for (let i = 0; i < 5; i++) {
+ const outerA = -Math.PI / 2 + (i * 2 * Math.PI) / 5
+ verts.push(
+ `${cx + radius * Math.cos(outerA)}px ${cy + radius * Math.sin(outerA)}px`
+ )
+ const innerA = outerA + Math.PI / 5
+ verts.push(
+ `${cx + radius * innerRatio * Math.cos(innerA)}px ${cy + radius * innerRatio * Math.sin(innerA)}px`
+ )
+ }
+ return `polygon(${verts.join(", ")})`
+ }
+ const startR = Math.max(2, R * 0.025)
+ return [starPolygon(startR), starPolygon(R)]
+ }
+ default:
+ return [
+ `circle(0px at ${cx}px ${cy}px)`,
+ `circle(${maxRadius}px at ${cx}px ${cy}px)`,
+ ]
+ }
}
export const AnimatedThemeToggler = ({
className,
duration = 400,
+ variant,
+ fromCenter = false,
...props
}: AnimatedThemeTogglerProps) => {
+ const shape = variant ?? "circle"
const [isDark, setIsDark] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)
@@ -3109,11 +3228,20 @@ export const AnimatedThemeToggler = ({
const button = buttonRef.current
if (!button) return
- const { top, left, width, height } = button.getBoundingClientRect()
- const x = left + width / 2
- const y = top + height / 2
const viewportWidth = window.visualViewport?.width ?? window.innerWidth
const viewportHeight = window.visualViewport?.height ?? window.innerHeight
+
+ let x: number
+ let y: number
+ if (fromCenter) {
+ x = viewportWidth / 2
+ y = viewportHeight / 2
+ } else {
+ const { top, left, width, height } = button.getBoundingClientRect()
+ x = left + width / 2
+ y = top + height / 2
+ }
+
const maxRadius = Math.hypot(
Math.max(x, viewportWidth - x),
Math.max(y, viewportHeight - y)
@@ -3131,29 +3259,52 @@ export const AnimatedThemeToggler = ({
return
}
+ const root = document.documentElement
+ root.dataset.magicuiThemeVt = "active"
+ root.style.setProperty(
+ "--magicui-theme-toggle-vt-duration",
+ `${duration}ms`
+ )
+ const cleanup = () => {
+ delete root.dataset.magicuiThemeVt
+ root.style.removeProperty("--magicui-theme-toggle-vt-duration")
+ }
+
const transition = document.startViewTransition(() => {
flushSync(applyTheme)
})
+ if (typeof transition?.finished?.finally === "function") {
+ transition.finished.finally(cleanup)
+ } else {
+ cleanup()
+ }
const ready = transition?.ready
if (ready && typeof ready.then === "function") {
+ const clipPath = getThemeTransitionClipPaths(
+ shape,
+ x,
+ y,
+ maxRadius,
+ viewportWidth,
+ viewportHeight
+ )
ready.then(() => {
document.documentElement.animate(
{
- clipPath: [
- `circle(0px at ${x}px ${y}px)`,
- `circle(${maxRadius}px at ${x}px ${y}px)`,
- ],
+ clipPath,
},
{
duration,
- easing: "ease-in-out",
+ // Star: linear avoids easing overshoot that fights polygon interpolation at t→1; VT group duration is synced above.
+ easing: shape === "star" ? "linear" : "ease-in-out",
+ fill: "forwards",
pseudoElement: "::view-transition-new(root)",
}
)
})
}
- }, [isDark, duration])
+ }, [shape, fromCenter, duration, isDark])
return (
<button
@@ -3177,7 +3328,101 @@ Title: Animated Theme Toggler Demo
import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
export default function AnimatedThemeTogglerDemo() {
- return <AnimatedThemeToggler />
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler />
+ </div>
+ )
+}
+
+
+===== EXAMPLE: animated-theme-toggler-square-demo =====
+Title: Animated Theme Toggler — Square
+
+--- file: example/animated-theme-toggler-square-demo.tsx ---
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerSquareDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="square" />
+ </div>
+ )
+}
+
+
+===== EXAMPLE: animated-theme-toggler-diamond-demo =====
+Title: Animated Theme Toggler — Diamond
+
+--- file: example/animated-theme-toggler-diamond-demo.tsx ---
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerDiamondDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="diamond" />
+ </div>
+ )
+}
+
+
+===== EXAMPLE: animated-theme-toggler-rectangle-demo =====
+Title: Animated Theme Toggler — Rectangle
+
+--- file: example/animated-theme-toggler-rectangle-demo.tsx ---
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerRectangleDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="rectangle" />
+ </div>
+ )
+}
+
+
+===== EXAMPLE: animated-theme-toggler-hexagon-demo =====
+Title: Animated Theme Toggler — Hexagon
+
+--- file: example/animated-theme-toggler-hexagon-demo.tsx ---
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerHexagonDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="hexagon" />
+ </div>
+ )
+}
+
+
+===== EXAMPLE: animated-theme-toggler-triangle-demo =====
+Title: Animated Theme Toggler — Triangle
+
+--- file: example/animated-theme-toggler-triangle-demo.tsx ---
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerTriangleDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="triangle" />
+ </div>
+ )
+}
+
+
+===== EXAMPLE: animated-theme-toggler-star-demo =====
+Title: Animated Theme Toggler — Star
+
+--- file: example/animated-theme-toggler-star-demo.tsx ---
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerStarDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="star" />
+ </div>
+ )
}
diff --git a/apps/www/public/llms.txt b/apps/www/public/llms.txt
index 5b06d6c2..b572884e 100644
--- a/apps/www/public/llms.txt
+++ b/apps/www/public/llms.txt
@@ -13,7 +13,7 @@ This file provides LLM-friendly entry points to documentation and examples.
- [Animated Grid Pattern](https://magicui.design/docs/components/animated-grid-pattern): A animated background grid pattern made with SVGs, fully customizable using Tailwind CSS.
- [Animated List](https://magicui.design/docs/components/animated-list): A list that animates each item in sequence with a delay. Used to showcase notifications or events on your landing page.
- [Animated Shiny Text](https://magicui.design/docs/components/animated-shiny-text): A light glare effect which pans across text making it appear as if it is shimmering.
-- [Theme Toggler](https://magicui.design/docs/components/animated-theme-toggler): A component for theme changing animation.
+- [Theme Toggler](https://magicui.design/docs/components/animated-theme-toggler): Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.
- [Aurora Text](https://magicui.design/docs/components/aurora-text): A beautiful aurora text effect
- [Avatar Circles](https://magicui.design/docs/components/avatar-circles): Overlapping circles of avatars.
- [Backlight](https://magicui.design/docs/components/backlight): A backlight glow effect for videos, images, and SVGs.
@@ -234,6 +234,12 @@ This file provides LLM-friendly entry points to documentation and examples.
- [Pixel Image Demo](https://github.com/magicuidesign/magicui/blob/main/example/pixel-image-demo.tsx): Example usage
- [Highlighter Demo](https://github.com/magicuidesign/magicui/blob/main/example/highlighter-demo.tsx): Example usage
- [Animated Theme Toggler Demo](https://github.com/magicuidesign/magicui/blob/main/example/animated-theme-toggler-demo.tsx): Example usage
+- [Animated Theme Toggler — Square](https://github.com/magicuidesign/magicui/blob/main/example/animated-theme-toggler-square-demo.tsx): Example usage
+- [Animated Theme Toggler — Diamond](https://github.com/magicuidesign/magicui/blob/main/example/animated-theme-toggler-diamond-demo.tsx): Example usage
+- [Animated Theme Toggler — Rectangle](https://github.com/magicuidesign/magicui/blob/main/example/animated-theme-toggler-rectangle-demo.tsx): Example usage
+- [Animated Theme Toggler — Hexagon](https://github.com/magicuidesign/magicui/blob/main/example/animated-theme-toggler-hexagon-demo.tsx): Example usage
+- [Animated Theme Toggler — Triangle](https://github.com/magicuidesign/magicui/blob/main/example/animated-theme-toggler-triangle-demo.tsx): Example usage
+- [Animated Theme Toggler — Star](https://github.com/magicuidesign/magicui/blob/main/example/animated-theme-toggler-star-demo.tsx): Example usage
- [light-rays-demo](https://github.com/magicuidesign/magicui/blob/main/example/light-rays-demo.tsx): Example usage
- [Dotted Map Demo](https://github.com/magicuidesign/magicui/blob/main/example/dotted-map-demo.tsx): Example usage
- [Dotted Map Demo 2](https://github.com/magicuidesign/magicui/blob/main/example/dotted-map-demo-2.tsx): Example usage
diff --git a/apps/www/public/r/animated-theme-toggler-demo.json b/apps/www/public/r/animated-theme-toggler-demo.json
index bbf87eec..51419cee 100644
--- a/apps/www/public/r/animated-theme-toggler-demo.json
+++ b/apps/www/public/r/animated-theme-toggler-demo.json
@@ -10,7 +10,7 @@
"files": [
{
"path": "registry/example/animated-theme-toggler-demo.tsx",
- "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerDemo() {\n return <AnimatedThemeToggler />\n}\n",
+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerDemo() {\n return (\n <div className=\"flex justify-center p-6\">\n <AnimatedThemeToggler />\n </div>\n )\n}\n",
"type": "registry:example"
}
]
diff --git a/apps/www/public/r/animated-theme-toggler-diamond-demo.json b/apps/www/public/r/animated-theme-toggler-diamond-demo.json
new file mode 100644
index 00000000..5da020e1
--- /dev/null
+++ b/apps/www/public/r/animated-theme-toggler-diamond-demo.json
@@ -0,0 +1,17 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "animated-theme-toggler-diamond-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Diamond",
+ "description": "Theme transition with a diamond-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-diamond-demo.tsx",
+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerDiamondDemo() {\n return (\n <div className=\"flex justify-center p-6\">\n <AnimatedThemeToggler variant=\"diamond\" />\n </div>\n )\n}\n",
+ "type": "registry:example"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/apps/www/public/r/animated-theme-toggler-hexagon-demo.json b/apps/www/public/r/animated-theme-toggler-hexagon-demo.json
new file mode 100644
index 00000000..8a524a51
--- /dev/null
+++ b/apps/www/public/r/animated-theme-toggler-hexagon-demo.json
@@ -0,0 +1,17 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "animated-theme-toggler-hexagon-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Hexagon",
+ "description": "Theme transition with a hexagon clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-hexagon-demo.tsx",
+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerHexagonDemo() {\n return (\n <div className=\"flex justify-center p-6\">\n <AnimatedThemeToggler variant=\"hexagon\" />\n </div>\n )\n}\n",
+ "type": "registry:example"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/apps/www/public/r/animated-theme-toggler-rectangle-demo.json b/apps/www/public/r/animated-theme-toggler-rectangle-demo.json
new file mode 100644
index 00000000..42b5034d
--- /dev/null
+++ b/apps/www/public/r/animated-theme-toggler-rectangle-demo.json
@@ -0,0 +1,17 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "animated-theme-toggler-rectangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Rectangle",
+ "description": "Theme transition with a rectangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-rectangle-demo.tsx",
+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerRectangleDemo() {\n return (\n <div className=\"flex justify-center p-6\">\n <AnimatedThemeToggler variant=\"rectangle\" />\n </div>\n )\n}\n",
+ "type": "registry:example"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/apps/www/public/r/animated-theme-toggler-square-demo.json b/apps/www/public/r/animated-theme-toggler-square-demo.json
new file mode 100644
index 00000000..9c88916c
--- /dev/null
+++ b/apps/www/public/r/animated-theme-toggler-square-demo.json
@@ -0,0 +1,17 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "animated-theme-toggler-square-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Square",
+ "description": "Theme transition with a square clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-square-demo.tsx",
+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerSquareDemo() {\n return (\n <div className=\"flex justify-center p-6\">\n <AnimatedThemeToggler variant=\"square\" />\n </div>\n )\n}\n",
+ "type": "registry:example"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/apps/www/public/r/animated-theme-toggler-star-demo.json b/apps/www/public/r/animated-theme-toggler-star-demo.json
new file mode 100644
index 00000000..36f3a2d8
--- /dev/null
+++ b/apps/www/public/r/animated-theme-toggler-star-demo.json
@@ -0,0 +1,17 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "animated-theme-toggler-star-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Star",
+ "description": "Theme transition with a star-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-star-demo.tsx",
+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerStarDemo() {\n return (\n <div className=\"flex justify-center p-6\">\n <AnimatedThemeToggler variant=\"star\" />\n </div>\n )\n}\n",
+ "type": "registry:example"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/apps/www/public/r/animated-theme-toggler-triangle-demo.json b/apps/www/public/r/animated-theme-toggler-triangle-demo.json
new file mode 100644
index 00000000..2e9c727b
--- /dev/null
+++ b/apps/www/public/r/animated-theme-toggler-triangle-demo.json
@@ -0,0 +1,17 @@
+{
+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",
+ "name": "animated-theme-toggler-triangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Triangle",
+ "description": "Theme transition with a triangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-triangle-demo.tsx",
+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerTriangleDemo() {\n return (\n <div className=\"flex justify-center p-6\">\n <AnimatedThemeToggler variant=\"triangle\" />\n </div>\n )\n}\n",
+ "type": "registry:example"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/apps/www/public/r/animated-theme-toggler.json b/apps/www/public/r/animated-theme-toggler.json
index 3e915ddf..769563c0 100644
--- a/apps/www/public/r/animated-theme-toggler.json
+++ b/apps/www/public/r/animated-theme-toggler.json
@@ -3,14 +3,14 @@
"name": "animated-theme-toggler",
"type": "registry:ui",
"title": "Theme Toggler",
- "description": "A component for theme changing animation.",
+ "description": "Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.",
"dependencies": [
"lucide-react"
],
"files": [
{
"path": "registry/magicui/animated-theme-toggler.tsx",
- "content": "\"use client\"\n\nimport { useCallback, useEffect, useRef, useState } from \"react\"\nimport { Moon, Sun } from \"lucide-react\"\nimport { flushSync } from \"react-dom\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface AnimatedThemeTogglerProps extends React.ComponentPropsWithoutRef<\"button\"> {\n duration?: number\n}\n\nexport const AnimatedThemeToggler = ({\n className,\n duration = 400,\n ...props\n}: AnimatedThemeTogglerProps) => {\n const [isDark, setIsDark] = useState(false)\n const buttonRef = useRef<HTMLButtonElement>(null)\n\n useEffect(() => {\n const updateTheme = () => {\n setIsDark(document.documentElement.classList.contains(\"dark\"))\n }\n\n updateTheme()\n\n const observer = new MutationObserver(updateTheme)\n observer.observe(document.documentElement, {\n attributes: true,\n attributeFilter: [\"class\"],\n })\n\n return () => observer.disconnect()\n }, [])\n\n const toggleTheme = useCallback(() => {\n const button = buttonRef.current\n if (!button) return\n\n const { top, left, width, height } = button.getBoundingClientRect()\n const x = left + width / 2\n const y = top + height / 2\n const viewportWidth = window.visualViewport?.width ?? window.innerWidth\n const viewportHeight = window.visualViewport?.height ?? window.innerHeight\n const maxRadius = Math.hypot(\n Math.max(x, viewportWidth - x),\n Math.max(y, viewportHeight - y)\n )\n\n const applyTheme = () => {\n const newTheme = !isDark\n setIsDark(newTheme)\n document.documentElement.classList.toggle(\"dark\")\n localStorage.setItem(\"theme\", newTheme ? \"dark\" : \"light\")\n }\n\n if (typeof document.startViewTransition !== \"function\") {\n applyTheme()\n return\n }\n\n const transition = document.startViewTransition(() => {\n flushSync(applyTheme)\n })\n\n const ready = transition?.ready\n if (ready && typeof ready.then === \"function\") {\n ready.then(() => {\n document.documentElement.animate(\n {\n clipPath: [\n `circle(0px at ${x}px ${y}px)`,\n `circle(${maxRadius}px at ${x}px ${y}px)`,\n ],\n },\n {\n duration,\n easing: \"ease-in-out\",\n pseudoElement: \"::view-transition-new(root)\",\n }\n )\n })\n }\n }, [isDark, duration])\n\n return (\n <button\n type=\"button\"\n ref={buttonRef}\n onClick={toggleTheme}\n className={cn(className)}\n {...props}\n >\n {isDark ? <Sun /> : <Moon />}\n <span className=\"sr-only\">Toggle theme</span>\n </button>\n )\n}\n",
+ "content": "\"use client\"\n\nimport { useCallback, useEffect, useRef, useState } from \"react\"\nimport { Moon, Sun } from \"lucide-react\"\nimport { flushSync } from \"react-dom\"\n\nimport { cn } from \"@/lib/utils\"\n\nexport type TransitionVariant =\n | \"circle\"\n | \"square\"\n | \"triangle\"\n | \"diamond\"\n | \"hexagon\"\n | \"rectangle\"\n | \"star\"\n\ninterface AnimatedThemeTogglerProps extends React.ComponentPropsWithoutRef<\"button\"> {\n duration?: number\n variant?: TransitionVariant\n /** When true, the transition expands from the viewport center instead of the button center. */\n fromCenter?: boolean\n}\n\nfunction polygonCollapsed(cx: number, cy: number, vertexCount: number): string {\n const pairs = Array.from(\n { length: vertexCount },\n () => `${cx}px ${cy}px`\n ).join(\", \")\n return `polygon(${pairs})`\n}\n\nfunction getThemeTransitionClipPaths(\n variant: TransitionVariant,\n cx: number,\n cy: number,\n maxRadius: number,\n viewportWidth: number,\n viewportHeight: number\n): [string, string] {\n switch (variant) {\n case \"circle\":\n return [\n `circle(0px at ${cx}px ${cy}px)`,\n `circle(${maxRadius}px at ${cx}px ${cy}px)`,\n ]\n case \"square\": {\n const halfW = Math.max(cx, viewportWidth - cx)\n const halfH = Math.max(cy, viewportHeight - cy)\n const halfSide = Math.max(halfW, halfH) * 1.05\n const end = [\n `${cx - halfSide}px ${cy - halfSide}px`,\n `${cx + halfSide}px ${cy - halfSide}px`,\n `${cx + halfSide}px ${cy + halfSide}px`,\n `${cx - halfSide}px ${cy + halfSide}px`,\n ].join(\", \")\n return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]\n }\n case \"triangle\": {\n const scale = maxRadius * 2.2\n const dx = (Math.sqrt(3) / 2) * scale\n const verts = [\n `${cx}px ${cy - scale}px`,\n `${cx + dx}px ${cy + 0.5 * scale}px`,\n `${cx - dx}px ${cy + 0.5 * scale}px`,\n ].join(\", \")\n return [polygonCollapsed(cx, cy, 3), `polygon(${verts})`]\n }\n case \"diamond\": {\n // Slightly larger than the view-transition circle radius so axis-aligned coverage matches the circle reveal.\n const R = maxRadius * Math.SQRT2\n const end = [\n `${cx}px ${cy - R}px`,\n `${cx + R}px ${cy}px`,\n `${cx}px ${cy + R}px`,\n `${cx - R}px ${cy}px`,\n ].join(\", \")\n return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]\n }\n case \"hexagon\": {\n const R = maxRadius * Math.SQRT2\n const verts: string[] = []\n for (let i = 0; i < 6; i++) {\n const a = -Math.PI / 2 + (i * Math.PI) / 3\n verts.push(`${cx + R * Math.cos(a)}px ${cy + R * Math.sin(a)}px`)\n }\n return [polygonCollapsed(cx, cy, 6), `polygon(${verts.join(\", \")})`]\n }\n case \"rectangle\": {\n const halfW = Math.max(cx, viewportWidth - cx)\n const halfH = Math.max(cy, viewportHeight - cy)\n const end = [\n `${cx - halfW}px ${cy - halfH}px`,\n `${cx + halfW}px ${cy - halfH}px`,\n `${cx + halfW}px ${cy + halfH}px`,\n `${cx - halfW}px ${cy + halfH}px`,\n ].join(\", \")\n return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]\n }\n case \"star\": {\n // Small overscan so the last frames never leave a 1px seam before the transition group ends.\n const R = maxRadius * Math.SQRT2 * 1.03\n const innerRatio = 0.42\n const starPolygon = (radius: number) => {\n const verts: string[] = []\n for (let i = 0; i < 5; i++) {\n const outerA = -Math.PI / 2 + (i * 2 * Math.PI) / 5\n verts.push(\n `${cx + radius * Math.cos(outerA)}px ${cy + radius * Math.sin(outerA)}px`\n )\n const innerA = outerA + Math.PI / 5\n verts.push(\n `${cx + radius * innerRatio * Math.cos(innerA)}px ${cy + radius * innerRatio * Math.sin(innerA)}px`\n )\n }\n return `polygon(${verts.join(\", \")})`\n }\n const startR = Math.max(2, R * 0.025)\n return [starPolygon(startR), starPolygon(R)]\n }\n default:\n return [\n `circle(0px at ${cx}px ${cy}px)`,\n `circle(${maxRadius}px at ${cx}px ${cy}px)`,\n ]\n }\n}\n\nexport const AnimatedThemeToggler = ({\n className,\n duration = 400,\n variant,\n fromCenter = false,\n ...props\n}: AnimatedThemeTogglerProps) => {\n const shape = variant ?? \"circle\"\n const [isDark, setIsDark] = useState(false)\n const buttonRef = useRef<HTMLButtonElement>(null)\n\n useEffect(() => {\n const updateTheme = () => {\n setIsDark(document.documentElement.classList.contains(\"dark\"))\n }\n\n updateTheme()\n\n const observer = new MutationObserver(updateTheme)\n observer.observe(document.documentElement, {\n attributes: true,\n attributeFilter: [\"class\"],\n })\n\n return () => observer.disconnect()\n }, [])\n\n const toggleTheme = useCallback(() => {\n const button = buttonRef.current\n if (!button) return\n\n const viewportWidth = window.visualViewport?.width ?? window.innerWidth\n const viewportHeight = window.visualViewport?.height ?? window.innerHeight\n\n let x: number\n let y: number\n if (fromCenter) {\n x = viewportWidth / 2\n y = viewportHeight / 2\n } else {\n const { top, left, width, height } = button.getBoundingClientRect()\n x = left + width / 2\n y = top + height / 2\n }\n\n const maxRadius = Math.hypot(\n Math.max(x, viewportWidth - x),\n Math.max(y, viewportHeight - y)\n )\n\n const applyTheme = () => {\n const newTheme = !isDark\n setIsDark(newTheme)\n document.documentElement.classList.toggle(\"dark\")\n localStorage.setItem(\"theme\", newTheme ? \"dark\" : \"light\")\n }\n\n if (typeof document.startViewTransition !== \"function\") {\n applyTheme()\n return\n }\n\n const root = document.documentElement\n root.dataset.magicuiThemeVt = \"active\"\n root.style.setProperty(\n \"--magicui-theme-toggle-vt-duration\",\n `${duration}ms`\n )\n const cleanup = () => {\n delete root.dataset.magicuiThemeVt\n root.style.removeProperty(\"--magicui-theme-toggle-vt-duration\")\n }\n\n const transition = document.startViewTransition(() => {\n flushSync(applyTheme)\n })\n if (typeof transition?.finished?.finally === \"function\") {\n transition.finished.finally(cleanup)\n } else {\n cleanup()\n }\n\n const ready = transition?.ready\n if (ready && typeof ready.then === \"function\") {\n const clipPath = getThemeTransitionClipPaths(\n shape,\n x,\n y,\n maxRadius,\n viewportWidth,\n viewportHeight\n )\n ready.then(() => {\n document.documentElement.animate(\n {\n clipPath,\n },\n {\n duration,\n // Star: linear avoids easing overshoot that fights polygon interpolation at t→1; VT group duration is synced above.\n easing: shape === \"star\" ? \"linear\" : \"ease-in-out\",\n fill: \"forwards\",\n pseudoElement: \"::view-transition-new(root)\",\n }\n )\n })\n }\n }, [shape, fromCenter, duration, isDark])\n\n return (\n <button\n type=\"button\"\n ref={buttonRef}\n onClick={toggleTheme}\n className={cn(className)}\n {...props}\n >\n {isDark ? <Sun /> : <Moon />}\n <span className=\"sr-only\">Toggle theme</span>\n </button>\n )\n}\n",
"type": "registry:ui"
}
],
diff --git a/apps/www/public/r/registry.json b/apps/www/public/r/registry.json
index c6b1a0d6..94bdd805 100644
--- a/apps/www/public/r/registry.json
+++ b/apps/www/public/r/registry.json
@@ -1260,7 +1260,7 @@
"name": "animated-theme-toggler",
"type": "registry:ui",
"title": "Theme Toggler",
- "description": "A component for theme changing animation.",
+ "description": "Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.",
"dependencies": [
"lucide-react"
],
@@ -3651,6 +3651,96 @@
}
]
},
+ {
+ "name": "animated-theme-toggler-square-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Square",
+ "description": "Theme transition with a square clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-square-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-diamond-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Diamond",
+ "description": "Theme transition with a diamond-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-diamond-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-rectangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Rectangle",
+ "description": "Theme transition with a rectangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-rectangle-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-hexagon-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Hexagon",
+ "description": "Theme transition with a hexagon clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-hexagon-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-triangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Triangle",
+ "description": "Theme transition with a triangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-triangle-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-star-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Star",
+ "description": "Theme transition with a star-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-star-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
{
"name": "light-rays-demo",
"type": "registry:example",
diff --git a/apps/www/public/registry.json b/apps/www/public/registry.json
index c6b1a0d6..94bdd805 100644
--- a/apps/www/public/registry.json
+++ b/apps/www/public/registry.json
@@ -1260,7 +1260,7 @@
"name": "animated-theme-toggler",
"type": "registry:ui",
"title": "Theme Toggler",
- "description": "A component for theme changing animation.",
+ "description": "Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.",
"dependencies": [
"lucide-react"
],
@@ -3651,6 +3651,96 @@
}
]
},
+ {
+ "name": "animated-theme-toggler-square-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Square",
+ "description": "Theme transition with a square clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-square-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-diamond-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Diamond",
+ "description": "Theme transition with a diamond-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-diamond-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-rectangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Rectangle",
+ "description": "Theme transition with a rectangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-rectangle-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-hexagon-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Hexagon",
+ "description": "Theme transition with a hexagon clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-hexagon-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-triangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Triangle",
+ "description": "Theme transition with a triangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-triangle-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-star-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Star",
+ "description": "Theme transition with a star-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-star-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
{
"name": "light-rays-demo",
"type": "registry:example",
diff --git a/apps/www/registry.json b/apps/www/registry.json
index c6b1a0d6..94bdd805 100644
--- a/apps/www/registry.json
+++ b/apps/www/registry.json
@@ -1260,7 +1260,7 @@
"name": "animated-theme-toggler",
"type": "registry:ui",
"title": "Theme Toggler",
- "description": "A component for theme changing animation.",
+ "description": "Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.",
"dependencies": [
"lucide-react"
],
@@ -3651,6 +3651,96 @@
}
]
},
+ {
+ "name": "animated-theme-toggler-square-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Square",
+ "description": "Theme transition with a square clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-square-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-diamond-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Diamond",
+ "description": "Theme transition with a diamond-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-diamond-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-rectangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Rectangle",
+ "description": "Theme transition with a rectangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-rectangle-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-hexagon-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Hexagon",
+ "description": "Theme transition with a hexagon clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-hexagon-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-triangle-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Triangle",
+ "description": "Theme transition with a triangle clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-triangle-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
+ {
+ "name": "animated-theme-toggler-star-demo",
+ "type": "registry:example",
+ "title": "Animated Theme Toggler — Star",
+ "description": "Theme transition with a star-shaped clip reveal.",
+ "registryDependencies": [
+ "@magicui/animated-theme-toggler"
+ ],
+ "files": [
+ {
+ "path": "registry/example/animated-theme-toggler-star-demo.tsx",
+ "type": "registry:example"
+ }
+ ]
+ },
{
"name": "light-rays-demo",
"type": "registry:example",
diff --git a/apps/www/registry/__index__.tsx b/apps/www/registry/__index__.tsx
index b57947f6..f84b4092 100644
--- a/apps/www/registry/__index__.tsx
+++ b/apps/www/registry/__index__.tsx
@@ -1207,7 +1207,7 @@ export const Index: Record<string, any> = {
},
"animated-theme-toggler": {
name: "animated-theme-toggler",
- description: "A component for theme changing animation.",
+ description: "Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.",
type: "registry:ui",
registryDependencies: undefined,
files: [{
@@ -3844,6 +3844,108 @@ export const Index: Record<string, any> = {
}),
meta: undefined,
},
+ "animated-theme-toggler-square-demo": {
+ name: "animated-theme-toggler-square-demo",
+ description: "Theme transition with a square clip reveal.",
+ type: "registry:example",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [{
+ path: "registry/example/animated-theme-toggler-square-demo.tsx",
+ type: "registry:example",
+ target: ""
+ }],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/example/animated-theme-toggler-square-demo.tsx")
+ const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') ?? item.name
+ return { default: mod.default ?? mod[exportName] }
+ }),
+ meta: undefined,
+ },
+ "animated-theme-toggler-diamond-demo": {
+ name: "animated-theme-toggler-diamond-demo",
+ description: "Theme transition with a diamond-shaped clip reveal.",
+ type: "registry:example",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [{
+ path: "registry/example/animated-theme-toggler-diamond-demo.tsx",
+ type: "registry:example",
+ target: ""
+ }],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/example/animated-theme-toggler-diamond-demo.tsx")
+ const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') ?? item.name
+ return { default: mod.default ?? mod[exportName] }
+ }),
+ meta: undefined,
+ },
+ "animated-theme-toggler-rectangle-demo": {
+ name: "animated-theme-toggler-rectangle-demo",
+ description: "Theme transition with a rectangle clip reveal.",
+ type: "registry:example",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [{
+ path: "registry/example/animated-theme-toggler-rectangle-demo.tsx",
+ type: "registry:example",
+ target: ""
+ }],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/example/animated-theme-toggler-rectangle-demo.tsx")
+ const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') ?? item.name
+ return { default: mod.default ?? mod[exportName] }
+ }),
+ meta: undefined,
+ },
+ "animated-theme-toggler-hexagon-demo": {
+ name: "animated-theme-toggler-hexagon-demo",
+ description: "Theme transition with a hexagon clip reveal.",
+ type: "registry:example",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [{
+ path: "registry/example/animated-theme-toggler-hexagon-demo.tsx",
+ type: "registry:example",
+ target: ""
+ }],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/example/animated-theme-toggler-hexagon-demo.tsx")
+ const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') ?? item.name
+ return { default: mod.default ?? mod[exportName] }
+ }),
+ meta: undefined,
+ },
+ "animated-theme-toggler-triangle-demo": {
+ name: "animated-theme-toggler-triangle-demo",
+ description: "Theme transition with a triangle clip reveal.",
+ type: "registry:example",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [{
+ path: "registry/example/animated-theme-toggler-triangle-demo.tsx",
+ type: "registry:example",
+ target: ""
+ }],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/example/animated-theme-toggler-triangle-demo.tsx")
+ const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') ?? item.name
+ return { default: mod.default ?? mod[exportName] }
+ }),
+ meta: undefined,
+ },
+ "animated-theme-toggler-star-demo": {
+ name: "animated-theme-toggler-star-demo",
+ description: "Theme transition with a star-shaped clip reveal.",
+ type: "registry:example",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [{
+ path: "registry/example/animated-theme-toggler-star-demo.tsx",
+ type: "registry:example",
+ target: ""
+ }],
+ component: React.lazy(async () => {
+ const mod = await import("@/registry/example/animated-theme-toggler-star-demo.tsx")
+ const exportName = Object.keys(mod).find(key => typeof mod[key] === 'function' || typeof mod[key] === 'object') ?? item.name
+ return { default: mod.default ?? mod[exportName] }
+ }),
+ meta: undefined,
+ },
"light-rays-demo": {
name: "light-rays-demo",
description: "Demo of the light-rays component showcasing animated light rays",
diff --git a/apps/www/registry/example/animated-theme-toggler-demo.tsx b/apps/www/registry/example/animated-theme-toggler-demo.tsx
index f3c19653..8e82816f 100644
--- a/apps/www/registry/example/animated-theme-toggler-demo.tsx
+++ b/apps/www/registry/example/animated-theme-toggler-demo.tsx
@@ -1,5 +1,9 @@
import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
export default function AnimatedThemeTogglerDemo() {
- return <AnimatedThemeToggler />
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler />
+ </div>
+ )
}
diff --git a/apps/www/registry/example/animated-theme-toggler-diamond-demo.tsx b/apps/www/registry/example/animated-theme-toggler-diamond-demo.tsx
new file mode 100644
index 00000000..b11c771f
--- /dev/null
+++ b/apps/www/registry/example/animated-theme-toggler-diamond-demo.tsx
@@ -0,0 +1,9 @@
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerDiamondDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="diamond" />
+ </div>
+ )
+}
diff --git a/apps/www/registry/example/animated-theme-toggler-hexagon-demo.tsx b/apps/www/registry/example/animated-theme-toggler-hexagon-demo.tsx
new file mode 100644
index 00000000..975fa253
--- /dev/null
+++ b/apps/www/registry/example/animated-theme-toggler-hexagon-demo.tsx
@@ -0,0 +1,9 @@
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerHexagonDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="hexagon" />
+ </div>
+ )
+}
diff --git a/apps/www/registry/example/animated-theme-toggler-rectangle-demo.tsx b/apps/www/registry/example/animated-theme-toggler-rectangle-demo.tsx
new file mode 100644
index 00000000..a7c60cff
--- /dev/null
+++ b/apps/www/registry/example/animated-theme-toggler-rectangle-demo.tsx
@@ -0,0 +1,9 @@
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerRectangleDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="rectangle" />
+ </div>
+ )
+}
diff --git a/apps/www/registry/example/animated-theme-toggler-square-demo.tsx b/apps/www/registry/example/animated-theme-toggler-square-demo.tsx
new file mode 100644
index 00000000..ea8ad87e
--- /dev/null
+++ b/apps/www/registry/example/animated-theme-toggler-square-demo.tsx
@@ -0,0 +1,9 @@
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerSquareDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="square" />
+ </div>
+ )
+}
diff --git a/apps/www/registry/example/animated-theme-toggler-star-demo.tsx b/apps/www/registry/example/animated-theme-toggler-star-demo.tsx
new file mode 100644
index 00000000..8a9c53d5
--- /dev/null
+++ b/apps/www/registry/example/animated-theme-toggler-star-demo.tsx
@@ -0,0 +1,9 @@
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerStarDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="star" />
+ </div>
+ )
+}
diff --git a/apps/www/registry/example/animated-theme-toggler-triangle-demo.tsx b/apps/www/registry/example/animated-theme-toggler-triangle-demo.tsx
new file mode 100644
index 00000000..c0284776
--- /dev/null
+++ b/apps/www/registry/example/animated-theme-toggler-triangle-demo.tsx
@@ -0,0 +1,9 @@
+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"
+
+export default function AnimatedThemeTogglerTriangleDemo() {
+ return (
+ <div className="flex justify-center p-6">
+ <AnimatedThemeToggler variant="triangle" />
+ </div>
+ )
+}
diff --git a/apps/www/registry/magicui/animated-theme-toggler.tsx b/apps/www/registry/magicui/animated-theme-toggler.tsx
index 1515337a..ec26352a 100644
--- a/apps/www/registry/magicui/animated-theme-toggler.tsx
+++ b/apps/www/registry/magicui/animated-theme-toggler.tsx
@@ -6,15 +6,134 @@ import { flushSync } from "react-dom"
import { cn } from "@/lib/utils"
+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
+}
+
+function polygonCollapsed(cx: number, cy: number, vertexCount: number): string {
+ const pairs = Array.from(
+ { length: vertexCount },
+ () => `${cx}px ${cy}px`
+ ).join(", ")
+ return `polygon(${pairs})`
+}
+
+function getThemeTransitionClipPaths(
+ variant: TransitionVariant,
+ cx: number,
+ cy: number,
+ maxRadius: number,
+ viewportWidth: number,
+ viewportHeight: number
+): [string, string] {
+ switch (variant) {
+ case "circle":
+ return [
+ `circle(0px at ${cx}px ${cy}px)`,
+ `circle(${maxRadius}px at ${cx}px ${cy}px)`,
+ ]
+ case "square": {
+ const halfW = Math.max(cx, viewportWidth - cx)
+ const halfH = Math.max(cy, viewportHeight - cy)
+ const halfSide = Math.max(halfW, halfH) * 1.05
+ const end = [
+ `${cx - halfSide}px ${cy - halfSide}px`,
+ `${cx + halfSide}px ${cy - halfSide}px`,
+ `${cx + halfSide}px ${cy + halfSide}px`,
+ `${cx - halfSide}px ${cy + halfSide}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
+ }
+ case "triangle": {
+ const scale = maxRadius * 2.2
+ const dx = (Math.sqrt(3) / 2) * scale
+ const verts = [
+ `${cx}px ${cy - scale}px`,
+ `${cx + dx}px ${cy + 0.5 * scale}px`,
+ `${cx - dx}px ${cy + 0.5 * scale}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 3), `polygon(${verts})`]
+ }
+ case "diamond": {
+ // Slightly larger than the view-transition circle radius so axis-aligned coverage matches the circle reveal.
+ const R = maxRadius * Math.SQRT2
+ const end = [
+ `${cx}px ${cy - R}px`,
+ `${cx + R}px ${cy}px`,
+ `${cx}px ${cy + R}px`,
+ `${cx - R}px ${cy}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
+ }
+ case "hexagon": {
+ const R = maxRadius * Math.SQRT2
+ const verts: string[] = []
+ for (let i = 0; i < 6; i++) {
+ const a = -Math.PI / 2 + (i * Math.PI) / 3
+ verts.push(`${cx + R * Math.cos(a)}px ${cy + R * Math.sin(a)}px`)
+ }
+ return [polygonCollapsed(cx, cy, 6), `polygon(${verts.join(", ")})`]
+ }
+ case "rectangle": {
+ const halfW = Math.max(cx, viewportWidth - cx)
+ const halfH = Math.max(cy, viewportHeight - cy)
+ const end = [
+ `${cx - halfW}px ${cy - halfH}px`,
+ `${cx + halfW}px ${cy - halfH}px`,
+ `${cx + halfW}px ${cy + halfH}px`,
+ `${cx - halfW}px ${cy + halfH}px`,
+ ].join(", ")
+ return [polygonCollapsed(cx, cy, 4), `polygon(${end})`]
+ }
+ case "star": {
+ // Small overscan so the last frames never leave a 1px seam before the transition group ends.
+ const R = maxRadius * Math.SQRT2 * 1.03
+ const innerRatio = 0.42
+ const starPolygon = (radius: number) => {
+ const verts: string[] = []
+ for (let i = 0; i < 5; i++) {
+ const outerA = -Math.PI / 2 + (i * 2 * Math.PI) / 5
+ verts.push(
+ `${cx + radius * Math.cos(outerA)}px ${cy + radius * Math.sin(outerA)}px`
+ )
+ const innerA = outerA + Math.PI / 5
+ verts.push(
+ `${cx + radius * innerRatio * Math.cos(innerA)}px ${cy + radius * innerRatio * Math.sin(innerA)}px`
+ )
+ }
+ return `polygon(${verts.join(", ")})`
+ }
+ const startR = Math.max(2, R * 0.025)
+ return [starPolygon(startR), starPolygon(R)]
+ }
+ default:
+ return [
+ `circle(0px at ${cx}px ${cy}px)`,
+ `circle(${maxRadius}px at ${cx}px ${cy}px)`,
+ ]
+ }
}
export const AnimatedThemeToggler = ({
className,
duration = 400,
+ variant,
+ fromCenter = false,
...props
}: AnimatedThemeTogglerProps) => {
+ const shape = variant ?? "circle"
const [isDark, setIsDark] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null)
@@ -38,11 +157,20 @@ export const AnimatedThemeToggler = ({
const button = buttonRef.current
if (!button) return
- const { top, left, width, height } = button.getBoundingClientRect()
- const x = left + width / 2
- const y = top + height / 2
const viewportWidth = window.visualViewport?.width ?? window.innerWidth
const viewportHeight = window.visualViewport?.height ?? window.innerHeight
+
+ let x: number
+ let y: number
+ if (fromCenter) {
+ x = viewportWidth / 2
+ y = viewportHeight / 2
+ } else {
+ const { top, left, width, height } = button.getBoundingClientRect()
+ x = left + width / 2
+ y = top + height / 2
+ }
+
const maxRadius = Math.hypot(
Math.max(x, viewportWidth - x),
Math.max(y, viewportHeight - y)
@@ -60,29 +188,52 @@ export const AnimatedThemeToggler = ({
return
}
+ const root = document.documentElement
+ root.dataset.magicuiThemeVt = "active"
+ root.style.setProperty(
+ "--magicui-theme-toggle-vt-duration",
+ `${duration}ms`
+ )
+ const cleanup = () => {
+ delete root.dataset.magicuiThemeVt
+ root.style.removeProperty("--magicui-theme-toggle-vt-duration")
+ }
+
const transition = document.startViewTransition(() => {
flushSync(applyTheme)
})
+ if (typeof transition?.finished?.finally === "function") {
+ transition.finished.finally(cleanup)
+ } else {
+ cleanup()
+ }
const ready = transition?.ready
if (ready && typeof ready.then === "function") {
+ const clipPath = getThemeTransitionClipPaths(
+ shape,
+ x,
+ y,
+ maxRadius,
+ viewportWidth,
+ viewportHeight
+ )
ready.then(() => {
document.documentElement.animate(
{
- clipPath: [
- `circle(0px at ${x}px ${y}px)`,
- `circle(${maxRadius}px at ${x}px ${y}px)`,
- ],
+ clipPath,
},
{
duration,
- easing: "ease-in-out",
+ // Star: linear avoids easing overshoot that fights polygon interpolation at t→1; VT group duration is synced above.
+ easing: shape === "star" ? "linear" : "ease-in-out",
+ fill: "forwards",
pseudoElement: "::view-transition-new(root)",
}
)
})
}
- }, [isDark, duration])
+ }, [shape, fromCenter, duration, isDark])
return (
<button
diff --git a/apps/www/registry/registry-examples.ts b/apps/www/registry/registry-examples.ts
index 8a741ee3..d3a5b3a2 100644
--- a/apps/www/registry/registry-examples.ts
+++ b/apps/www/registry/registry-examples.ts
@@ -2010,6 +2010,84 @@ export const examples: Registry["items"] = [
},
],
},
+ {
+ name: "animated-theme-toggler-square-demo",
+ type: "registry:example",
+ title: "Animated Theme Toggler — Square",
+ description: "Theme transition with a square clip reveal.",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [
+ {
+ path: "example/animated-theme-toggler-square-demo.tsx",
+ type: "registry:example",
+ },
+ ],
+ },
+ {
+ name: "animated-theme-toggler-diamond-demo",
+ type: "registry:example",
+ title: "Animated Theme Toggler — Diamond",
+ description: "Theme transition with a diamond-shaped clip reveal.",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [
+ {
+ path: "example/animated-theme-toggler-diamond-demo.tsx",
+ type: "registry:example",
+ },
+ ],
+ },
+ {
+ name: "animated-theme-toggler-rectangle-demo",
+ type: "registry:example",
+ title: "Animated Theme Toggler — Rectangle",
+ description: "Theme transition with a rectangle clip reveal.",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [
+ {
+ path: "example/animated-theme-toggler-rectangle-demo.tsx",
+ type: "registry:example",
+ },
+ ],
+ },
+ {
+ name: "animated-theme-toggler-hexagon-demo",
+ type: "registry:example",
+ title: "Animated Theme Toggler — Hexagon",
+ description: "Theme transition with a hexagon clip reveal.",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [
+ {
+ path: "example/animated-theme-toggler-hexagon-demo.tsx",
+ type: "registry:example",
+ },
+ ],
+ },
+ {
+ name: "animated-theme-toggler-triangle-demo",
+ type: "registry:example",
+ title: "Animated Theme Toggler — Triangle",
+ description: "Theme transition with a triangle clip reveal.",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [
+ {
+ path: "example/animated-theme-toggler-triangle-demo.tsx",
+ type: "registry:example",
+ },
+ ],
+ },
+ {
+ name: "animated-theme-toggler-star-demo",
+ type: "registry:example",
+ title: "Animated Theme Toggler — Star",
+ description: "Theme transition with a star-shaped clip reveal.",
+ registryDependencies: ["@magicui/animated-theme-toggler"],
+ files: [
+ {
+ path: "example/animated-theme-toggler-star-demo.tsx",
+ type: "registry:example",
+ },
+ ],
+ },
{
name: "light-rays-demo",
description:
diff --git a/apps/www/registry/registry-ui.ts b/apps/www/registry/registry-ui.ts
index 8630416d..2caf2513 100644
--- a/apps/www/registry/registry-ui.ts
+++ b/apps/www/registry/registry-ui.ts
@@ -1215,7 +1215,8 @@ export const ui: Registry["items"] = [
name: "animated-theme-toggler",
type: "registry:ui",
title: "Theme Toggler",
- description: "A component for theme changing animation.",
+ description:
+ "Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.",
dependencies: ["lucide-react"],
files: [
{
diff --git a/apps/www/styles/globals.css b/apps/www/styles/globals.css
index cea7b850..e109b3d0 100644
--- a/apps/www/styles/globals.css
+++ b/apps/www/styles/globals.css
@@ -441,6 +441,11 @@
animation: none;
mix-blend-mode: normal;
}
+
+ /* Scoped to AnimatedThemeToggler so other root view transitions stay unaffected. */
+ html[data-magicui-theme-vt="active"]::view-transition-group(root) {
+ animation-duration: var(--magicui-theme-toggle-vt-duration);
+ }
}
@utility no-scrollbar {