diff --git a/src/web/screens/detail/ThemePicker.tsx b/src/web/screens/detail/ThemePicker.tsx index ea02957..ba86a35 100644 --- a/src/web/screens/detail/ThemePicker.tsx +++ b/src/web/screens/detail/ThemePicker.tsx @@ -1,5 +1,5 @@ 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 { Theme } from "../../../shared/validation.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 paletteLabel = (p: SavedPalette) => `Imported ${new Date(p.createdAt).toLocaleDateString(undefined, { month: "short", day: "numeric" })}`; + const cardClass = (selected: boolean) => cx( "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. -function VariantThumb({ theme, label, selected, onClick }: { theme: Theme; label: string; selected: boolean; onClick: () => void }) { +// Miniature of a link page: background, a line of text and a button. +function ThemeThumb({ theme }: { theme: Theme }) { return ( - + + + ); } -// 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({ colors, label, selected, - onApply, + onOpenVariants, onPickColor, onRemove, }: { colors: string[]; label: string; selected: Variant | null; - onApply: (variant: Variant) => void; + onOpenVariants: (anchor: HTMLElement) => void; onPickColor: (color: string, anchor: HTMLElement) => void; onRemove?: () => void; }) { @@ -113,24 +107,28 @@ function PaletteCard({ /> ))} -
- {label} - {(["dark", "light"] as const).map((v) => ( - onApply(v)} - /> - ))} +
+ {onRemove && ( @@ -140,24 +138,24 @@ function PaletteCard({ ); } -// Small menu next to a clicked color: "use as background / text / buttons / button text". -function ColorRoleMenu({ - color, +// Small menu anchored to an element: placed below it (or above when there's no room), closes on outside +// click, Escape, scroll and resize, and supports arrow keys. +function PopoverMenu({ anchor, - theme, - onPick, + label, + header, onClose, + children, }: { - color: string; anchor: HTMLElement; - theme: Theme; - onPick: (role: keyof Theme) => void; + label: string; + header: ReactNode; onClose: () => void; + children: ReactNode; }) { const ref = useRef(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(() => { const menu = ref.current; if (!menu) return; @@ -176,7 +174,7 @@ function ColorRoleMenu({ useEffect(() => { 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 escape = (e: globalThis.KeyboardEvent) => { @@ -197,49 +195,48 @@ function ColorRoleMenu({ }, [anchor, onClose]); const onKeyDown = (e: KeyboardEvent) => { + if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return; + e.preventDefault(); const items = [...(ref.current?.querySelectorAll("[role=menuitem]") ?? [])]; const index = items.indexOf(document.activeElement as HTMLButtonElement); - if (e.key === "ArrowDown" || e.key === "ArrowUp") { - e.preventDefault(); - const next = (index + (e.key === "ArrowDown" ? 1 : -1) + items.length) % items.length; - items[next]?.focus(); - } + items[(index + (e.key === "ArrowDown" ? 1 : -1) + items.length) % items.length]?.focus(); }; return (
-
- - {color} - use for… -
- {COLOR_FIELDS.map(([key, label]) => { - const current = theme[key].toLowerCase() === color.toLowerCase(); - return ( - - ); - })} +
{header}
+ {children}
); } +function MenuItem({ leading, label, checked, onClick }: { leading: ReactNode; label: string; checked: boolean; onClick: () => void }) { + return ( + + ); +} + +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. export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (theme: Theme) => void }) { const saved = useApi("/palettes"); @@ -250,9 +247,14 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them const [link, setLink] = useState(""); const [linkError, setLinkError] = useState(); const [importing, setImporting] = useState(false); - const [menu, setMenu] = useState<{ color: string; anchor: HTMLElement } | null>(null); + const [menu, setMenu] = useState(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) => 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) => { 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 (
@@ -334,8 +336,8 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them label={p.name} colors={p.colors} selected={selectedVariant(p.colors)} - onApply={(v) => applyPalette(p.colors, v)} - onPickColor={toggleMenu} + onOpenVariants={(anchor) => toggleMenu({ kind: "variant", colors: p.colors, label: p.name, anchor })} + onPickColor={(color, anchor) => toggleMenu({ kind: "color", color, anchor })} /> ))}
@@ -379,11 +381,11 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them {saved.data.map((p) => ( applyPalette(p.colors, v)} - onPickColor={toggleMenu} + onOpenVariants={(anchor) => toggleMenu({ kind: "variant", colors: p.colors, label: paletteLabel(p), anchor })} + onPickColor={(color, anchor) => toggleMenu({ kind: "color", color, anchor })} onRemove={() => void removePalette(p)} /> ))} @@ -403,18 +405,49 @@ export function ThemePicker({ theme, onChange }: { theme: Theme; onChange: (them
- {menu && ( - setMenu(null)} - onPick={(role) => { - setColor(role, menu.color); - setMenu(null); - menu.anchor.focus(); - }} - /> + label={`Use ${menu.color.toUpperCase()} for`} + onClose={closeMenu} + header={ + <> + + {menu.color} + use for… + + } + > + {COLOR_FIELDS.map(([key, label]) => ( + } + checked={theme[key].toLowerCase() === menu.color.toLowerCase()} + onClick={() => { + setColor(key, menu.color); + closeMenu(true); + }} + /> + ))} + + )} + + {menu?.kind === "variant" && ( + Use {menu.label} with…}> + {(["dark", "light"] as const).map((v) => ( + } + checked={selectedVariant(menu.colors) === v} + onClick={() => { + applyPalette(menu.colors, v); + closeMenu(true); + }} + /> + ))} + )} );