Spaces:
Paused
Paused
File size: 4,264 Bytes
d8804ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | "use client";
import { useTheme } from "next-themes";
import { useEffect, useState } from "react";
import { motion } from "framer-motion";
import { Sun, Moon } from "lucide-react";
import { useSplitFlapAudio } from "@/components/ui/split-flap-text";
import { cn } from "@/lib/utils";
export function ThemeToggle() {
const { resolvedTheme, setTheme } = useTheme();
const [mounted, setMounted] = useState(false);
const audio = useSplitFlapAudio();
// Prevent hydration mismatch
useEffect(() => {
const frame = requestAnimationFrame(() => {
setMounted(true);
});
return () => cancelAnimationFrame(frame);
}, []);
if (!mounted) {
return (
<div className="w-[68px] h-7 rounded-full border border-black/5 dark:border-white/10 bg-white/10 dark:bg-black/20 backdrop-blur-md opacity-40" />
);
}
const isDark = resolvedTheme === "dark";
const handleToggle = (targetTheme: "light" | "dark") => {
if (resolvedTheme === targetTheme) return;
// Play satisfying mechanical click sound
if (audio && !audio.isMuted) {
audio.playClick();
}
setTheme(targetTheme);
};
return (
<div
className="relative flex items-center p-0.5 rounded-full border border-black/10 dark:border-white/10 bg-secondary dark:bg-secondary/40 backdrop-blur-md shadow-[0_4px_20px_rgba(0,0,0,0.05)] dark:shadow-[0_4px_20px_rgba(0,0,0,0.3)] w-[68px] h-7 select-none overflow-hidden"
>
{/* Sliding background container (GPU accelerated via x transform) */}
<motion.div
className="absolute top-0.5 left-0.5 w-[31px] h-[22px] rounded-full bg-primary shadow-[0_2px_6px_rgba(0,0,0,0.12)] dark:shadow-[0_2px_6px_rgba(0,0,0,0.6)] border border-black/5 dark:border-white/5"
initial={false}
animate={{
x: isDark ? 31 : 0,
}}
transition={{ type: "spring", stiffness: 350, damping: 26 }}
/>
{/* Light Mode Button */}
<button
onClick={() => handleToggle("light")}
className={cn(
"relative z-10 flex items-center justify-center w-1/2 h-full rounded-full transition-colors duration-300 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary/50 focus-visible:ring-offset-0 focus-visible:z-20",
!isDark ? "text-primary-foreground" : "text-muted-foreground hover:text-foreground"
)}
aria-label="Light mode"
>
<motion.div
animate={{ rotate: isDark ? 45 : 0, scale: !isDark ? 1.05 : 0.95 }}
transition={{ type: "spring", stiffness: 200, damping: 15 }}
className="relative"
>
<Sun className="w-3.5 h-3.5 fill-current" aria-hidden="true" />
{/* Subtle warm halo glow in light mode */}
{!isDark && (
<motion.span
layoutId="glowingHalo"
className="absolute -inset-1.5 rounded-full bg-primary/20 blur-[3px] -z-10"
transition={{ type: "spring", stiffness: 300, damping: 20 }}
/>
)}
</motion.div>
</button>
{/* Dark Mode Button */}
<button
onClick={() => handleToggle("dark")}
className={cn(
"relative z-10 flex items-center justify-center w-1/2 h-full rounded-full transition-colors duration-300 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary/50 focus-visible:ring-offset-0 focus-visible:z-20",
isDark ? "text-primary-foreground" : "text-muted-foreground hover:text-foreground"
)}
aria-label="Dark mode"
>
<motion.div
animate={{ rotate: isDark ? 0 : -45, scale: isDark ? 1.05 : 0.95 }}
transition={{ type: "spring", stiffness: 200, damping: 15 }}
className="relative"
>
<Moon className="w-3.5 h-3.5 fill-current" aria-hidden="true" />
{/* Subtle cool halo glow in dark mode */}
{isDark && (
<motion.span
layoutId="glowingHalo"
className="absolute -inset-1.5 rounded-full bg-primary/30 blur-[3px] -z-10"
transition={{ type: "spring", stiffness: 300, damping: 20 }}
/>
)}
</motion.div>
</button>
</div>
);
}
|