import { useEffect, useState } from 'react'; import { Pipette } from 'lucide-react'; import { Input } from '~/components/ui/input'; import { normalizeHex } from '~/lib/color'; import { cn } from '~/lib/utils'; import type { ColorPreset } from '~/types/project'; const swatch = 'relative h-[26px] w-[26px] shrink-0 cursor-pointer rounded-full transition-shadow'; const ring = (selected: boolean) => (selected ? 'ring-2 ring-text-primary ring-offset-2 ring-offset-surface-dialog' : 'ring-1 ring-inset ring-text-primary/15'); interface ColorPickerProps { id: string; label: string; value: string; presets: ColorPreset[]; onChange: (hex: string) => void; } // Preset swatches, a custom swatch that opens the system colour picker, and a hex field export function ColorPicker({ id, label, value, presets, onChange }: ColorPickerProps) { const [draft, setDraft] = useState(value); const isCustom = !presets.some((preset) => preset.value === value); // Keep the hex field in step when a swatch or the system picker changes the colour useEffect(() => setDraft(value), [value]); return (
{presets.map((preset) => (
{ setDraft(e.target.value); const hex = normalizeHex(e.target.value); if (hex) onChange(hex); }} onBlur={() => setDraft(value)} />
); }