|
| 1 | +import React, { |
| 2 | + createContext, |
| 3 | + useCallback, |
| 4 | + useContext, |
| 5 | + useEffect, |
| 6 | + useLayoutEffect, |
| 7 | + useMemo, |
| 8 | + type ReactNode, |
| 9 | +} from "react"; |
| 10 | +import { usePersistedState } from "@/browser/hooks/usePersistedState"; |
| 11 | +import { KEYBINDS, matchesKeybind } from "@/browser/utils/ui/keybinds"; |
| 12 | +import { UI_THEME_KEY } from "@/common/constants/storage"; |
| 13 | + |
| 14 | +export type ThemeMode = "light" | "dark"; |
| 15 | + |
| 16 | +interface ThemeContextValue { |
| 17 | + theme: ThemeMode; |
| 18 | + setTheme: React.Dispatch<React.SetStateAction<ThemeMode>>; |
| 19 | + toggleTheme: () => void; |
| 20 | +} |
| 21 | + |
| 22 | +const ThemeContext = createContext<ThemeContextValue | null>(null); |
| 23 | + |
| 24 | +const DARK_THEME_COLOR = "#1e1e1e"; |
| 25 | +const LIGHT_THEME_COLOR = "#f5f6f8"; |
| 26 | + |
| 27 | +function resolveSystemTheme(): ThemeMode { |
| 28 | + if (typeof window === "undefined" || !window.matchMedia) { |
| 29 | + return "dark"; |
| 30 | + } |
| 31 | + |
| 32 | + return window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark"; |
| 33 | +} |
| 34 | + |
| 35 | +function applyThemeToDocument(theme: ThemeMode) { |
| 36 | + if (typeof document === "undefined") { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + const root = document.documentElement; |
| 41 | + root.dataset.theme = theme; |
| 42 | + root.style.colorScheme = theme; |
| 43 | + |
| 44 | + const themeColor = theme === "light" ? LIGHT_THEME_COLOR : DARK_THEME_COLOR; |
| 45 | + const meta = document.querySelector<HTMLMetaElement>('meta[name="theme-color"]'); |
| 46 | + if (meta) { |
| 47 | + meta.setAttribute("content", themeColor); |
| 48 | + } |
| 49 | + |
| 50 | + const body = document.body; |
| 51 | + if (body) { |
| 52 | + body.style.backgroundColor = "var(--color-background)"; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export function ThemeProvider(props: { children: ReactNode }) { |
| 57 | + const [theme, setTheme] = usePersistedState<ThemeMode>(UI_THEME_KEY, resolveSystemTheme(), { |
| 58 | + listener: true, |
| 59 | + }); |
| 60 | + |
| 61 | + useLayoutEffect(() => { |
| 62 | + applyThemeToDocument(theme); |
| 63 | + }, [theme]); |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 67 | + if (matchesKeybind(event, KEYBINDS.TOGGLE_THEME)) { |
| 68 | + event.preventDefault(); |
| 69 | + setTheme((current) => (current === "dark" ? "light" : "dark")); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + window.addEventListener("keydown", handleKeyDown); |
| 74 | + return () => window.removeEventListener("keydown", handleKeyDown); |
| 75 | + }, [setTheme]); |
| 76 | + |
| 77 | + const toggleTheme = useCallback(() => { |
| 78 | + setTheme((current) => (current === "dark" ? "light" : "dark")); |
| 79 | + }, [setTheme]); |
| 80 | + |
| 81 | + const value = useMemo<ThemeContextValue>( |
| 82 | + () => ({ |
| 83 | + theme, |
| 84 | + setTheme, |
| 85 | + toggleTheme, |
| 86 | + }), |
| 87 | + [setTheme, theme, toggleTheme] |
| 88 | + ); |
| 89 | + |
| 90 | + return <ThemeContext.Provider value={value}>{props.children}</ThemeContext.Provider>; |
| 91 | +} |
| 92 | + |
| 93 | +export function useTheme(): ThemeContextValue { |
| 94 | + const context = useContext(ThemeContext); |
| 95 | + if (!context) { |
| 96 | + throw new Error("useTheme must be used within a ThemeProvider"); |
| 97 | + } |
| 98 | + return context; |
| 99 | +} |
0 commit comments