mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-09-06 08:18:58 +00:00
Rename directories (arc-web → fabro-web, arc-api-client → fabro-api-client), update package names, import paths, TS-only identifiers (theme key, session cookie, demo cookie, OAuth state, db filename, mock data), and supporting files (Dockerfile, docker-compose, entrypoint, CI workflow, CLAUDE.md). 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 = "fabro-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 };
|