Skip to content

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

Fix: Refactored the animated-theme logic to make the selected theme persist (#811)

JJe94b1330e5c4
diff --git a/apps/www/registry/magicui/animated-theme-toggler.tsx b/apps/www/registry/magicui/animated-theme-toggler.tsx
index 47b541b9..04b581de 100644
--- a/apps/www/registry/magicui/animated-theme-toggler.tsx
+++ b/apps/www/registry/magicui/animated-theme-toggler.tsx
@@ -1,41 +1,60 @@
"use client";
-import { Moon, SunDim } from "lucide-react";
-import { useState, useRef } from "react";
+import { Moon, Sun } from "lucide-react";
+import { useState, useEffect, useRef, useCallback } from "react";
import { flushSync } from "react-dom";
import { cn } from "@/lib/utils";
-type props = {
+type Props = {
className?: string;
};
-export const AnimatedThemeToggler = ({ className }: props) => {
- const [isDarkMode, setIsDarkMode] = useState<boolean>(false);
- const buttonRef = useRef<HTMLButtonElement | null>(null);
- const changeTheme = async () => {
+export const AnimatedThemeToggler = ({ className }: Props) => {
+ const [isDark, setIsDark] = useState(false);
+ const buttonRef = useRef<HTMLButtonElement>(null);
+
+ useEffect(() => {
+ const updateTheme = () => {
+ setIsDark(document.documentElement.classList.contains("dark"));
+ };
+
+ updateTheme();
+
+ const observer = new MutationObserver(updateTheme);
+ observer.observe(document.documentElement, {
+ attributes: true,
+ attributeFilter: ["class"],
+ });
+
+ return () => observer.disconnect();
+ }, []);
+
+ const toggleTheme = useCallback(async () => {
if (!buttonRef.current) return;
await document.startViewTransition(() => {
flushSync(() => {
- const dark = document.documentElement.classList.toggle("dark");
- setIsDarkMode(dark);
+ 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 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));
+ 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(${maxRad}px at ${x}px ${y}px)`,
+ `circle(${maxRadius}px at ${x}px ${y}px)`,
],
},
{
@@ -44,10 +63,11 @@ export const AnimatedThemeToggler = ({ className }: props) => {
pseudoElement: "::view-transition-new(root)",
},
);
- };
+ }, [isDark]);
+
return (
- <button ref={buttonRef} onClick={changeTheme} className={cn(className)}>
- {isDarkMode ? <SunDim /> : <Moon />}
+ <button ref={buttonRef} onClick={toggleTheme} className={cn(className)}>
+ {isDark ? <Sun /> : <Moon />}
</button>
);
};