UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
fix: update code example for theme toggler
Dillion Verma181422d2787c
diff --git a/apps/www/public/llms-full.txt b/apps/www/public/llms-full.txtindex d881c5a2..6451aa5c 100644--- a/apps/www/public/llms-full.txt+++ b/apps/www/public/llms-full.txt@@ -2795,11 +2795,16 @@ import { flushSync } from "react-dom"import { cn } from "@/lib/utils"-type Props = {- className?: string+interface AnimatedThemeTogglerProps+ extends React.ComponentPropsWithoutRef<"button"> {+ duration?: number}-export const AnimatedThemeToggler = ({ className }: Props) => {+export const AnimatedThemeToggler = ({+ className,+ duration = 400,+ ...props+}: AnimatedThemeTogglerProps) => {const [isDark, setIsDark] = useState(false)const buttonRef = useRef<HTMLButtonElement>(null)@@ -2848,15 +2853,20 @@ export const AnimatedThemeToggler = ({ className }: Props) => {],},{- duration: 700,+ duration,easing: "ease-in-out",pseudoElement: "::view-transition-new(root)",})- }, [isDark])+ }, [isDark, duration])return (- <button ref={buttonRef} onClick={toggleTheme} className={cn(className)}>+ <button+ ref={buttonRef}+ onClick={toggleTheme}+ className={cn(className)}+ {...props}+ >{isDark ? <Sun /> : <Moon />}</button>)@@ -2869,14 +2879,9 @@ Title: Animated Theme Toggler Demo--- file: example/animated-theme-toggler-demo.tsx ---import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"-function AnimatedThemeTogglerDemo() {- return (- <div>- <AnimatedThemeToggler />- </div>- )+export default function AnimatedThemeTogglerDemo() {+ return <AnimatedThemeToggler />}-export default AnimatedThemeTogglerDemodiff --git a/apps/www/public/r/animated-theme-toggler-demo.json b/apps/www/public/r/animated-theme-toggler-demo.jsonindex d2e4081a..bbf87eec 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\nfunction AnimatedThemeTogglerDemo() {\n return (\n <div>\n <AnimatedThemeToggler />\n </div>\n )\n}\nexport default AnimatedThemeTogglerDemo\n",+ "content": "import { AnimatedThemeToggler } from \"@/registry/magicui/animated-theme-toggler\"\n\nexport default function AnimatedThemeTogglerDemo() {\n return <AnimatedThemeToggler />\n}\n","type": "registry:example"}]diff --git a/apps/www/public/r/animated-theme-toggler.json b/apps/www/public/r/animated-theme-toggler.jsonindex b3a411ca..1881bd55 100644--- a/apps/www/public/r/animated-theme-toggler.json+++ b/apps/www/public/r/animated-theme-toggler.json@@ -10,7 +10,7 @@"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\ntype Props = {\n className?: string\n}\n\nexport const AnimatedThemeToggler = ({ className }: Props) => {\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(async () => {\n if (!buttonRef.current) return\n\n await document.startViewTransition(() => {\n flushSync(() => {\n const newTheme = !isDark\n setIsDark(newTheme)\n document.documentElement.classList.toggle(\"dark\")\n localStorage.setItem(\"theme\", newTheme ? \"dark\" : \"light\")\n })\n }).ready\n\n const { top, left, width, height } =\n buttonRef.current.getBoundingClientRect()\n const x = left + width / 2\n const y = top + height / 2\n const maxRadius = Math.hypot(\n Math.max(left, window.innerWidth - left),\n Math.max(top, window.innerHeight - top)\n )\n\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: 700,\n easing: \"ease-in-out\",\n pseudoElement: \"::view-transition-new(root)\",\n }\n )\n }, [isDark])\n\n return (\n <button ref={buttonRef} onClick={toggleTheme} className={cn(className)}>\n {isDark ? <Sun /> : <Moon />}\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\ninterface AnimatedThemeTogglerProps\n 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(async () => {\n if (!buttonRef.current) return\n\n await document.startViewTransition(() => {\n flushSync(() => {\n const newTheme = !isDark\n setIsDark(newTheme)\n document.documentElement.classList.toggle(\"dark\")\n localStorage.setItem(\"theme\", newTheme ? \"dark\" : \"light\")\n })\n }).ready\n\n const { top, left, width, height } =\n buttonRef.current.getBoundingClientRect()\n const x = left + width / 2\n const y = top + height / 2\n const maxRadius = Math.hypot(\n Math.max(left, window.innerWidth - left),\n Math.max(top, window.innerHeight - top)\n )\n\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 }, [isDark, duration])\n\n return (\n <button\n ref={buttonRef}\n onClick={toggleTheme}\n className={cn(className)}\n {...props}\n >\n {isDark ? <Sun /> : <Moon />}\n </button>\n )\n}\n","type": "registry:ui"}],diff --git a/apps/www/registry/example/animated-theme-toggler-demo.tsx b/apps/www/registry/example/animated-theme-toggler-demo.tsxindex b83f24c4..f3c19653 100644--- a/apps/www/registry/example/animated-theme-toggler-demo.tsx+++ b/apps/www/registry/example/animated-theme-toggler-demo.tsx@@ -1,10 +1,5 @@import { AnimatedThemeToggler } from "@/registry/magicui/animated-theme-toggler"-function AnimatedThemeTogglerDemo() {- return (- <div>- <AnimatedThemeToggler />- </div>- )+export default function AnimatedThemeTogglerDemo() {+ return <AnimatedThemeToggler />}-export default AnimatedThemeTogglerDemodiff --git a/apps/www/registry/magicui/animated-theme-toggler.tsx b/apps/www/registry/magicui/animated-theme-toggler.tsxindex 8057dbc6..0b8983f5 100644--- a/apps/www/registry/magicui/animated-theme-toggler.tsx+++ b/apps/www/registry/magicui/animated-theme-toggler.tsx@@ -6,11 +6,16 @@ import { flushSync } from "react-dom"import { cn } from "@/lib/utils"-type Props = {- className?: string+interface AnimatedThemeTogglerProps+ extends React.ComponentPropsWithoutRef<"button"> {+ duration?: number}-export const AnimatedThemeToggler = ({ className }: Props) => {+export const AnimatedThemeToggler = ({+ className,+ duration = 400,+ ...props+}: AnimatedThemeTogglerProps) => {const [isDark, setIsDark] = useState(false)const buttonRef = useRef<HTMLButtonElement>(null)@@ -59,15 +64,20 @@ export const AnimatedThemeToggler = ({ className }: Props) => {],},{- duration: 700,+ duration,easing: "ease-in-out",pseudoElement: "::view-transition-new(root)",})- }, [isDark])+ }, [isDark, duration])return (- <button ref={buttonRef} onClick={toggleTheme} className={cn(className)}>+ <button+ ref={buttonRef}+ onClick={toggleTheme}+ className={cn(className)}+ {...props}+ >{isDark ? <Sun /> : <Moon />}</button>)