UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
Fix AnimatedThemeToggler crash on Firefox (View Transitions API fallback) (#879)
Dhiraj Bhawsar3929c8c05893
diff --git a/apps/www/public/llms-full.txt b/apps/www/public/llms-full.txtindex 5a2a5d4b..5e1939f6 100644--- a/apps/www/public/llms-full.txt+++ b/apps/www/public/llms-full.txt@@ -2845,40 +2845,59 @@ export const AnimatedThemeToggler = ({return () => observer.disconnect()}, [])- const toggleTheme = useCallback(async () => {+ const toggleTheme = useCallback(() => {if (!buttonRef.current) return- await document.startViewTransition(() => {- flushSync(() => {- const newTheme = !isDark- setIsDark(newTheme)- document.documentElement.classList.toggle("dark")- localStorage.setItem("theme", newTheme ? "dark" : "light")- })- }).ready-- const { top, left, width, height } =- buttonRef.current.getBoundingClientRect()- const x = left + width / 2- const y = top + height / 2- const maxRadius = Math.hypot(- Math.max(left, window.innerWidth - left),- Math.max(top, window.innerHeight - top)- )+ const applyTheme = () => {+ const newTheme = !isDark+ setIsDark(newTheme)+ document.documentElement.classList.toggle("dark")+ localStorage.setItem("theme", newTheme ? "dark" : "light")+ }- document.documentElement.animate(- {- clipPath: [- `circle(0px at ${x}px ${y}px)`,- `circle(${maxRadius}px at ${x}px ${y}px)`,- ],- },- {- duration,- easing: "ease-in-out",- pseudoElement: "::view-transition-new(root)",- }- )+ if (+ typeof document === "undefined" ||+ !("startViewTransition" in document)+ ) {+ applyTheme()+ return+ }++ const transition = document.startViewTransition(() => {+ flushSync(applyTheme)+ })++ const ready = transition?.ready+ if (ready && typeof ready.then === "function") {+ ready.then(() => {+ 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 maxRadius = Math.hypot(+ Math.max(left, window.innerWidth - left),+ Math.max(top, window.innerHeight - top)+ )++ document.documentElement.animate(+ {+ clipPath: [+ `circle(0px at ${x}px ${y}px)`,+ `circle(${maxRadius}px at ${x}px ${y}px)`,+ ],+ },+ {+ duration,+ easing: "ease-in-out",+ pseudoElement: "::view-transition-new(root)",+ }+ )+ })+ }}, [isDark, duration])return (diff --git a/apps/www/public/r/animated-theme-toggler.json b/apps/www/public/r/animated-theme-toggler.jsonindex 8c0bb7b1..95f999d4 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\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(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 <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\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 if (!buttonRef.current) return\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 (\n typeof document === \"undefined\" ||\n !(\"startViewTransition\" in document)\n ) {\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 const button = buttonRef.current\n if (!button) return\n\n const { top, left, width, height } = button.getBoundingClientRect()\n\n const x = left + width / 2\n const y = top + height / 2\n\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 })\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 <span className=\"sr-only\">Toggle theme</span>\n </button>\n )\n}\n","type": "registry:ui"}],diff --git a/apps/www/registry/magicui/animated-theme-toggler.tsx b/apps/www/registry/magicui/animated-theme-toggler.tsxindex f896fbcd..0df20abd 100644--- a/apps/www/registry/magicui/animated-theme-toggler.tsx+++ b/apps/www/registry/magicui/animated-theme-toggler.tsx@@ -34,40 +34,59 @@ export const AnimatedThemeToggler = ({return () => observer.disconnect()}, [])- const toggleTheme = useCallback(async () => {+ const toggleTheme = useCallback(() => {if (!buttonRef.current) return- await document.startViewTransition(() => {- flushSync(() => {- const newTheme = !isDark- setIsDark(newTheme)- document.documentElement.classList.toggle("dark")- localStorage.setItem("theme", newTheme ? "dark" : "light")+ const applyTheme = () => {+ const newTheme = !isDark+ setIsDark(newTheme)+ document.documentElement.classList.toggle("dark")+ localStorage.setItem("theme", newTheme ? "dark" : "light")+ }++ if (+ typeof document === "undefined" ||+ !("startViewTransition" in document)+ ) {+ applyTheme()+ return+ }++ const transition = document.startViewTransition(() => {+ flushSync(applyTheme)+ })++ const ready = transition?.ready+ if (ready && typeof ready.then === "function") {+ ready.then(() => {+ 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 maxRadius = Math.hypot(+ Math.max(left, window.innerWidth - left),+ Math.max(top, window.innerHeight - top)+ )++ document.documentElement.animate(+ {+ clipPath: [+ `circle(0px at ${x}px ${y}px)`,+ `circle(${maxRadius}px at ${x}px ${y}px)`,+ ],+ },+ {+ duration,+ easing: "ease-in-out",+ pseudoElement: "::view-transition-new(root)",+ }+ )})- }).ready-- const { top, left, width, height } =- buttonRef.current.getBoundingClientRect()- const x = left + width / 2- const y = top + height / 2- const maxRadius = Math.hypot(- Math.max(left, window.innerWidth - left),- Math.max(top, window.innerHeight - top)- )-- document.documentElement.animate(- {- clipPath: [- `circle(0px at ${x}px ${y}px)`,- `circle(${maxRadius}px at ${x}px ${y}px)`,- ],- },- {- duration,- easing: "ease-in-out",- pseudoElement: "::view-transition-new(root)",- }- )+ }}, [isDark, duration])return (