Spaces:
Paused
Paused
feat(design-system): implement absolute inversion principle, typographic audits, waitlist interactive features & WCAG 2.1 compliance
d8804ab Download components/ThemeToggle.tsx from samirhosninet/HubAPI: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/spaces/samirhosninet/HubAPI/resolve/main/components/ThemeToggle.tsx
- Command line
-
hf download hf://spaces/samirhosninet/HubAPI/components/ThemeToggle.tsx
-
curl -L -o ThemeToggle.tsx https://huggingface.co/spaces/samirhosninet/HubAPI/resolve/main/components/ThemeToggle.tsx
4.26 kB
| "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> | |
| ); | |
| } | |