Replace the palette dark/light toggle with per-palette thumbnails

The global "Dark background / Light background" switch sat in the
trending header but also affected saved palettes and meant nothing
for single colors. Each palette now shows two small page thumbnails
(dark and light version) that apply exactly what they show; the
selected one is highlighted, and editing a single color deselects it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 16:56:27 +02:00
co-authored by Claude Opus 5.5
parent 35cb7da929
commit 767290e1b5
+57 -49
View File
@@ -5,7 +5,7 @@ import type { Theme } from "../../../shared/validation.js";
import { api, errorMessage, useApi } from "../../api.js"; import { api, errorMessage, useApi } from "../../api.js";
import { Button } from "../../components/ui/button.js"; import { Button } from "../../components/ui/button.js";
import { useConfirm } from "../../components/ui/dialog.js"; import { useConfirm } from "../../components/ui/dialog.js";
import { ColorInput, Input, Segmented } from "../../components/ui/form.js"; import { ColorInput, Input } from "../../components/ui/form.js";
import { useToast } from "../../components/ui/toast.js"; import { useToast } from "../../components/ui/toast.js";
import { cx } from "../../lib/cx.js"; import { cx } from "../../lib/cx.js";
import { parseCoolorsInput, themeFromPalette, TRENDING_PALETTES, type Variant } from "../../lib/palette.js"; import { parseCoolorsInput, themeFromPalette, TRENDING_PALETTES, type Variant } from "../../lib/palette.js";
@@ -59,7 +59,29 @@ function PresetCard({ name, theme, selected, onClick }: { name: string; theme: T
); );
} }
// A palette: each color opens the role menu, the name row applies the whole palette. // Miniature of a link page (background, a text line and a button) that applies one version of a palette.
function VariantThumb({ theme, label, selected, onClick }: { theme: Theme; label: string; selected: boolean; onClick: () => void }) {
return (
<button
type="button"
onClick={onClick}
aria-pressed={selected}
aria-label={label}
title={label}
className={cx(
"flex h-8 w-11 shrink-0 cursor-pointer flex-col items-center justify-center gap-1 rounded-md border px-1.5 transition",
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent",
selected ? "border-accent ring-2 ring-accent/40" : "border-border hover:scale-105 hover:border-border-strong",
)}
style={{ background: theme.background }}
>
<span className="h-1 w-3/5 rounded-full" style={{ background: theme.text }} />
<span className="h-2 w-full rounded-sm" style={{ background: theme.button }} />
</button>
);
}
// A palette: each color opens the role menu, the two thumbnails apply a dark or light version of it.
function PaletteCard({ function PaletteCard({
colors, colors,
label, label,
@@ -70,14 +92,14 @@ function PaletteCard({
}: { }: {
colors: string[]; colors: string[];
label: string; label: string;
selected: boolean; selected: Variant | null;
onApply: () => void; onApply: (variant: Variant) => void;
onPickColor: (color: string, anchor: HTMLElement) => void; onPickColor: (color: string, anchor: HTMLElement) => void;
onRemove?: () => void; onRemove?: () => void;
}) { }) {
return ( return (
<div className={cardClass(selected)}> <div className={cardClass(selected !== null)}>
<div className="flex h-14"> <div className="flex h-12">
{colors.map((c, i) => ( {colors.map((c, i) => (
<button <button
key={i} key={i}
@@ -91,23 +113,24 @@ function PaletteCard({
/> />
))} ))}
</div> </div>
<div className="flex items-center"> <div className="flex items-center gap-1.5 py-2 pr-2 pl-3">
<button <span className="min-w-0 flex-1 truncate text-sm font-medium">{label}</span>
type="button" {(["dark", "light"] as const).map((v) => (
onClick={onApply} <VariantThumb
aria-pressed={selected} key={v}
className="flex min-w-0 flex-1 cursor-pointer items-center justify-between gap-2 px-3 py-2 text-left text-sm font-medium transition-colors hover:bg-surface-2/60" theme={themeFromPalette(colors, v)}
> label={`Use ${label} with a ${v} background`}
<span className="truncate">{label}</span> selected={selected === v}
{selected && <Check className="size-4 shrink-0 text-accent" />} onClick={() => onApply(v)}
</button> />
))}
{onRemove && ( {onRemove && (
<button <button
type="button" type="button"
onClick={onRemove} onClick={onRemove}
aria-label={`Remove ${label}`} aria-label={`Remove ${label}`}
title="Remove palette" title="Remove palette"
className="cursor-pointer self-stretch px-2.5 text-muted transition-colors hover:bg-surface-2/60 hover:text-danger" className="ml-0.5 flex size-8 shrink-0 cursor-pointer items-center justify-center rounded-md text-muted transition-colors hover:bg-surface-2 hover:text-danger"
> >
<X className="size-4" /> <X className="size-4" />
</button> </button>
@@ -222,9 +245,8 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
const saved = useApi<SavedPalette[]>("/palettes"); const saved = useApi<SavedPalette[]>("/palettes");
const toast = useToast(); const toast = useToast();
const confirm = useConfirm(); const confirm = useConfirm();
const [variant, setVariant] = useState<Variant>("dark"); // The palette version the current theme was generated from, to mark it as selected.
// The palette the current theme was generated from, so switching dark/light can re-map it. const [source, setSource] = useState<{ colors: string[]; variant: Variant } | null>(null);
const [source, setSource] = useState<string[] | null>(null);
const [link, setLink] = useState(""); const [link, setLink] = useState("");
const [linkError, setLinkError] = useState<string>(); const [linkError, setLinkError] = useState<string>();
const [importing, setImporting] = useState(false); const [importing, setImporting] = useState(false);
@@ -232,21 +254,20 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
const toggleMenu = (color: string, anchor: HTMLElement) => setMenu((m) => (m?.anchor === anchor ? null : { color, anchor })); const toggleMenu = (color: string, anchor: HTMLElement) => setMenu((m) => (m?.anchor === anchor ? null : { color, anchor }));
const applyPalette = (colors: string[], v = variant) => { const applyPalette = (colors: string[], variant: Variant) => {
setSource(colors); setSource({ colors, variant });
onChange(themeFromPalette(colors, v)); onChange(themeFromPalette(colors, variant));
};
const changeVariant = (v: Variant) => {
setVariant(v);
if (source) onChange(themeFromPalette(source, v));
}; };
const setColor = (role: keyof Theme, color: string) => { const setColor = (role: keyof Theme, color: string) => {
setSource(null); setSource(null);
onChange({ ...theme, [role]: color }); onChange({ ...theme, [role]: color });
}; };
const isSelected = (colors: string[]) => // Which version of this palette is in use, if any (editing a single color deselects it).
!!source && paletteKey(source) === paletteKey(colors) && sameTheme(themeFromPalette(colors, variant), theme); const selectedVariant = (colors: string[]): Variant | null =>
source && paletteKey(source.colors) === paletteKey(colors) && sameTheme(themeFromPalette(colors, source.variant), theme)
? source.variant
: null;
const importLink = async () => { const importLink = async () => {
const colors = parseCoolorsInput(link); const colors = parseCoolorsInput(link);
@@ -258,7 +279,7 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
try { try {
await api<SavedPalette>("POST", "/palettes", { colors }); await api<SavedPalette>("POST", "/palettes", { colors });
await saved.reload(); await saved.reload();
applyPalette(colors); applyPalette(colors, "dark");
setLink(""); setLink("");
setLinkError(undefined); setLinkError(undefined);
} catch (err) { } catch (err) {
@@ -280,7 +301,7 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
} }
}; };
const pickHint = "Click a name to use the whole palette, or a single color to assign it."; const pickHint = "Pick the dark or light version of a palette, or click a single color to use it for one part.";
return ( return (
<div className="flex flex-col gap-7"> <div className="flex flex-col gap-7">
@@ -303,20 +324,7 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
</div> </div>
<div> <div>
<SectionLabel <SectionLabel hint={pickHint}>
hint={pickHint}
actions={
<Segmented
ariaLabel="Background for palettes"
value={variant}
onChange={changeVariant}
options={[
{ value: "dark", label: "Dark background" },
{ value: "light", label: "Light background" },
]}
/>
}
>
Trending on{" "} Trending on{" "}
<a href="https://coolors.co/palettes/trending" target="_blank" rel="noreferrer" className="text-accent hover:underline"> <a href="https://coolors.co/palettes/trending" target="_blank" rel="noreferrer" className="text-accent hover:underline">
coolors.co coolors.co
@@ -328,8 +336,8 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
key={p.name} key={p.name}
label={p.name} label={p.name}
colors={p.colors} colors={p.colors}
selected={isSelected(p.colors)} selected={selectedVariant(p.colors)}
onApply={() => applyPalette(p.colors)} onApply={(v) => applyPalette(p.colors, v)}
onPickColor={toggleMenu} onPickColor={toggleMenu}
/> />
))} ))}
@@ -376,8 +384,8 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
key={p.id} key={p.id}
label={`Imported ${new Date(p.createdAt).toLocaleDateString(undefined, { month: "short", day: "numeric" })}`} label={`Imported ${new Date(p.createdAt).toLocaleDateString(undefined, { month: "short", day: "numeric" })}`}
colors={p.colors} colors={p.colors}
selected={isSelected(p.colors)} selected={selectedVariant(p.colors)}
onApply={() => applyPalette(p.colors)} onApply={(v) => applyPalette(p.colors, v)}
onPickColor={toggleMenu} onPickColor={toggleMenu}
onRemove={() => void removePalette(p)} onRemove={() => void removePalette(p)}
/> />