Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 2x 2x 2x 2x 10x 2x 10x 10x 2x 10x 2x 17x 17x 17x 17x 10x 10x 10x 10x 1x 1x 10x 17x 10x 10x 10x 10x 17x 17x 16x 17x 11x 11x 11x 11x 11x 11x 11x 11x 17x 5x 5x 17x 1x 1x 17x 3x 17x 17x 17x 2x 22x 22x 4x 17x | import React, {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useState,
ReactNode,
} from "react";
import { AccentId, DEFAULT_ACCENT, getAccent } from "./accents";
export type ThemeMode = "light" | "dark" | "system";
export type ResolvedTheme = "light" | "dark";
interface ThemeContextType {
/** The user's chosen mode. `system` follows the OS preference live. */
mode: ThemeMode;
/** The concrete theme currently applied (`system` resolved to light/dark). */
resolvedTheme: ResolvedTheme;
/** The selected accent / brand colour. */
accent: AccentId;
setMode: (mode: ThemeMode) => void;
setAccent: (accent: AccentId) => void;
/**
* Convenience for simple light/dark toggles (e.g. the navbar switch).
* Sets an explicit `light`/`dark` mode based on what is currently shown.
*/
toggleTheme: () => void;
}
// localStorage keys — shared with the Appearance settings page so the context
// and the settings UI read/write a single source of truth.
const MODE_KEY = "settings:appearance:theme_mode";
const ACCENT_KEY = "settings:appearance:accent";
const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
const prefersDark = (): boolean =>
typeof window !== "undefined" &&
window.matchMedia?.("(prefers-color-scheme: dark)").matches;
const readStoredMode = (): ThemeMode => {
const stored = localStorage.getItem(MODE_KEY);
return stored === "dark" || stored === "light" || stored === "system"
? stored
: "light";
};
const readStoredAccent = (): AccentId =>
getAccent(localStorage.getItem(ACCENT_KEY)).id;
export const ThemeProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const [mode, setModeState] = useState<ThemeMode>(readStoredMode);
const [accent, setAccentState] = useState<AccentId>(readStoredAccent);
const [systemDark, setSystemDark] = useState<boolean>(prefersDark);
// One-time cleanup of the removed "invert" mode (legacy filter hack).
useEffect(() => {
localStorage.removeItem("invert");
Iif (document.documentElement.style.filter) {
document.documentElement.style.removeProperty("filter");
}
// Migrate the old standalone "theme" key into the new namespaced key.
const legacy = localStorage.getItem("theme");
if (legacy && !localStorage.getItem(MODE_KEY)) {
localStorage.setItem(MODE_KEY, legacy);
setModeState(legacy === "dark" ? "dark" : "light");
}
localStorage.removeItem("theme");
}, []);
// Track the OS colour-scheme preference while in `system` mode.
useEffect(() => {
const mq = window.matchMedia("(prefers-color-scheme: dark)");
const handler = (e: MediaQueryListEvent) => setSystemDark(e.matches);
mq.addEventListener("change", handler);
return () => mq.removeEventListener("change", handler);
}, []);
const resolvedTheme: ResolvedTheme =
mode === "system" ? (systemDark ? "dark" : "light") : mode;
// Apply the resolved light/dark theme to the document root.
useEffect(() => {
document.documentElement.classList.toggle("dark", resolvedTheme === "dark");
}, [resolvedTheme]);
// Apply the accent colour to both the legacy and shadcn CSS variables.
useEffect(() => {
const root = document.documentElement;
const a = getAccent(accent);
root.style.setProperty("--primary-color", a.color);
root.style.setProperty("--primary", a.color);
root.style.setProperty("--primary-foreground", a.foreground);
root.style.setProperty("--ring", a.color);
root.style.setProperty("--sidebar-primary", a.color);
root.style.setProperty("--sidebar-ring", a.color);
}, [accent]);
const setMode = useCallback((next: ThemeMode) => {
setModeState(next);
localStorage.setItem(MODE_KEY, next);
}, []);
const setAccent = useCallback((next: AccentId) => {
setAccentState(next);
localStorage.setItem(ACCENT_KEY, next);
}, []);
const toggleTheme = useCallback(() => {
setMode(resolvedTheme === "dark" ? "light" : "dark");
}, [resolvedTheme, setMode]);
const value = useMemo<ThemeContextType>(
() => ({ mode, resolvedTheme, accent, setMode, setAccent, toggleTheme }),
[mode, resolvedTheme, accent, setMode, setAccent, toggleTheme],
);
return (
<ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>
);
};
export const useTheme = (): ThemeContextType => {
const context = useContext(ThemeContext);
if (!context) {
throw new Error("useTheme must be used within a ThemeProvider");
}
return context;
};
export { DEFAULT_ACCENT };
export type { AccentId };
|