UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
Animated theme toggler (#643)
Nazam Kalsi687289cf82f3
diff --git a/__registry__/index.tsx b/__registry__/index.tsxindex 32693dda..6bd09263 100644--- a/__registry__/index.tsx+++ b/__registry__/index.tsx@@ -1697,6 +1697,29 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "animated-theme-toggler": {+ name: "animated-theme-toggler",+ description: "A component for theme changing animation.",+ type: "registry:ui",+ registryDependencies: undefined,+ files: [+ {+ path: "registry/magicui/animated-theme-toggler.tsx",+ type: "registry:ui",+ target: "components/magicui/animated-theme-toggler.tsx",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import("@/registry/magicui/animated-theme-toggler.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,+ },"magic-card-demo": {name: "magic-card-demo",description:@@ -4687,6 +4710,31 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "animated-theme-toggler-demo": {+ name: "animated-theme-toggler-demo",+ description: "Example showing animation while changing the theme.",+ type: "registry:example",+ registryDependencies: ["https://magicui.design/animated-theme-toggler"],+ files: [+ {+ path: "registry/example/animated-theme-toggler-demo.tsx",+ type: "registry:example",+ target: "components/animated-theme-toggler-demo.tsx",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import(+ "@/registry/example/animated-theme-toggler-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,+ },utils: {name: "utils",description: "",diff --git a/config/docs.ts b/config/docs.tsindex 63aaa0f5..b1ec814c 100644--- a/config/docs.ts+++ b/config/docs.ts@@ -218,6 +218,12 @@ export const docsConfig: DocsConfig = {items: [],label: "",},+ {+ title: "Animated Theme Toggler",+ href: `/docs/components/animated-theme-toggler`,+ items: [],+ label: "New",+ },{title: "File Tree",href: `/docs/components/file-tree`,diff --git a/content/docs/components/animated-theme-toggler.mdx b/content/docs/components/animated-theme-toggler.mdxnew file mode 100644index 00000000..2ad3de5e--- /dev/null+++ b/content/docs/components/animated-theme-toggler.mdx@@ -0,0 +1,71 @@+---+title: Theme Toggler+date: 2025-0-17+description: An Animated theme toggler component, fully customizable using Tailwind CSS.+author: Nazam Kalsi+published: true+---++<ComponentPreview name="animated-theme-toggler-demo" />++## Installation++<Tabs defaultValue="cli">++<TabsList>+ <TabsTrigger value="cli">CLI</TabsTrigger>+ <TabsTrigger value="manual">Manual</TabsTrigger>+</TabsList>+<TabsContent value="cli">++```bash+npx shadcn@latest add "https://magicui.design/r/animated-theme-toggler"+```++</TabsContent>++<TabsContent value="manual">++<Steps>++<Step>Copy and paste the following code into your project.</Step>++<ComponentSource name="animated-theme-toggler" />++<Step>Update the import paths to match your project setup.</Step>++<Step>Add the required CSS into your index.css</Step>++```css title="./index.css"+::view-transition-old(root),+::view-transition-new(root) {+ animation: none;+ mix-blend-mode: normal;+}+```++</Steps>++</TabsContent>++</Tabs>++## Usage++```tsx+import { AnimatedThemeToggler } from "@/components/magicui/animated-theme-toggler";+```++```tsx+<AnimatedThemeToggler />+```++## Props++| Prop | Type | Default | Description |+| ----------- | -------- | ------- | ----------------------------------------------- |+| `className` | `String` | " " | Additional classes to be added to the component |++## Credits++- Credit to [Nazam Kalsi](https://nazam-kalsi-portfolio.vercel.app)diff --git a/public/r/animated-theme-toggler-demo.json b/public/r/animated-theme-toggler-demo.jsonnew file mode 100644index 00000000..ee540769--- /dev/null+++ b/public/r/animated-theme-toggler-demo.json@@ -0,0 +1,18 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "animated-theme-toggler-demo",+ "type": "registry:example",+ "title": "Animated Theme Toggler Demo",+ "description": "Example showing animation while changing the theme.",+ "registryDependencies": [+ "https://magicui.design/animated-theme-toggler"+ ],+ "files": [+ {+ "path": "registry/example/animated-theme-toggler-demo.tsx",+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\";\n\nfunction AnimatedThemeTogglerDemo() {\n return (\n <div>\n <AnimatedThemeToggler />\n </div>\n );\n}\nexport default AnimatedThemeTogglerDemo;\n",+ "type": "registry:example",+ "target": "components/animated-theme-toggler-demo.tsx"+ }+ ]+}\ No newline at end of filediff --git a/public/r/animated-theme-toggler.json b/public/r/animated-theme-toggler.jsonnew file mode 100644index 00000000..71498ccb--- /dev/null+++ b/public/r/animated-theme-toggler.json@@ -0,0 +1,24 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "animated-theme-toggler",+ "type": "registry:ui",+ "title": "Theme Toggler",+ "description": "A component for theme changing animation.",+ "dependencies": [+ "lucide-react"+ ],+ "files": [+ {+ "path": "registry/magicui/animated-theme-toggler.tsx",+ "content": "\"use client\";\n\nimport { Moon, SunDim } from \"lucide-react\";\nimport { useState, useRef } from \"react\";\nimport { flushSync } from \"react-dom\";\nimport { cn } from \"@/lib/utils\";\n\ntype props = {\n className?: string;\n};\n\nexport const AnimatedThemeToggler = ({ className }: props) => {\n const [isDarkMode, setIsDarkMode] = useState<boolean>(false);\n const buttonRef = useRef<HTMLButtonElement | null>(null);\n const changeTheme = async () => {\n if (!buttonRef.current) return;\n\n await document.startViewTransition(() => {\n flushSync(() => {\n const dark = document.documentElement.classList.toggle(\"dark\");\n setIsDarkMode(dark);\n });\n }).ready;\n\n const { top, left, width, height } =\n buttonRef.current.getBoundingClientRect();\n const y = top + height / 2;\n const x = left + width / 2;\n\n const right = window.innerWidth - left;\n const bottom = window.innerHeight - top;\n const maxRad = Math.hypot(Math.max(left, right), Math.max(top, bottom));\n\n document.documentElement.animate(\n {\n clipPath: [\n `circle(0px at ${x}px ${y}px)`,\n `circle(${maxRad}px at ${x}px ${y}px)`,\n ],\n },\n {\n duration: 700,\n easing: \"ease-in-out\",\n pseudoElement: \"::view-transition-new(root)\",\n },\n );\n };\n return (\n <button ref={buttonRef} onClick={changeTheme} className={cn(className)}>\n {isDarkMode ? <SunDim /> : <Moon />}\n </button>\n );\n};\n",+ "type": "registry:ui",+ "target": "components/magicui/animated-theme-toggler.tsx"+ }+ ],+ "css": {+ "::view-transition-old(root), ::view-transition-new(root)": {+ "animation": "none",+ "mix-blend-mode": "normal"+ }+ }+}\ No newline at end of filediff --git a/public/r/theme-toggler-demo.json b/public/r/theme-toggler-demo.jsonnew file mode 100644index 00000000..be41707a--- /dev/null+++ b/public/r/theme-toggler-demo.json@@ -0,0 +1,16 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "theme-toggler-demo",+ "type": "registry:example",+ "description": "Example showing animation in changing the theme.",+ "registryDependencies": [+ "animated-theme-toggler"+ ],+ "files": [+ {+ "path": "registry/example/animated-theme-toggler-demo.tsx",+ "content": "import ThemeToggler from '@/registry/magicui/animated-theme-toggler';\r\n\r\nfunction AnimatedThemeTogglerDemo() {\r\n return (\r\n <div className='flex justify-center items-center'>\r\n <ThemeToggler/>\r\n </div>\r\n )\r\n}\r\nexport default AnimatedThemeTogglerDemo;",+ "type": "registry:example"+ }+ ]+}\ No newline at end of filediff --git a/public/registry.json b/public/registry.jsonindex 04aed234..6a34c010 100644--- a/public/registry.json+++ b/public/registry.json@@ -1341,6 +1341,28 @@}]},+ {+ "name": "animated-theme-toggler",+ "type": "registry:ui",+ "title": "Theme Toggler",+ "description": "A component for theme changing animation.",+ "dependencies": [+ "lucide-react"+ ],+ "files": [+ {+ "path": "registry/magicui/animated-theme-toggler.tsx",+ "type": "registry:ui",+ "target": "components/magicui/animated-theme-toggler.tsx"+ }+ ],+ "css": {+ "::view-transition-old(root), ::view-transition-new(root)": {+ "animation": "none",+ "mix-blend-mode": "normal"+ }+ }+ },{"name": "magic-card-demo","type": "registry:example",@@ -3417,6 +3439,22 @@}]},+ {+ "name": "animated-theme-toggler-demo",+ "type": "registry:example",+ "title": "Animated Theme Toggler Demo",+ "description": "Example showing animation while changing the theme.",+ "registryDependencies": [+ "https://magicui.design/animated-theme-toggler"+ ],+ "files": [+ {+ "path": "registry/example/animated-theme-toggler-demo.tsx",+ "type": "registry:example",+ "target": "components/animated-theme-toggler-demo.tsx"+ }+ ]+ },{"name": "utils","type": "registry:lib",diff --git a/registry.json b/registry.jsonindex 04aed234..6a34c010 100644--- a/registry.json+++ b/registry.json@@ -1341,6 +1341,28 @@}]},+ {+ "name": "animated-theme-toggler",+ "type": "registry:ui",+ "title": "Theme Toggler",+ "description": "A component for theme changing animation.",+ "dependencies": [+ "lucide-react"+ ],+ "files": [+ {+ "path": "registry/magicui/animated-theme-toggler.tsx",+ "type": "registry:ui",+ "target": "components/magicui/animated-theme-toggler.tsx"+ }+ ],+ "css": {+ "::view-transition-old(root), ::view-transition-new(root)": {+ "animation": "none",+ "mix-blend-mode": "normal"+ }+ }+ },{"name": "magic-card-demo","type": "registry:example",@@ -3417,6 +3439,22 @@}]},+ {+ "name": "animated-theme-toggler-demo",+ "type": "registry:example",+ "title": "Animated Theme Toggler Demo",+ "description": "Example showing animation while changing the theme.",+ "registryDependencies": [+ "https://magicui.design/animated-theme-toggler"+ ],+ "files": [+ {+ "path": "registry/example/animated-theme-toggler-demo.tsx",+ "type": "registry:example",+ "target": "components/animated-theme-toggler-demo.tsx"+ }+ ]+ },{"name": "utils","type": "registry:lib",diff --git a/registry/example/animated-theme-toggler-demo.tsx b/registry/example/animated-theme-toggler-demo.tsxnew file mode 100644index 00000000..b2f49b26--- /dev/null+++ b/registry/example/animated-theme-toggler-demo.tsx@@ -0,0 +1,10 @@+import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler";++function AnimatedThemeTogglerDemo() {+ return (+ <div>+ <AnimatedThemeToggler />+ </div>+ );+}+export default AnimatedThemeTogglerDemo;diff --git a/registry/magicui/animated-theme-toggler.tsx b/registry/magicui/animated-theme-toggler.tsxnew file mode 100644index 00000000..47b541b9--- /dev/null+++ b/registry/magicui/animated-theme-toggler.tsx@@ -0,0 +1,53 @@+"use client";++import { Moon, SunDim } from "lucide-react";+import { useState, useRef } from "react";+import { flushSync } from "react-dom";+import { cn } from "@/lib/utils";++type props = {+ className?: string;+};++export const AnimatedThemeToggler = ({ className }: props) => {+ const [isDarkMode, setIsDarkMode] = useState<boolean>(false);+ const buttonRef = useRef<HTMLButtonElement | null>(null);+ const changeTheme = async () => {+ if (!buttonRef.current) return;++ await document.startViewTransition(() => {+ flushSync(() => {+ const dark = document.documentElement.classList.toggle("dark");+ setIsDarkMode(dark);+ });+ }).ready;++ const { top, left, width, height } =+ buttonRef.current.getBoundingClientRect();+ const y = top + height / 2;+ const x = left + width / 2;++ const right = window.innerWidth - left;+ const bottom = window.innerHeight - top;+ const maxRad = Math.hypot(Math.max(left, right), Math.max(top, bottom));++ document.documentElement.animate(+ {+ clipPath: [+ `circle(0px at ${x}px ${y}px)`,+ `circle(${maxRad}px at ${x}px ${y}px)`,+ ],+ },+ {+ duration: 700,+ easing: "ease-in-out",+ pseudoElement: "::view-transition-new(root)",+ },+ );+ };+ return (+ <button ref={buttonRef} onClick={changeTheme} className={cn(className)}>+ {isDarkMode ? <SunDim /> : <Moon />}+ </button>+ );+};diff --git a/registry/registry-examples.ts b/registry/registry-examples.tsindex e6ccae06..54a59c1e 100644--- a/registry/registry-examples.ts+++ b/registry/registry-examples.ts@@ -1819,4 +1819,18 @@ export const examples: Registry["items"] = [},],},+ {+ name: "animated-theme-toggler-demo",+ type: "registry:example",+ title: "Animated Theme Toggler Demo",+ description: "Example showing animation while changing the theme.",+ registryDependencies: ["https://magicui.design/animated-theme-toggler"],+ files: [+ {+ path: "registry/example/animated-theme-toggler-demo.tsx",+ type: "registry:example",+ target: "components/animated-theme-toggler-demo.tsx",+ },+ ],+ },];diff --git a/registry/registry-ui.ts b/registry/registry-ui.tsindex 4b72bace..dbec2c94 100644--- a/registry/registry-ui.ts+++ b/registry/registry-ui.ts@@ -1273,4 +1273,24 @@ export const ui: Registry["items"] = [},],},+ {+ name: "animated-theme-toggler",+ type: "registry:ui",+ title: "Theme Toggler",+ description: "A component for theme changing animation.",+ dependencies: ["lucide-react"],+ files: [+ {+ path: "registry/magicui/animated-theme-toggler.tsx",+ type: "registry:ui",+ target: "components/magicui/animated-theme-toggler.tsx",+ },+ ],+ css: {+ "::view-transition-old(root), ::view-transition-new(root)": {+ animation: "none",+ "mix-blend-mode": "normal",+ },+ },+ },];diff --git a/styles/globals.css b/styles/globals.cssindex 4b1cbde3..61d8ce4a 100644--- a/styles/globals.css+++ b/styles/globals.css@@ -520,3 +520,9 @@.steps > h3 {@apply mt-8 mb-4 text-base font-semibold;}++::view-transition-old(root),+::view-transition-new(root) {+ animation: none;+ mix-blend-mode: normal;+}\ No newline at end of file