type Mode = "light" | "dark"; interface Swatch { background: string; text: string; accent: string; } const PRESETS = { dark: { label: "Default Dark", mode: "dark", swatch: { background: "#141414", text: "#fafafa", accent: "#6a63f0" } }, light: { label: "Default Light", mode: "light", swatch: { background: "#ffffff", text: "#0a0a0a", accent: "#5750f1" } }, "classic-light": { label: "Classic Light", mode: "light", swatch: { background: "#ffffff", text: "#111827", accent: "#5750f1" } }, "classic-dark": { label: "Classic Dark", mode: "dark", swatch: { background: "#1f2937", text: "#f3f4f6", accent: "#6a63f0" } }, ash: { label: "Ash", mode: "dark", swatch: { background: "#313338", text: "#eceef0", accent: "#5865f2" } }, snow: { label: "Snow", mode: "light", swatch: { background: "#ffffff", text: "#282a30", accent: "#5e6ad2" } } } as const satisfies Record; export type Preset = keyof typeof PRESETS; export type Theme = { system: true; light: Preset; dark: Preset } | { system: false; preset: Preset }; export const PRESET_NAMES = Object.keys(PRESETS) as Preset[]; export const DEFAULT_THEME: Theme = { system: true, light: "light", dark: "dark" }; export const THEME_ATTRIBUTES = ["data-theme", "data-palette-light", "data-palette-dark"]; const COOKIE = "theme"; const YEAR = 60 * 60 * 24 * 365; export const themeLabel = (preset: Preset): string => PRESETS[preset].label; export const themeSwatch = (preset: Preset): Swatch => PRESETS[preset].swatch; export const presetMode = (preset: Preset): Mode => PRESETS[preset].mode; export const presetsFor = (mode: Mode): Preset[] => PRESET_NAMES.filter((preset) => PRESETS[preset].mode === mode); const isPreset = (raw: string, mode?: Mode): raw is Preset => Object.hasOwn(PRESETS, raw) && (!mode || PRESETS[raw as Preset].mode === mode); // a lone preset is a fixed theme, light:dark follows the system export const serializeTheme = (theme: Theme): string => theme.system ? `${theme.light}:${theme.dark}` : theme.preset; export const parseTheme = (raw: string | null | undefined): Theme => { if (!raw) return DEFAULT_THEME; const [first, second] = raw.split(":"); if (second === undefined) return isPreset(first) ? { system: false, preset: first } : DEFAULT_THEME; return { system: true, light: isPreset(first, "light") ? first : DEFAULT_THEME.light, dark: isPreset(second, "dark") ? second : DEFAULT_THEME.dark }; }; // the server reads the raw header so the hook works on any request shape export const themeFromCookies = (header: string | null): Theme => { for (const part of header?.split(";") ?? []) { const [name, value] = part.trim().split("="); if (name === COOKIE) return parseTheme(value); } return DEFAULT_THEME; }; // the default presets are the base tokens, so they have no palette const palette = (preset: Preset, mode: Mode): Preset | undefined => preset !== mode && PRESETS[preset].mode === mode ? preset : undefined; // a system theme has no data-theme, so the dark variant uses prefers-color-scheme const themeAttributes = (theme: Theme): [string, string | undefined][] => { const [light, dark] = theme.system ? [theme.light, theme.dark] : [theme.preset, theme.preset]; return [ ["data-theme", theme.system ? undefined : PRESETS[theme.preset].mode], ["data-palette-light", palette(light, "light")], ["data-palette-dark", palette(dark, "dark")] ]; }; export const themeAttribute = (theme: Theme): string => themeAttributes(theme) .map(([name, value]) => (value ? ` ${name}="${value}"` : "")) .join(""); export const readTheme = (): Theme => themeFromCookies(document.cookie); export const applyTheme = (theme: Theme): void => { const root = document.documentElement; for (const [name, value] of themeAttributes(theme)) { if (value) root.setAttribute(name, value); else root.removeAttribute(name); } const value = serializeTheme(theme); document.cookie = value === serializeTheme(DEFAULT_THEME) ? `${COOKIE}=; path=/; max-age=0; samesite=lax` : `${COOKIE}=${value}; path=/; max-age=${YEAR}; samesite=lax`; window.dispatchEvent(new Event("themechange")); };