Skip to content

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

fix(www): correct mobile viewport-based transition radius in animated-theme-toggle (#911)

Yeom-JinHoae3dcf5b601e
diff --git a/apps/www/public/llms-full.txt b/apps/www/public/llms-full.txt
index 14037f8d..bec3945b 100644
--- a/apps/www/public/llms-full.txt
+++ b/apps/www/public/llms-full.txt
@@ -2856,7 +2856,18 @@ export const AnimatedThemeToggler = ({
}, [])
const toggleTheme = useCallback(() => {
- if (!buttonRef.current) return
+ 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
+ const maxRadius = Math.hypot(
+ Math.max(x, viewportWidth - x),
+ Math.max(y, viewportHeight - y)
+ )
const applyTheme = () => {
const newTheme = !isDark
@@ -2865,10 +2876,7 @@ export const AnimatedThemeToggler = ({
localStorage.setItem("theme", newTheme ? "dark" : "light")
}
- if (
- typeof document === "undefined" ||
- !("startViewTransition" in document)
- ) {
+ if (typeof document.startViewTransition !== "function") {
applyTheme()
return
}
@@ -2880,19 +2888,6 @@ export const AnimatedThemeToggler = ({
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: [
@@ -2912,6 +2907,7 @@ export const AnimatedThemeToggler = ({
return (
<button
+ type="button"
ref={buttonRef}
onClick={toggleTheme}
className={cn(className)}
diff --git a/apps/www/public/r/animated-theme-toggler.json b/apps/www/public/r/animated-theme-toggler.json
index 95f999d4..3e915ddf 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(() => {\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",
+ "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",
"type": "registry:ui"
}
],
diff --git a/apps/www/registry/magicui/animated-theme-toggler.tsx b/apps/www/registry/magicui/animated-theme-toggler.tsx
index 0df20abd..1515337a 100644
--- a/apps/www/registry/magicui/animated-theme-toggler.tsx
+++ b/apps/www/registry/magicui/animated-theme-toggler.tsx
@@ -35,7 +35,18 @@ export const AnimatedThemeToggler = ({
}, [])
const toggleTheme = useCallback(() => {
- if (!buttonRef.current) return
+ 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
+ const maxRadius = Math.hypot(
+ Math.max(x, viewportWidth - x),
+ Math.max(y, viewportHeight - y)
+ )
const applyTheme = () => {
const newTheme = !isDark
@@ -44,10 +55,7 @@ export const AnimatedThemeToggler = ({
localStorage.setItem("theme", newTheme ? "dark" : "light")
}
- if (
- typeof document === "undefined" ||
- !("startViewTransition" in document)
- ) {
+ if (typeof document.startViewTransition !== "function") {
applyTheme()
return
}
@@ -59,19 +67,6 @@ export const AnimatedThemeToggler = ({
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: [
@@ -91,6 +86,7 @@ export const AnimatedThemeToggler = ({
return (
<button
+ type="button"
ref={buttonRef}
onClick={toggleTheme}
className={cn(className)}