mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-09-23 00:41:13 +00:00
Introduce semantic CSS custom properties (page, panel, fg, line, overlay, etc.) that swap values via .light/.dark class on <html>. An inline script reads localStorage / prefers-color-scheme before first paint to prevent flash-of-wrong-theme. - ThemeProvider + useTheme hook in app/lib/theme.tsx - Sun/Moon toggle in desktop nav and mobile menu - .light overrides for all semantic tokens, accent colors (WCAG AA on white), atmosphere gradient, @pierre/diffs surfaces, and chart variables - Migrated ~30 files from hardcoded color classes to semantic tokens - GraphViz diagrams use getGraphTheme() for light/dark hex maps - Chart gridlines and axis labels use CSS custom properties - Workflow card colors reference CSS vars for automatic theme switching - Pierre diffs switch between pierre-dark and pierre-light themes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
|
|
|
type Theme = "light" | "dark";
|
|
|
|
const STORAGE_KEY = "arc-theme";
|
|
|
|
function getInitialTheme(): Theme {
|
|
if (typeof window === "undefined") return "dark";
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
if (stored === "light" || stored === "dark") return stored;
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
? "dark"
|
|
: "light";
|
|
}
|
|
|
|
function applyThemeClass(theme: Theme) {
|
|
const root = document.documentElement;
|
|
root.classList.remove("light", "dark");
|
|
root.classList.add(theme);
|
|
}
|
|
|
|
const ThemeContext = createContext<{
|
|
theme: Theme;
|
|
toggle: () => void;
|
|
}>({ theme: "dark", toggle: () => {} });
|
|
|
|
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
|
const [theme, setTheme] = useState<Theme>(getInitialTheme);
|
|
|
|
useEffect(() => {
|
|
applyThemeClass(theme);
|
|
}, [theme]);
|
|
|
|
const toggle = useCallback(() => {
|
|
setTheme((prev) => {
|
|
const next = prev === "dark" ? "light" : "dark";
|
|
localStorage.setItem(STORAGE_KEY, next);
|
|
return next;
|
|
});
|
|
}, []);
|
|
|
|
return (
|
|
<ThemeContext value={{ theme, toggle }}>
|
|
{children}
|
|
</ThemeContext>
|
|
);
|
|
}
|
|
|
|
export function useTheme() {
|
|
return useContext(ThemeContext);
|
|
}
|
|
|
|
export type { Theme };
|