{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"swatch-picker","type":"registry:ui","title":"Swatch Picker","description":"Color swatches where the selection ring glides from one to the next and a check pops in on the chosen color, picking a dark or light check to suit it. Arrow keys move the choice.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/swatch-picker.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { Check } from \"lucide-react\";\nimport { MotionConfig, motion } from \"motion/react\";\nimport { useId, useRef, useState, type KeyboardEvent } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface Swatch {\n  name: string;\n  value: string;\n}\n\nexport interface SwatchPickerProps {\n  colors?: Swatch[];\n  /** Controlled selection (a swatch value). */\n  value?: string;\n  defaultValue?: string;\n  onChange?: (value: string) => void;\n  /** Group label, read by screen readers and shown above. */\n  label?: string;\n  /** Swatch diameter in px. */\n  size?: number;\n  className?: string;\n}\n\nconst DEFAULT_COLORS: Swatch[] = [\n  { name: \"Lime\", value: \"#b5e61d\" },\n  { name: \"Sky\", value: \"#38bdf8\" },\n  { name: \"Violet\", value: \"#8b5cf6\" },\n  { name: \"Rose\", value: \"#f43f5e\" },\n  { name: \"Amber\", value: \"#f59e0b\" },\n  { name: \"Slate\", value: \"#64748b\" },\n];\n\n// Dark or light check depending on how bright the swatch is.\nfunction inkFor(hex: string) {\n  const m = hex.replace(\"#\", \"\").match(/.{2}/g);\n  if (!m || m.length < 3) return \"#fff\";\n  const [r, g, b] = m.map((x) => parseInt(x, 16));\n  return 0.299 * r + 0.587 * g + 0.114 * b > 160 ? \"#111\" : \"#fff\";\n}\n\n/**\n * A row of color swatches where the selection ring glides from one swatch to\n * the next and a check pops in on the chosen color. Arrow keys move the choice.\n */\nexport function SwatchPicker({\n  colors = DEFAULT_COLORS,\n  value,\n  defaultValue,\n  onChange,\n  label = \"Color\",\n  size = 32,\n  className,\n}: SwatchPickerProps) {\n  const ringId = useId();\n  const [inner, setInner] = useState(defaultValue ?? colors[0]?.value);\n  const current = value ?? inner;\n  const group = useRef<HTMLDivElement>(null);\n  const picked = colors.find((c) => c.value === current);\n\n  const choose = (v: string) => {\n    if (value === undefined) setInner(v);\n    onChange?.(v);\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    const i = colors.findIndex((c) => c.value === current);\n    const step = e.key === \"ArrowRight\" || e.key === \"ArrowDown\" ? 1 : e.key === \"ArrowLeft\" || e.key === \"ArrowUp\" ? -1 : 0;\n    if (!step) return;\n    e.preventDefault();\n    const next = (i + step + colors.length) % colors.length;\n    choose(colors[next].value);\n    group.current?.querySelectorAll<HTMLElement>(\"[role=radio]\")[next]?.focus();\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div className={cn(\"grid gap-2.5\", className)}>\n        <div className=\"flex items-baseline justify-between text-sm\">\n          <span className=\"font-medium text-foreground\">{label}</span>\n          <span className=\"text-xs text-muted-foreground\">{picked?.name}</span>\n        </div>\n        <div ref={group} role=\"radiogroup\" aria-label={label} onKeyDown={onKey} className=\"flex flex-wrap gap-2\">\n          {colors.map((c) => {\n            const on = c.value === current;\n            return (\n              <button\n                key={c.value}\n                type=\"button\"\n                role=\"radio\"\n                aria-checked={on}\n                aria-label={c.name}\n                tabIndex={on ? 0 : -1}\n                onClick={() => choose(c.value)}\n                className=\"group relative grid place-items-center rounded-full outline-none\"\n                style={{ width: size + 8, height: size + 8 }}\n              >\n                {on && (\n                  <motion.span\n                    layoutId={ringId}\n                    className=\"absolute inset-0 rounded-full border-2 group-focus-visible:ring-2 group-focus-visible:ring-ring group-focus-visible:ring-offset-2 group-focus-visible:ring-offset-background\"\n                    style={{ borderColor: c.value }}\n                    transition={{ type: \"spring\", bounce: 0.3, duration: 0.45 }}\n                  />\n                )}\n                <motion.span\n                  className=\"grid place-items-center rounded-full shadow-sm transition-transform group-hover:scale-110 group-active:scale-90\"\n                  style={{ width: size, height: size, background: c.value, color: inkFor(c.value) }}\n                  animate={{ scale: on ? 0.86 : 1 }}\n                  transition={{ type: \"spring\", bounce: 0.4, duration: 0.35 }}\n                >\n                  <motion.span initial={false} animate={{ scale: on ? 1 : 0, rotate: on ? 0 : -60 }} transition={{ type: \"spring\", bounce: 0.5, duration: 0.35 }}>\n                    <Check className=\"size-4\" strokeWidth={3} />\n                  </motion.span>\n                </motion.span>\n              </button>\n            );\n          })}\n        </div>\n      </div>\n    </MotionConfig>\n  );\n}\n"}]}