|
| 1 | +import clsx from "clsx"; |
| 2 | +import { AnimatePresence, motion } from "motion/react"; |
| 3 | +import { |
| 4 | + useEffect, |
| 5 | + useLayoutEffect, |
| 6 | + useState, |
| 7 | + type ComponentType, |
| 8 | + type SVGProps, |
| 9 | +} from "react"; |
| 10 | +import { ComputerDesktopIcon, MoonIcon, SunIcon } from "~/components/icons"; |
| 11 | + |
| 12 | +function useSyncToSystemTheme(isEnabled: boolean) { |
| 13 | + useEffect(() => { |
| 14 | + function onMediaQueryChanged(e: MediaQueryListEvent) { |
| 15 | + updateCurrentTheme(e.matches ? "dark" : "light"); |
| 16 | + } |
| 17 | + |
| 18 | + const mediaQuery = getDarkModeMediaQuery(); |
| 19 | + if (isEnabled) { |
| 20 | + mediaQuery.addEventListener("change", onMediaQueryChanged); |
| 21 | + } else { |
| 22 | + mediaQuery.removeEventListener("change", onMediaQueryChanged); |
| 23 | + } |
| 24 | + |
| 25 | + return () => mediaQuery.removeEventListener("change", onMediaQueryChanged); |
| 26 | + }, [isEnabled]); |
| 27 | +} |
| 28 | + |
| 29 | +export function ThemeInitScript() { |
| 30 | + // Best to add inline in 'head' to avoid FOUC |
| 31 | + return ( |
| 32 | + <script type="text/javascript"> |
| 33 | + {` |
| 34 | + document.documentElement.classList.toggle( |
| 35 | + "dark", |
| 36 | + localStorage.theme === "dark" || |
| 37 | + (!("theme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches), |
| 38 | + );`} |
| 39 | + </script> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function getDarkModeMediaQuery() { |
| 44 | + return window.matchMedia("(prefers-color-scheme: dark)"); |
| 45 | +} |
| 46 | + |
| 47 | +export function ThemeToggle() { |
| 48 | + const [themeSetting, setThemeSetting] = useState<ThemeSetting>(); |
| 49 | + useLayoutEffect(() => { |
| 50 | + setThemeSetting(getStoredThemeSetting()); |
| 51 | + }, []); |
| 52 | + |
| 53 | + useSyncToSystemTheme(themeSetting === "system"); |
| 54 | + |
| 55 | + function onThemeSelected(themeSetting: ThemeSetting) { |
| 56 | + setThemeSetting(themeSetting); |
| 57 | + updateCurrentTheme(themeSetting); |
| 58 | + storeThemeSetting(themeSetting); |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className="relative h-10 w-11 overflow-hidden rounded-full shadow-lg ring-1 shadow-zinc-800/5 ring-zinc-900/5 backdrop-blur-sm dark:bg-zinc-800/90 dark:ring-white/10"> |
| 63 | + <ThemeButton |
| 64 | + icon={MoonIcon} |
| 65 | + isSelected={themeSetting === "dark"} |
| 66 | + onSelected={() => onThemeSelected("light")} |
| 67 | + title="Dark Theme" |
| 68 | + /> |
| 69 | + <ThemeButton |
| 70 | + icon={SunIcon} |
| 71 | + isSelected={themeSetting === "light"} |
| 72 | + onSelected={() => onThemeSelected("system")} |
| 73 | + title="Light Theme" |
| 74 | + /> |
| 75 | + <ThemeButton |
| 76 | + icon={ComputerDesktopIcon} |
| 77 | + isSelected={themeSetting === "system"} |
| 78 | + onSelected={() => onThemeSelected("dark")} |
| 79 | + title="System Theme" |
| 80 | + /> |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function getStoredThemeSetting(): ThemeSetting { |
| 86 | + return localStorage.theme === "dark" |
| 87 | + ? "dark" |
| 88 | + : localStorage.theme === "light" |
| 89 | + ? "light" |
| 90 | + : "system"; |
| 91 | +} |
| 92 | + |
| 93 | +function storeThemeSetting(themeSetting: ThemeSetting) { |
| 94 | + if (themeSetting === "dark") { |
| 95 | + localStorage.theme = "dark"; |
| 96 | + } else if (themeSetting === "light") { |
| 97 | + localStorage.theme = "light"; |
| 98 | + } else { |
| 99 | + localStorage.removeItem("theme"); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +function updateCurrentTheme(themeSetting: ThemeSetting) { |
| 104 | + const theme = themeSetting === "system" ? getSystemTheme() : themeSetting; |
| 105 | + |
| 106 | + document.documentElement.classList.toggle("dark", theme === "dark"); |
| 107 | +} |
| 108 | + |
| 109 | +function getSystemTheme(): Theme { |
| 110 | + return getDarkModeMediaQuery().matches ? "dark" : "light"; |
| 111 | +} |
| 112 | + |
| 113 | +type Theme = "dark" | "light"; |
| 114 | +type ThemeSetting = Theme | "system"; |
| 115 | + |
| 116 | +function ThemeButton({ |
| 117 | + isSelected, |
| 118 | + onSelected, |
| 119 | + icon: Icon, |
| 120 | + title, |
| 121 | +}: { |
| 122 | + icon: ComponentType<SVGProps<SVGSVGElement>>; |
| 123 | + isSelected: boolean; |
| 124 | + onSelected: () => void; |
| 125 | + title?: string; |
| 126 | +}) { |
| 127 | + return ( |
| 128 | + <AnimatePresence> |
| 129 | + {isSelected ? ( |
| 130 | + <motion.button |
| 131 | + animate={{ translateY: 0 }} |
| 132 | + initial={{ translateY: "-100%" }} |
| 133 | + exit={{ translateY: "100%" }} |
| 134 | + className={clsx( |
| 135 | + "absolute top-1/2 left-1/2 -translate-1/2 fill-zinc-500 py-2.5 hover:fill-zinc-600 dark:fill-zinc-400 dark:hover:fill-zinc-300", |
| 136 | + )} |
| 137 | + onClick={() => onSelected()} |
| 138 | + title={title} |
| 139 | + > |
| 140 | + <Icon className="size-5" /> |
| 141 | + </motion.button> |
| 142 | + ) : null} |
| 143 | + </AnimatePresence> |
| 144 | + ); |
| 145 | +} |
0 commit comments