{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "swatch",
  "title": "Swatch and hex field",
  "description": "a colour: the browser’s picker beside the hex as text · six digits apply as you type",
  "dependencies": [
    "cn@^0.5.2"
  ],
  "registryDependencies": [
    "@halftone-web/kit-setup"
  ],
  "files": [
    {
      "path": "components/controls/swatch.tsx",
      "content": "'use client';\nimport { cn } from 'cn';\nimport * as React from 'react';\n\n/** `#abc`, `abc`, `#aabbcc` → `#aabbcc`; null for anything else. */\nexport function readHex(input: string): string | null {\n  const m = /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(input.trim());\n  if (!m) return null;\n  const h = m[1].toLowerCase();\n  return `#${h.length === 3 ? [...h].map((c) => c + c).join('') : h}`;\n}\n\n/** A colour square with a hairline, so white and black both show on any surface. */\nexport function Swatch({ hex, className }: { hex: string | null | undefined; className?: string }) {\n  return <span aria-hidden className={cn('ring-foreground/15 inline-block size-5 shrink-0 rounded ring-1 ring-inset', className)} style={{ background: hex ?? 'transparent' }} />;\n}\n\n/**\n * A hex colour: the browser's colour picker beside the hex as text. Six typed digits apply as you type; a short `#abc`\n * waits for Enter or the blur, so typing `#e11d48` doesn't flash `#ee1111` on the way. A drag of the picker applies\n * live (coalesce it into one undo step in the caller).\n */\nexport function HexField({ id, value, onChange, label, className }: { id?: string; value: string; onChange: (hex: string) => void; label: string; className?: string }) {\n  const [draft, setDraft] = React.useState<string | null>(null);\n  const shown = draft ?? value.toUpperCase();\n  const invalid = draft !== null && readHex(draft) === null;\n  const commit = () => {\n    const hex = draft === null ? null : readHex(draft);\n    if (hex && hex !== value) onChange(hex);\n    setDraft(null);\n  };\n  return (\n    <div className={cn('flex min-w-0 flex-1 items-center gap-1.5', className)}>\n      <input\n        type=\"color\"\n        value={readHex(value) ?? '#000000'}\n        aria-label={`${label}, colour picker`}\n        onChange={(e) => onChange(e.target.value.toLowerCase())}\n        className=\"border-border focus-visible:ring-ring/50 size-8 shrink-0 cursor-pointer rounded-md border bg-transparent p-0.5 outline-none focus-visible:ring-[3px] [&::-webkit-color-swatch]:rounded [&::-webkit-color-swatch]:border-0 [&::-webkit-color-swatch-wrapper]:p-0\"\n      />\n      <input\n        id={id}\n        value={shown}\n        spellCheck={false}\n        autoComplete=\"off\"\n        aria-label={id ? undefined : label}\n        aria-invalid={invalid || undefined}\n        onChange={(e) => {\n          setDraft(e.target.value);\n          const hex = /^#?[0-9a-f]{6}$/i.test(e.target.value.trim()) ? readHex(e.target.value) : null;\n          if (hex && hex !== value) onChange(hex);\n        }}\n        onKeyDown={(e) => {\n          if (e.key === 'Enter') commit();\n          if (e.key === 'Escape' && draft !== null) {\n            e.preventDefault();\n            setDraft(null);\n          }\n        }}\n        onBlur={commit}\n        className=\"border-border bg-background focus-visible:ring-ring/50 aria-invalid:border-destructive h-8 w-full min-w-0 rounded-md border px-2 font-mono text-sm uppercase outline-none focus-visible:ring-[3px]\"\n      />\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/controls/swatch.tsx"
    }
  ],
  "meta": {
    "exports": [
      "HexField",
      "Swatch",
      "readHex"
    ],
    "api": "<HexField label value onChange id? /> · <Swatch hex />"
  },
  "categories": [
    "controls"
  ],
  "type": "registry:component"
}