"use client"
import { useTheme } from "next-themes"
import { SVGProps, useState, useEffect } from "react"
export function ThemeToggle() {
const { theme, systemTheme, setTheme } = useTheme();
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
if (!mounted) return <div className="size-8 rounded-md bg-accent/20 animate-pulse" />;
const toggleTheme = () => {
if (theme === 'system') {
setTheme(systemTheme === 'dark' ? 'light' : 'dark');
} else {
setTheme(theme === 'dark' ? 'light' : 'dark');
}
}
const handleThemeToggle = (e: React.MouseEvent<HTMLButtonElement>) => {
if (!document?.startViewTransition) {
toggleTheme();
return;
}
// Set click coordinates as CSS variables for cursor-relative animation
document.documentElement.style.setProperty("--x", `${e.clientX}px`);
document.documentElement.style.setProperty("--y", `${e.clientY}px`);
document.startViewTransition(() => {
toggleTheme();
});
};
const isDark = theme === 'dark' || (theme === 'system' && systemTheme === 'dark');
return (
<button
aria-label="Toggle theme"
onClick={handleThemeToggle}
className="rounded-md size-8 flex justify-center items-center aspect-square relative overflow-hidden bg-accent hover:bg-accent/80 active:translate-y-px transition-all duration-75 ease-out"
type="button"
>
{isDark ? (
<Moon className="size-4" />
) : (
<Sun className="size-4" />
)}
</button>
)
}
const Sun = (props: SVGProps<SVGSVGElement>) => (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
{...props}
>
<circle cx="12" cy="12" r="4" />
<path d="M12 3v1" />
<path d="m18.364 5.636-.707.707" />
<path d="M20 12h1" />
<path d="m17.657 17.657.707.707" />
<path d="M12 20v1" />
<path d="m6.343 17.657-.707.707" />
<path d="M3 12h1" />
<path d="m5.636 5.636.707.707" />
</svg>
)
const Moon = (props: SVGProps<SVGSVGElement>) => (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
{...props}
>
<path d="M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401" />
</svg>
)