Add dark/light mode toggle and coolors.co palettes

Admin UI:
- system / light / dark switch in the sidebar (cycle button on
  mobile), stored in localStorage and applied before first paint
- dark mode now driven by data-theme instead of only the media query

Link page themes:
- keep the Midnight and Paper presets, replace the others with the
  4 trending palettes from coolors.co
- paste a coolors.co link (or a bare hex list) to use any palette;
  the colors are read from the URL, nothing is fetched
- palettes are mapped to background, text and button colors by
  contrast, with a dark/light background choice

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 16:27:21 +02:00
co-authored by Claude Opus 5.5
parent 74ce76ab8b
commit 624517e010
13 changed files with 477 additions and 82 deletions
+4 -14
View File
@@ -10,26 +10,16 @@ import { Segmented } from "../../components/ui/form.js";
import { Stat } from "../../components/ui/misc.js";
import { Card, CardHeader, Skeleton } from "../../components/ui/surface.js";
import { useAction } from "../../components/ui/toast.js";
import { useResolvedTheme } from "../../lib/theme.js";
type Range = 7 | 30 | 90;
const cssVar = (name: string) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
// Re-renders when the system switches between light and dark, so the chart picks up the new colors.
function useColorScheme() {
const [dark, setDark] = useState(() => matchMedia("(prefers-color-scheme: dark)").matches);
useEffect(() => {
const mq = matchMedia("(prefers-color-scheme: dark)");
const update = () => setDark(mq.matches);
mq.addEventListener("change", update);
return () => mq.removeEventListener("change", update);
}, []);
return dark;
}
function Chart({ series }: { series: StatsDay[] }) {
const container = useRef<HTMLDivElement>(null);
const dark = useColorScheme();
// Redraw when light/dark mode changes so the chart picks up the new colors.
const mode = useResolvedTheme();
useEffect(() => {
const el = container.current;
@@ -70,7 +60,7 @@ function Chart({ series }: { series: StatsDay[] }) {
resize.disconnect();
plot.destroy();
};
}, [series, dark]);
}, [series, mode]);
return <div ref={container} className="w-full" />;
}
+195 -42
View File
@@ -1,23 +1,20 @@
import { useRef, useState } from "react";
import { Check, ImageUp, Trash2 } from "lucide-react";
import { useRef, useState, type ReactNode } from "react";
import { Check, ExternalLink, ImageUp, Trash2 } from "lucide-react";
import type { PageDetail } from "../../../shared/api-types.js";
import type { Theme } from "../../../shared/validation.js";
import { api } from "../../api.js";
import { Button } from "../../components/ui/button.js";
import { ColorInput, Field, Input } from "../../components/ui/form.js";
import { ColorInput, Field, Input, Segmented } from "../../components/ui/form.js";
import { Card, CardBody, CardFooter, CardHeader } from "../../components/ui/surface.js";
import { useAction } from "../../components/ui/toast.js";
import { cx } from "../../lib/cx.js";
import { stripMarkdown } from "../../lib/markdown.js";
import { parseCoolorsInput, themeFromPalette, TRENDING_PALETTES, type Variant } from "../../lib/palette.js";
import { PhoneFrame } from "./PhonePreview.js";
const PRESETS: { name: string; theme: Theme }[] = [
{ name: "Midnight", theme: { background: "#111827", text: "#e5e7eb", button: "#475569", buttonText: "#ffffff" } },
{ name: "Paper", theme: { background: "#fafaf9", text: "#1c1917", button: "#1c1917", buttonText: "#fafaf9" } },
{ name: "Violet", theme: { background: "#2e1065", text: "#ede9fe", button: "#7c3aed", buttonText: "#ffffff" } },
{ name: "Forest", theme: { background: "#052e16", text: "#dcfce7", button: "#15803d", buttonText: "#ffffff" } },
{ name: "Sunset", theme: { background: "#431407", text: "#ffedd5", button: "#ea580c", buttonText: "#ffffff" } },
{ name: "Ocean", theme: { background: "#082f49", text: "#e0f2fe", button: "#0284c7", buttonText: "#ffffff" } },
];
const COLOR_FIELDS: [keyof Theme, string][] = [
@@ -29,6 +26,194 @@ const COLOR_FIELDS: [keyof Theme, string][] = [
const sameTheme = (a: Theme, b: Theme) => COLOR_FIELDS.every(([k]) => a[k].toLowerCase() === b[k].toLowerCase());
function OptionCard({ selected, onClick, swatch, label }: { selected: boolean; onClick: () => void; swatch: ReactNode; label: string }) {
return (
<button
type="button"
onClick={onClick}
aria-pressed={selected}
className={cx(
"relative cursor-pointer overflow-hidden rounded-xl border text-left transition",
selected ? "border-accent ring-3 ring-accent/20" : "border-border hover:border-border-strong",
)}
>
{swatch}
<div className="flex items-center justify-between gap-2 px-3 py-2 text-sm font-medium">
<span className="truncate">{label}</span>
{selected && <Check className="size-4 shrink-0 text-accent" />}
</div>
</button>
);
}
function PaletteStrip({ colors }: { colors: string[] }) {
return (
<div className="flex h-14">
{colors.map((c, i) => (
<div key={i} className="flex-1" style={{ background: c }} />
))}
</div>
);
}
function SectionLabel({ children, actions }: { children: ReactNode; actions?: ReactNode }) {
return (
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
<h3 className="text-sm font-medium">{children}</h3>
{actions}
</div>
);
}
// Theme picker: basic presets, coolors.co palettes (trending or pasted link) and custom colors.
function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (theme: Theme) => void }) {
const [variant, setVariant] = useState<Variant>("dark");
// The palette the current theme was generated from, so switching dark/light can re-map it.
const [source, setSource] = useState<string[] | null>(null);
const [link, setLink] = useState("");
const [imported, setImported] = useState<string[] | null>(null);
const [linkError, setLinkError] = useState<string>();
const applyPalette = (colors: string[], v = variant) => {
setSource(colors);
onChange(themeFromPalette(colors, v));
};
const changeVariant = (v: Variant) => {
setVariant(v);
if (source) onChange(themeFromPalette(source, v));
};
const importLink = () => {
const colors = parseCoolorsInput(link);
if (!colors) {
setLinkError("That doesn't look like a coolors.co palette link.");
return;
}
setLinkError(undefined);
setImported(colors);
applyPalette(colors);
};
const isSelected = (colors: string[]) => source === colors && sameTheme(themeFromPalette(colors, variant), theme);
return (
<div className="flex flex-col gap-7">
<div>
<SectionLabel>Basic</SectionLabel>
<div className="grid grid-cols-2 gap-3">
{PRESETS.map((preset) => (
<OptionCard
key={preset.name}
label={preset.name}
selected={sameTheme(preset.theme, theme)}
onClick={() => {
setSource(null);
onChange(preset.theme);
}}
swatch={
<div className="flex h-14 flex-col items-center justify-center gap-1.5 px-6" style={{ background: preset.theme.background }}>
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
</div>
}
/>
))}
</div>
</div>
<div>
<SectionLabel
actions={
<Segmented
ariaLabel="Background for palettes"
value={variant}
onChange={changeVariant}
options={[
{ value: "dark", label: "Dark background" },
{ value: "light", label: "Light background" },
]}
/>
}
>
Trending on{" "}
<a href="https://coolors.co/palettes/trending" target="_blank" rel="noreferrer" className="text-accent hover:underline">
coolors.co
</a>
</SectionLabel>
<div className="grid grid-cols-2 gap-3">
{TRENDING_PALETTES.map((p) => (
<OptionCard
key={p.name}
label={p.name}
selected={isSelected(p.colors)}
onClick={() => applyPalette(p.colors)}
swatch={<PaletteStrip colors={p.colors} />}
/>
))}
</div>
</div>
<div>
<SectionLabel>Import from coolors.co</SectionLabel>
<form
className="flex gap-2"
onSubmit={(e) => {
e.preventDefault();
importLink();
}}
>
<Input
aria-label="coolors.co link"
placeholder="https://coolors.co/palette/264653-2a9d8f-e9c46a-f4a261-e76f51"
value={link}
onChange={(e) => {
setLink(e.target.value);
setLinkError(undefined);
}}
/>
<Button type="submit" variant="secondary" disabled={!link.trim()}>
Apply
</Button>
</form>
{linkError ? (
<p className="mt-1.5 text-xs text-danger">{linkError}</p>
) : (
<p className="mt-1.5 text-xs text-muted">
Create or pick a palette on{" "}
<a href="https://coolors.co/generate" target="_blank" rel="noreferrer" className="inline-flex items-center gap-0.5 underline">
coolors.co <ExternalLink className="size-3" />
</a>{" "}
and paste its URL here.
</p>
)}
{imported && (
<div className="mt-3 grid grid-cols-2 gap-3">
<OptionCard label="Imported palette" selected={isSelected(imported)} onClick={() => applyPalette(imported)} swatch={<PaletteStrip colors={imported} />} />
</div>
)}
</div>
<div>
<SectionLabel>Custom colors</SectionLabel>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
{COLOR_FIELDS.map(([key, label]) => (
<div key={key} className="flex flex-col gap-1.5">
<span className="text-xs text-muted">{label}</span>
<ColorInput
label={label}
value={theme[key]}
onChange={(v) => {
setSource(null);
onChange({ ...theme, [key]: v });
}}
/>
</div>
))}
</div>
</div>
</div>
);
}
// Renders the page with unsaved settings so changes are visible while editing.
function MockPreview({ title, avatarUrl, theme, page }: { title: string; avatarUrl: string | null; theme: Theme; page: PageDetail }) {
return (
@@ -125,41 +310,9 @@ export function AppearanceTab({ page, onChange }: { page: PageDetail; onChange:
</Card>
<Card>
<CardHeader title="Theme" description="Pick a preset or set your own colors." />
<CardBody className="flex flex-col gap-6">
<div className="grid grid-cols-2 gap-3 sm:grid-cols-3">
{PRESETS.map((preset) => {
const selected = sameTheme(preset.theme, theme);
return (
<button
key={preset.name}
type="button"
onClick={() => setTheme(preset.theme)}
className={cx(
"relative cursor-pointer overflow-hidden rounded-xl border text-left transition",
selected ? "border-accent ring-3 ring-accent/20" : "border-border hover:border-border-strong",
)}
>
<div className="flex h-16 flex-col items-center justify-center gap-1.5 px-6" style={{ background: preset.theme.background }}>
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
<div className="h-2.5 w-full rounded-full" style={{ background: preset.theme.button }} />
</div>
<div className="flex items-center justify-between px-3 py-2 text-sm font-medium">
{preset.name}
{selected && <Check className="size-4 text-accent" />}
</div>
</button>
);
})}
</div>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
{COLOR_FIELDS.map(([key, label]) => (
<div key={key} className="flex flex-col gap-1.5">
<span className="text-sm font-medium">{label}</span>
<ColorInput label={label} value={theme[key]} onChange={(v) => setTheme({ ...theme, [key]: v })} />
</div>
))}
</div>
<CardHeader title="Theme" description="Pick a preset, use a palette from coolors.co or set your own colors." />
<CardBody>
<ThemePicker theme={theme} onChange={setTheme} />
</CardBody>
<CardFooter>
{dirty && <span className="mr-auto text-sm text-muted">Unsaved changes</span>}