"use client"; // Dev-only theme switcher for A/B-ing fonts and palettes on the real pages. // It overrides the CSS variables from app/globals.css on ; once a combo is // picked, bake it into globals.css (and layout.tsx for fonts) and it ships. // Rendered only in development, see app/layout.tsx. import { useCallback, useEffect, useState } from "react"; import type { FontOption } from "./themeFonts"; type Config = { sans: string; serif: string; tone: string; accent: string }; type Slot = "A" | "B"; type Stored = { current: Config; A?: Config; B?: Config }; const STORAGE_KEY = "dev-theme-switcher-v2"; const DEFAULT: Config = { sans: "manrope", serif: "garamond", tone: "zinc", accent: "gold" }; // Tailwind v4 grey scales, copied from node_modules/tailwindcss/theme.css. const TONES: Record }> = { neutral: { label: "Neutral (previous)", scale: { "50": "oklch(98.5% 0 0)", "100": "oklch(97% 0 0)", "200": "oklch(92.2% 0 0)", "300": "oklch(87% 0 0)", "400": "oklch(70.8% 0 0)", "500": "oklch(55.6% 0 0)", "600": "oklch(43.9% 0 0)", "700": "oklch(37.1% 0 0)", "800": "oklch(26.9% 0 0)", "900": "oklch(20.5% 0 0)", "950": "oklch(14.5% 0 0)" }, }, stone: { label: "Stone (warm)", scale: { "50": "oklch(98.5% 0.001 106.423)", "100": "oklch(97% 0.001 106.424)", "200": "oklch(92.3% 0.003 48.717)", "300": "oklch(86.9% 0.005 56.366)", "400": "oklch(70.9% 0.01 56.259)", "500": "oklch(55.3% 0.013 58.071)", "600": "oklch(44.4% 0.011 73.639)", "700": "oklch(37.4% 0.01 67.558)", "800": "oklch(26.8% 0.007 34.298)", "900": "oklch(21.6% 0.006 56.043)", "950": "oklch(14.7% 0.004 49.25)" }, }, zinc: { label: "Zinc (current)", scale: { "50": "oklch(98.5% 0 0)", "100": "oklch(96.7% 0.001 286.375)", "200": "oklch(92% 0.004 286.32)", "300": "oklch(87.1% 0.006 286.286)", "400": "oklch(70.5% 0.015 286.067)", "500": "oklch(55.2% 0.016 285.938)", "600": "oklch(44.2% 0.017 285.786)", "700": "oklch(37% 0.013 285.805)", "800": "oklch(27.4% 0.006 286.033)", "900": "oklch(21% 0.006 285.885)", "950": "oklch(14.1% 0.005 285.823)" }, }, gray: { label: "Gray (blue hint)", scale: { "50": "oklch(98.5% 0.002 247.839)", "100": "oklch(96.7% 0.003 264.542)", "200": "oklch(92.8% 0.006 264.531)", "300": "oklch(87.2% 0.01 258.338)", "400": "oklch(70.7% 0.022 261.325)", "500": "oklch(55.1% 0.027 264.364)", "600": "oklch(44.6% 0.03 256.802)", "700": "oklch(37.3% 0.034 259.733)", "800": "oklch(27.8% 0.033 256.848)", "900": "oklch(21% 0.034 264.665)", "950": "oklch(13% 0.028 261.692)" }, }, slate: { label: "Slate (blue)", scale: { "50": "oklch(98.4% 0.003 247.858)", "100": "oklch(96.8% 0.007 247.896)", "200": "oklch(92.9% 0.013 255.508)", "300": "oklch(86.9% 0.022 252.894)", "400": "oklch(70.4% 0.04 256.788)", "500": "oklch(55.4% 0.046 257.417)", "600": "oklch(44.6% 0.043 257.281)", "700": "oklch(37.2% 0.044 257.287)", "800": "oklch(27.9% 0.041 260.031)", "900": "oklch(20.8% 0.042 265.755)", "950": "oklch(12.9% 0.042 264.695)" }, }, }; const ACCENTS: Record = { gold: { label: "Gold (current)", value: "#d4a24a", hover: "#e6b966" }, ember: { label: "Ember", value: "#e07b3c" }, coral: { label: "Coral", value: "#ef7a6a" }, rose: { label: "Rose", value: "#e8829f" }, sage: { label: "Sage", value: "#94b88a" }, teal: { label: "Teal", value: "#3cbfae" }, iris: { label: "Iris", value: "#8f8cf5" }, sky: { label: "Sky", value: "#5ba8f0" }, }; function readStored(): Stored | null { try { const raw = localStorage.getItem(STORAGE_KEY); return raw ? (JSON.parse(raw) as Stored) : null; } catch { return null; } } function writeStored(value: Stored) { try { localStorage.setItem(STORAGE_KEY, JSON.stringify(value)); } catch { // storage unavailable, the switcher still works for this page view } } function applyConfig(cfg: Config, sans: FontOption[], serif: FontOption[]) { const root = document.documentElement.style; const sansOpt = sans.find((o) => o.id === cfg.sans) ?? sans[0]; const serifOpt = serif.find((o) => o.id === cfg.serif) ?? serif[0]; root.setProperty("--site-sans", `var(${sansOpt.variable})`); root.setProperty("--site-serif", `var(${serifOpt.variable})`); const tone = TONES[cfg.tone] ?? TONES.neutral; for (const [step, value] of Object.entries(tone.scale)) root.setProperty(`--n-${step}`, value); if (cfg.tone === "neutral") { // The original site values, so "Neutral" matches the old look exactly. root.setProperty("--background", "#0a0a0a"); root.setProperty("--foreground", "#ededed"); root.setProperty("--bg-elevated", "#1c1c1c"); } else { root.setProperty("--background", "var(--n-950)"); root.setProperty("--foreground", "color-mix(in oklab, var(--n-100), var(--n-200) 60%)"); root.setProperty("--bg-elevated", "color-mix(in oklab, var(--n-900), var(--n-800) 30%)"); } const accent = ACCENTS[cfg.accent] ?? ACCENTS.gold; root.setProperty("--accent", accent.value); root.setProperty("--accent-hover", accent.hover ?? `color-mix(in oklab, ${accent.value} 75%, white)`); } function describe(cfg: Config, sans: FontOption[], serif: FontOption[]) { const strip = (s: string) => s.replace(/ \(.*\)$/, ""); return [ strip(sans.find((o) => o.id === cfg.sans)?.label ?? cfg.sans), strip(serif.find((o) => o.id === cfg.serif)?.label ?? cfg.serif), strip(TONES[cfg.tone]?.label ?? cfg.tone), strip(ACCENTS[cfg.accent]?.label ?? cfg.accent), ].join(" · "); } // navigator.clipboard only exists on secure origins (https or localhost). The dev // server is usually opened by LAN IP over plain http, so fall back to the old // hidden-textarea + execCommand trick, which still works there. async function copyText(text: string): Promise { try { if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(text); return true; } } catch { // fall through to the legacy path } const area = document.createElement("textarea"); area.value = text; area.setAttribute("readonly", ""); area.style.position = "fixed"; area.style.opacity = "0"; document.body.appendChild(area); area.select(); let ok = false; try { ok = document.execCommand("copy"); } catch { ok = false; } area.remove(); return ok; } const panel: React.CSSProperties = { fontFamily: "ui-sans-serif, system-ui, sans-serif", background: "#111", color: "#ddd", border: "1px solid #333", }; const field: React.CSSProperties = { background: "#1b1b1b", color: "#eee", border: "1px solid #333" }; export default function ThemeSwitcher({ sansOptions, serifOptions, }: { sansOptions: FontOption[]; serifOptions: FontOption[]; }) { const [open, setOpen] = useState(true); const [stored, setStored] = useState({ current: DEFAULT }); const [activeSlot, setActiveSlot] = useState(null); const [copied, setCopied] = useState(false); // Restore after mount (localStorage isn't available during SSR). useEffect(() => { const saved = readStored(); if (saved) setStored({ ...saved, current: { ...DEFAULT, ...saved.current } }); }, []); useEffect(() => { applyConfig(stored.current, sansOptions, serifOptions); writeStored(stored); }, [stored, sansOptions, serifOptions]); const update = (patch: Partial) => { setActiveSlot(null); setStored((s) => ({ ...s, current: { ...s.current, ...patch } })); }; const save = (slot: Slot) => { setStored((s) => ({ ...s, [slot]: s.current })); setActiveSlot(slot); }; const show = useCallback((slot: Slot) => { setStored((s) => (s[slot] ? { ...s, current: s[slot]! } : s)); setActiveSlot(slot); }, []); // Backtick flips between A and B, unless typing in a field. useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key !== "`") return; const tag = (e.target as HTMLElement | null)?.tagName; if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT") return; if (!stored.A || !stored.B) return; e.preventDefault(); show(activeSlot === "A" ? "B" : "A"); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [stored.A, stored.B, activeSlot, show]); const summary = describe(stored.current, sansOptions, serifOptions); if (!open) { return ( ); } const select = (label: string, value: string, options: { id: string; label: string }[], key: keyof Config) => ( ); const slotButton = (slot: Slot) => { const has = Boolean(stored[slot]); const active = activeSlot === slot; return (
); }; return (
Theme (dev only)
{select("Body font", stored.current.sans, sansOptions, "sans")} {select("Display font", stored.current.serif, serifOptions, "serif")} {select("Greys", stored.current.tone, Object.entries(TONES).map(([id, t]) => ({ id, label: t.label })), "tone")} {select("Accent", stored.current.accent, Object.entries(ACCENTS).map(([id, a]) => ({ id, label: a.label })), "accent")}
{slotButton("A")}{slotButton("B")}

Save two combos, then press ` to flip between them.

{summary}
); }