45bfae6e94
Initial import of all source code, config, and README assets: the packages workspace (cli, core, server, web, docs, landing, skills), build scripts, tooling config, and CI workflows. Includes the data-layout revision made on this branch: the local data root defaults to ~/.penguin/data (PENGUIN_HOME still overrides; the installer keeps its binaries in ~/.penguin), and every Agent lives under <project>/agents/<agent>/ — path helpers, the three agent-enumeration scans, the system prompt, built-in Skills, tests and docs all follow the new layout. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018ihk8iQuo3kv2aPjAYEPuR
65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
/**
|
|
* Theme context: light / dark / system (tracks prefers-color-scheme live), toggled
|
|
* via the html.dark class + Tailwind dark: variant, persisted to localStorage.
|
|
* Same behavior as the landing page, under the docs site's own storage key
|
|
* (index.html pre-applies the stored value before first paint).
|
|
*/
|
|
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
|
import type { ReactNode } from "react";
|
|
|
|
export type ThemeMode = "light" | "dark" | "system";
|
|
|
|
const MODE_KEY = "penguin-docs.theme";
|
|
|
|
interface ThemeContextValue {
|
|
mode: ThemeMode;
|
|
/** Resolved effective theme (system mode resolved against the OS preference). */
|
|
dark: boolean;
|
|
setMode: (mode: ThemeMode) => void;
|
|
}
|
|
|
|
const ThemeContext = createContext<ThemeContextValue | null>(null);
|
|
|
|
function initialMode(): ThemeMode {
|
|
const stored = localStorage.getItem(MODE_KEY);
|
|
if (stored === "light" || stored === "dark" || stored === "system") return stored;
|
|
return "system";
|
|
}
|
|
|
|
function systemDark(): boolean {
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
}
|
|
|
|
export function ThemeProvider({ children }: { children: ReactNode }) {
|
|
const [mode, setModeState] = useState<ThemeMode>(initialMode);
|
|
const [sysDark, setSysDark] = useState(systemDark);
|
|
|
|
const dark = mode === "system" ? sysDark : mode === "dark";
|
|
|
|
useEffect(() => {
|
|
document.documentElement.classList.toggle("dark", dark);
|
|
}, [dark]);
|
|
|
|
useEffect(() => {
|
|
if (mode !== "system") return;
|
|
const mq = window.matchMedia("(prefers-color-scheme: dark)");
|
|
const onChange = (e: MediaQueryListEvent) => setSysDark(e.matches);
|
|
setSysDark(mq.matches);
|
|
mq.addEventListener("change", onChange);
|
|
return () => mq.removeEventListener("change", onChange);
|
|
}, [mode]);
|
|
|
|
const setMode = useCallback((next: ThemeMode) => {
|
|
localStorage.setItem(MODE_KEY, next);
|
|
setModeState(next);
|
|
}, []);
|
|
|
|
return <ThemeContext.Provider value={{ mode, dark, setMode }}>{children}</ThemeContext.Provider>;
|
|
}
|
|
|
|
export function useTheme(): ThemeContextValue {
|
|
const ctx = useContext(ThemeContext);
|
|
if (!ctx) throw new Error("useTheme must be used inside ThemeProvider");
|
|
return ctx;
|
|
}
|