// The theme choice: the system's (no cookie), or an explicit light or dark. // // Stored in a cookie rather than localStorage because the ROOT LAYOUT reads it: // the server stamps `data-theme` on before a byte of CSS is applied, so an // explicit choice paints right on the first frame -- no inline script, no flash, // nothing for hydration to correct. "auto" is the absence of the cookie, which is // also what a cleared or refused cookie reads as. // // Pure, so the client control and the server layout share it. export const THEME_COOKIE = "umtool-theme"; export const THEMES = ["auto", "light", "dark"] as const; export type Theme = (typeof THEMES)[number]; /** A cookie value read back is untrusted: anything but light/dark is auto. */ export function parseTheme(v: string | undefined | null): Theme { return v === "light" || v === "dark" ? v : "auto"; } /** The `data-theme` attribute for a choice; auto leaves the system in charge. */ export function themeAttr(t: Theme): "light" | "dark" | undefined { return t === "auto" ? undefined : t; } /** One year, path-wide; Lax because nothing cross-site ever needs it. */ export function themeCookie(t: Theme): string { return t === "auto" ? `${THEME_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax` : `${THEME_COOKIE}=${t}; Path=/; Max-Age=31536000; SameSite=Lax`; }