Choose a palette's dark or light version from a dropdown

The two thumbnail buttons on each palette card are replaced by a menu
on the card's name row, like the one for single colors: "Dark
background" and "Light background", each with a small preview. The
active version is shown next to the name. Both menus now share one
popover component.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-14 17:22:09 +02:00
co-authored by Claude Opus 5.5
parent 03fb4dcf0c
commit 6d427db2c4
+121 -88
View File
@@ -1,5 +1,5 @@
import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from "react"; import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
import { Check, ExternalLink, X } from "lucide-react"; import { Check, ChevronDown, ExternalLink, X } from "lucide-react";
import type { SavedPalette } from "../../../shared/api-types.js"; import type { SavedPalette } from "../../../shared/api-types.js";
import type { Theme } from "../../../shared/validation.js"; import type { Theme } from "../../../shared/validation.js";
import { api, errorMessage, useApi } from "../../api.js"; import { api, errorMessage, useApi } from "../../api.js";
@@ -26,6 +26,8 @@ const PRESETS: { name: string; theme: Theme }[] = [
const paletteKey = (colors: string[]) => colors.join("-").toLowerCase(); const paletteKey = (colors: string[]) => colors.join("-").toLowerCase();
const paletteLabel = (p: SavedPalette) => `Imported ${new Date(p.createdAt).toLocaleDateString(undefined, { month: "short", day: "numeric" })}`;
const cardClass = (selected: boolean) => const cardClass = (selected: boolean) =>
cx( cx(
"relative overflow-hidden rounded-xl border transition", "relative overflow-hidden rounded-xl border transition",
@@ -59,41 +61,33 @@ function PresetCard({ name, theme, selected, onClick }: { name: string; theme: T
); );
} }
// Miniature of a link page (background, a text line and a button) that applies one version of a palette. // Miniature of a link page: background, a line of text and a button.
function VariantThumb({ theme, label, selected, onClick }: { theme: Theme; label: string; selected: boolean; onClick: () => void }) { function ThemeThumb({ theme }: { theme: Theme }) {
return ( return (
<button <span
type="button" className="flex h-6 w-9 shrink-0 flex-col items-center justify-center gap-0.5 rounded border border-border px-1"
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 }} style={{ background: theme.background }}
aria-hidden
> >
<span className="h-1 w-3/5 rounded-full" style={{ background: theme.text }} /> <span className="h-0.5 w-3/5 rounded-full" style={{ background: theme.text }} />
<span className="h-2 w-full rounded-sm" style={{ background: theme.button }} /> <span className="h-1.5 w-full rounded-sm" style={{ background: theme.button }} />
</button> </span>
); );
} }
// A palette: each color opens the role menu, the two thumbnails apply a dark or light version of it. // A palette card: each color opens the role menu, the name row opens the dark/light menu.
function PaletteCard({ function PaletteCard({
colors, colors,
label, label,
selected, selected,
onApply, onOpenVariants,
onPickColor, onPickColor,
onRemove, onRemove,
}: { }: {
colors: string[]; colors: string[];
label: string; label: string;
selected: Variant | null; selected: Variant | null;
onApply: (variant: Variant) => void; onOpenVariants: (anchor: HTMLElement) => void;
onPickColor: (color: string, anchor: HTMLElement) => void; onPickColor: (color: string, anchor: HTMLElement) => void;
onRemove?: () => void; onRemove?: () => void;
}) { }) {
@@ -113,24 +107,28 @@ function PaletteCard({
/> />
))} ))}
</div> </div>
<div className="flex items-center gap-1.5 py-2 pr-2 pl-3"> <div className="flex items-center">
<span className="min-w-0 flex-1 truncate text-sm font-medium">{label}</span> <button
{(["dark", "light"] as const).map((v) => ( type="button"
<VariantThumb aria-haspopup="menu"
key={v} aria-label={`Use ${label}…`}
theme={themeFromPalette(colors, v)} onClick={(e) => onOpenVariants(e.currentTarget)}
label={`Use ${label} with a ${v} background`} className="flex min-w-0 flex-1 cursor-pointer items-center gap-2 px-3 py-2 text-left text-sm font-medium outline-none transition-colors hover:bg-surface-2/60 focus-visible:bg-surface-2/60 focus-visible:ring-2 focus-visible:ring-accent/40 focus-visible:ring-inset"
selected={selected === v} >
onClick={() => onApply(v)} <span className="truncate">{label}</span>
/> {selected && <span className="shrink-0 text-xs font-normal text-muted">{selected === "dark" ? "Dark" : "Light"}</span>}
))} <span className="ml-auto flex shrink-0 items-center gap-1">
{selected && <Check className="size-4 text-accent" />}
<ChevronDown className="size-4 text-muted" />
</span>
</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="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" className="cursor-pointer self-stretch px-2.5 text-muted outline-none transition-colors hover:bg-surface-2/60 hover:text-danger focus-visible:ring-2 focus-visible:ring-accent/40 focus-visible:ring-inset"
> >
<X className="size-4" /> <X className="size-4" />
</button> </button>
@@ -140,24 +138,24 @@ function PaletteCard({
); );
} }
// Small menu next to a clicked color: "use as background / text / buttons / button text". // Small menu anchored to an element: placed below it (or above when there's no room), closes on outside
function ColorRoleMenu({ // click, Escape, scroll and resize, and supports arrow keys.
color, function PopoverMenu({
anchor, anchor,
theme, label,
onPick, header,
onClose, onClose,
children,
}: { }: {
color: string;
anchor: HTMLElement; anchor: HTMLElement;
theme: Theme; label: string;
onPick: (role: keyof Theme) => void; header: ReactNode;
onClose: () => void; onClose: () => void;
children: ReactNode;
}) { }) {
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const [position, setPosition] = useState<{ top: number; left: number } | null>(null); const [position, setPosition] = useState<{ top: number; left: number } | null>(null);
// Place below the color (or above when there's no room), kept inside the viewport.
useLayoutEffect(() => { useLayoutEffect(() => {
const menu = ref.current; const menu = ref.current;
if (!menu) return; if (!menu) return;
@@ -176,7 +174,7 @@ function ColorRoleMenu({
useEffect(() => { useEffect(() => {
const outside = (e: PointerEvent) => { const outside = (e: PointerEvent) => {
if (!ref.current?.contains(e.target as Node) && e.target !== anchor) onClose(); if (!ref.current?.contains(e.target as Node) && !anchor.contains(e.target as Node)) onClose();
}; };
const dismiss = () => onClose(); const dismiss = () => onClose();
const escape = (e: globalThis.KeyboardEvent) => { const escape = (e: globalThis.KeyboardEvent) => {
@@ -197,49 +195,48 @@ function ColorRoleMenu({
}, [anchor, onClose]); }, [anchor, onClose]);
const onKeyDown = (e: KeyboardEvent) => { const onKeyDown = (e: KeyboardEvent) => {
if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
e.preventDefault();
const items = [...(ref.current?.querySelectorAll<HTMLButtonElement>("[role=menuitem]") ?? [])]; const items = [...(ref.current?.querySelectorAll<HTMLButtonElement>("[role=menuitem]") ?? [])];
const index = items.indexOf(document.activeElement as HTMLButtonElement); const index = items.indexOf(document.activeElement as HTMLButtonElement);
if (e.key === "ArrowDown" || e.key === "ArrowUp") { items[(index + (e.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus();
e.preventDefault();
const next = (index + (e.key === "ArrowDown" ? 1 : -1) + items.length) % items.length;
items[next]?.focus();
}
}; };
return ( return (
<div <div
ref={ref} ref={ref}
role="menu" role="menu"
aria-label={`Use ${color.toUpperCase()} for`} aria-label={label}
onKeyDown={onKeyDown} onKeyDown={onKeyDown}
className="fixed z-50 w-56 animate-pop-in rounded-xl border border-border bg-surface p-1 shadow-xl" className="fixed z-50 w-56 animate-pop-in rounded-xl border border-border bg-surface p-1 shadow-xl"
style={position ?? { visibility: "hidden", top: 0, left: 0 }} style={position ?? { visibility: "hidden", top: 0, left: 0 }}
> >
<div className="flex items-center gap-2 px-2.5 pt-1.5 pb-2 text-xs text-muted"> <div className="flex items-center gap-2 px-2.5 pt-1.5 pb-2 text-xs text-muted">{header}</div>
<span className="size-4 rounded border border-border" style={{ background: color }} /> {children}
<span className="font-mono uppercase">{color}</span>
<span>use for…</span>
</div>
{COLOR_FIELDS.map(([key, label]) => {
const current = theme[key].toLowerCase() === color.toLowerCase();
return (
<button
key={key}
type="button"
role="menuitem"
onClick={() => onPick(key)}
className="flex w-full cursor-pointer items-center gap-2.5 rounded-lg px-2.5 py-2 text-left text-sm outline-none hover:bg-surface-2 focus-visible:bg-surface-2"
>
<span className="size-4 rounded border border-border" style={{ background: theme[key] }} title={`Current: ${theme[key]}`} />
<span className="flex-1">{label}</span>
{current && <Check className="size-4 text-accent" />}
</button>
);
})}
</div> </div>
); );
} }
function MenuItem({ leading, label, checked, onClick }: { leading: ReactNode; label: string; checked: boolean; onClick: () => void }) {
return (
<button
type="button"
role="menuitem"
onClick={onClick}
className="flex w-full cursor-pointer items-center gap-2.5 rounded-lg px-2.5 py-2 text-left text-sm outline-none hover:bg-surface-2 focus-visible:bg-surface-2"
>
{leading}
<span className="flex-1">{label}</span>
{checked && <Check className="size-4 text-accent" />}
</button>
);
}
type OpenMenu =
| { kind: "color"; color: string; anchor: HTMLElement }
| { kind: "variant"; colors: string[]; label: string; anchor: HTMLElement }
| null;
// Theme picker: basic presets, trending and saved coolors.co palettes, and custom colors. // Theme picker: basic presets, trending and saved coolors.co palettes, and custom colors.
export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (theme: Theme) => void }) { export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (theme: Theme) => void }) {
const saved = useApi<SavedPalette[]>("/palettes"); const saved = useApi<SavedPalette[]>("/palettes");
@@ -250,9 +247,14 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
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);
const [menu, setMenu] = useState<{ color: string; anchor: HTMLElement } | null>(null); const [menu, setMenu] = useState<OpenMenu>(null);
const toggleMenu = (color: string, anchor: HTMLElement) => setMenu((m) => (m?.anchor === anchor ? null : { color, anchor })); // Clicking the element that opened a menu again closes it.
const toggleMenu = (next: NonNullable<OpenMenu>) => setMenu((m) => (m?.anchor === next.anchor ? null : next));
const closeMenu = (refocus = false) => {
if (refocus) menu?.anchor.focus();
setMenu(null);
};
const applyPalette = (colors: string[], variant: Variant) => { const applyPalette = (colors: string[], variant: Variant) => {
setSource({ colors, variant }); setSource({ colors, variant });
@@ -301,7 +303,7 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
} }
}; };
const pickHint = "Pick the dark or light version of a palette, or click a single color to use it for one part."; const pickHint = "Click a palette's name to use it with a dark or light background, or 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">
@@ -334,8 +336,8 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
label={p.name} label={p.name}
colors={p.colors} colors={p.colors}
selected={selectedVariant(p.colors)} selected={selectedVariant(p.colors)}
onApply={(v) => applyPalette(p.colors, v)} onOpenVariants={(anchor) => toggleMenu({ kind: "variant", colors: p.colors, label: p.name, anchor })}
onPickColor={toggleMenu} onPickColor={(color, anchor) => toggleMenu({ kind: "color", color, anchor })}
/> />
))} ))}
</div> </div>
@@ -379,11 +381,11 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
{saved.data.map((p) => ( {saved.data.map((p) => (
<PaletteCard <PaletteCard
key={p.id} key={p.id}
label={`Imported ${new Date(p.createdAt).toLocaleDateString(undefined, { month: "short", day: "numeric" })}`} label={paletteLabel(p)}
colors={p.colors} colors={p.colors}
selected={selectedVariant(p.colors)} selected={selectedVariant(p.colors)}
onApply={(v) => applyPalette(p.colors, v)} onOpenVariants={(anchor) => toggleMenu({ kind: "variant", colors: p.colors, label: paletteLabel(p), anchor })}
onPickColor={toggleMenu} onPickColor={(color, anchor) => toggleMenu({ kind: "color", color, anchor })}
onRemove={() => void removePalette(p)} onRemove={() => void removePalette(p)}
/> />
))} ))}
@@ -403,18 +405,49 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
</div> </div>
</div> </div>
{menu && ( {menu?.kind === "color" && (
<ColorRoleMenu <PopoverMenu
color={menu.color}
anchor={menu.anchor} anchor={menu.anchor}
theme={theme} label={`Use ${menu.color.toUpperCase()} for`}
onClose={() => setMenu(null)} onClose={closeMenu}
onPick={(role) => { header={
setColor(role, menu.color); <>
setMenu(null); <span className="size-4 rounded border border-border" style={{ background: menu.color }} />
menu.anchor.focus(); <span className="font-mono uppercase">{menu.color}</span>
}} <span>use for…</span>
/> </>
}
>
{COLOR_FIELDS.map(([key, label]) => (
<MenuItem
key={key}
label={label}
leading={<span className="size-4 rounded border border-border" style={{ background: theme[key] }} title={`Current: ${theme[key]}`} />}
checked={theme[key].toLowerCase() === menu.color.toLowerCase()}
onClick={() => {
setColor(key, menu.color);
closeMenu(true);
}}
/>
))}
</PopoverMenu>
)}
{menu?.kind === "variant" && (
<PopoverMenu anchor={menu.anchor} label={`Use ${menu.label} with`} onClose={closeMenu} header={<span>Use {menu.label} with…</span>}>
{(["dark", "light"] as const).map((v) => (
<MenuItem
key={v}
label={v === "dark" ? "Dark background" : "Light background"}
leading={<ThemeThumb theme={themeFromPalette(menu.colors, v)} />}
checked={selectedVariant(menu.colors) === v}
onClick={() => {
applyPalette(menu.colors, v);
closeMenu(true);
}}
/>
))}
</PopoverMenu>
)} )}
</div> </div>
); );