{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "variant-swatches",
  "title": "Variant Swatches",
  "description": "Product option pickers for colour and size: round colour swatches with a ring that glides to the pick, size chips with a sliding fill, the chosen value named in the label, sold-out options struck through, and a low-stock warning when only a few are left.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/variant-swatches/variant-swatches.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface SwatchOption {\n  value: string;\n  label: string;\n  /** CSS colour for colour swatches. */\n  color?: string;\n  soldOut?: boolean;\n  /** Units left; low numbers show a hint. */\n  stock?: number;\n}\n\nexport interface SwatchGroup {\n  name: string;\n  type: \"color\" | \"size\";\n  options: SwatchOption[];\n}\n\nexport type VariantSwatchesSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface VariantSwatchesProps {\n  groups?: SwatchGroup[];\n  /** Selected value per group name. */\n  value?: Record<string, string>;\n  defaultValue?: Record<string, string>;\n  onValueChange?: (value: Record<string, string>) => void;\n  /** Show \"Only n left\" when stock is at or below this. */\n  lowStock?: number;\n  size?: VariantSwatchesSize;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.5)\", faint: \"rgba(245,244,241,0.25)\", bg: \"rgba(255,255,255,0.03)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.28)\", ring: \"#F5F4F1\", chipOn: \"#F5F4F1\", chipOnText: \"#0A0A0A\", warn: \"#F2A841\", tip: \"#F5F4F1\", tipText: \"#0A0A0A\", focus: \"rgba(245,244,241,0.85)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.5)\", faint: \"rgba(10,10,10,0.25)\", bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.14)\", borderHover: \"rgba(10,10,10,0.3)\", ring: \"#0A0A0A\", chipOn: \"#0A0A0A\", chipOnText: \"#FFFFFF\", warn: \"#B7791F\", tip: \"#0A0A0A\", tipText: \"#FFFFFF\", focus: \"rgba(10,10,10,0.85)\" },\n};\n\nconst SIZES: Record<VariantSwatchesSize, { dot: number; chip: number; font: number; small: number }> = {\n  sm: { dot: 26, chip: 34, font: 13, small: 12 },\n  md: { dot: 32, chip: 40, font: 14, small: 12.5 },\n  lg: { dot: 38, chip: 46, font: 15.5, small: 13.5 },\n};\n\nconst DEFAULT_GROUPS: SwatchGroup[] = [\n  {\n    name: \"Color\",\n    type: \"color\",\n    options: [\n      { value: \"black\", label: \"Black\", color: \"#141414\" },\n      { value: \"sand\", label: \"Sand\", color: \"#D8C7A8\" },\n      { value: \"sage\", label: \"Sage\", color: \"#9BAE93\", stock: 3 },\n      { value: \"clay\", label: \"Clay\", color: \"#B86B4B\", soldOut: true },\n      { value: \"navy\", label: \"Navy\", color: \"#27344F\" },\n    ],\n  },\n  {\n    name: \"Size\",\n    type: \"size\",\n    options: [\n      { value: \"xs\", label: \"XS\" },\n      { value: \"s\", label: \"S\" },\n      { value: \"m\", label: \"M\", stock: 2 },\n      { value: \"l\", label: \"L\" },\n      { value: \"xl\", label: \"XL\", soldOut: true },\n    ],\n  },\n];\n\nexport function VariantSwatches({ groups = DEFAULT_GROUPS, value, defaultValue = { Color: \"black\", Size: \"s\" }, onValueChange, lowStock = 3, size = \"md\", theme = \"dark\", className }: VariantSwatchesProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const [internal, setInternal] = React.useState(defaultValue);\n  const sel = value ?? internal;\n  const [tip, setTip] = React.useState<string | null>(null);\n\n  const pick = (group: string, v: string) => {\n    const next = { ...sel, [group]: v };\n    if (value === undefined) setInternal(next);\n    onValueChange?.(next);\n  };\n\n  const onKey = (e: React.KeyboardEvent, g: SwatchGroup, i: number) => {\n    const dir = e.key === \"ArrowRight\" || e.key === \"ArrowDown\" ? 1 : e.key === \"ArrowLeft\" || e.key === \"ArrowUp\" ? -1 : 0;\n    if (!dir) return;\n    e.preventDefault();\n    const n = g.options.length;\n    for (let k = 1; k <= n; k++) {\n      const j = (i + dir * k + n) % n;\n      if (!g.options[j].soldOut) {\n        pick(g.name, g.options[j].value);\n        (e.currentTarget.parentElement?.children[j] as HTMLElement | undefined)?.focus();\n        return;\n      }\n    }\n  };\n\n  const spring = reduce ? { duration: 0 } : { type: \"spring\" as const, stiffness: 500, damping: 34 };\n\n  return (\n    <div className={cn(\"flex flex-col\", className)} style={{ gap: 22, fontFamily: \"Inter, sans-serif\", color: p.text }}>\n      {groups.map((g) => {\n        const current = g.options.find((o) => o.value === sel[g.name]);\n        const low = current?.stock !== undefined && current.stock <= lowStock && !current.soldOut;\n        return (\n          <div key={g.name} className=\"flex flex-col\" style={{ gap: 10 }}>\n            <div className=\"flex items-baseline justify-between\" style={{ gap: 12 }}>\n              <span className=\"font-medium\" style={{ fontSize: s.font }}>\n                {g.name}\n                <span style={{ color: p.muted }}> · </span>\n                <AnimatePresence mode=\"popLayout\" initial={false}>\n                  <motion.span key={current?.label ?? \"none\"} className=\"inline-block\" style={{ color: p.muted }} initial={{ opacity: 0, y: 6 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -6 }} transition={{ duration: reduce ? 0 : 0.18 }}>\n                    {current?.label ?? \"Choose\"}\n                  </motion.span>\n                </AnimatePresence>\n              </span>\n              <AnimatePresence initial={false}>\n                {low && (\n                  <motion.span key=\"low\" className=\"flex items-center font-medium\" style={{ gap: 6, fontSize: s.small, color: p.warn }} initial={{ opacity: 0, x: 6 }} animate={{ opacity: 1, x: 0 }} exit={{ opacity: 0, x: 6 }}>\n                    <span className=\"block rounded-full\" style={{ width: 6, height: 6, background: p.warn }} />\n                    Only {current?.stock} left\n                  </motion.span>\n                )}\n              </AnimatePresence>\n            </div>\n            <div role=\"radiogroup\" aria-label={g.name} className=\"flex flex-wrap items-center\" style={{ gap: g.type === \"color\" ? 10 : 8 }}>\n              {g.options.map((o, i) => {\n                const on = sel[g.name] === o.value;\n                const focusable = on || (!g.options.some((x) => x.value === sel[g.name]) && i === 0);\n                const tipKey = `${g.name}-${o.value}`;\n                if (g.type === \"color\") {\n                  return (\n                    <button\n                      key={o.value}\n                      type=\"button\"\n                      role=\"radio\"\n                      aria-checked={on}\n                      aria-label={`${o.label}${o.soldOut ? \", sold out\" : \"\"}`}\n                      aria-disabled={o.soldOut || undefined}\n                      tabIndex={focusable ? 0 : -1}\n                      onClick={() => !o.soldOut && pick(g.name, o.value)}\n                      onKeyDown={(e) => onKey(e, g, i)}\n                      onMouseEnter={() => setTip(tipKey)}\n                      onMouseLeave={() => setTip(null)}\n                      className=\"relative flex cursor-pointer items-center justify-center rounded-full border-none bg-transparent p-0 outline-none focus-visible:ring-2 focus-visible:ring-offset-2\"\n                      style={{ width: s.dot + 10, height: s.dot + 10, cursor: o.soldOut ? \"not-allowed\" : \"pointer\", [\"--tw-ring-color\" as string]: p.focus, [\"--tw-ring-offset-color\" as string]: \"transparent\" }}\n                    >\n                      {on && <motion.span layoutId={`${uid}-${g.name}-ring`} className=\"absolute inset-0 rounded-full\" style={{ border: `2px solid ${p.ring}` }} transition={spring} />}\n                      <motion.span className=\"relative block overflow-hidden rounded-full\" style={{ width: s.dot, height: s.dot, background: o.color, boxShadow: `inset 0 0 0 1px ${p.border}`, opacity: o.soldOut ? 0.45 : 1 }} whileHover={o.soldOut || reduce ? undefined : { scale: 1.08 }} transition={{ type: \"spring\", stiffness: 500, damping: 22 }}>\n                        {o.soldOut && <span className=\"absolute top-1/2 left-[-10%] block h-[1.5px] w-[120%] -rotate-45\" style={{ background: p.text }} />}\n                      </motion.span>\n                      <AnimatePresence>\n                        {tip === tipKey && (\n                          <motion.span key=\"tip\" role=\"tooltip\" className=\"pointer-events-none absolute bottom-full left-1/2 mb-1.5 rounded-md text-[11.5px] font-semibold whitespace-nowrap\" style={{ padding: \"3px 8px\", background: p.tip, color: p.tipText, x: \"-50%\" }} initial={{ opacity: 0, y: 4 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: 4 }} transition={{ duration: 0.12 }}>\n                            {o.label}\n                            {o.soldOut ? \" · sold out\" : \"\"}\n                          </motion.span>\n                        )}\n                      </AnimatePresence>\n                    </button>\n                  );\n                }\n                return (\n                  <button\n                    key={o.value}\n                    type=\"button\"\n                    role=\"radio\"\n                    aria-checked={on}\n                    aria-label={`${o.label}${o.soldOut ? \", sold out\" : \"\"}`}\n                    aria-disabled={o.soldOut || undefined}\n                    tabIndex={focusable ? 0 : -1}\n                    onClick={() => !o.soldOut && pick(g.name, o.value)}\n                    onKeyDown={(e) => onKey(e, g, i)}\n                    className=\"relative flex cursor-pointer items-center justify-center overflow-hidden rounded-xl font-semibold outline-none focus-visible:ring-2\"\n                    style={{ minWidth: s.chip + 8, height: s.chip, padding: \"0 12px\", fontSize: s.font, background: p.bg, border: `1px solid ${on ? \"transparent\" : p.border}`, color: on ? p.chipOnText : o.soldOut ? p.faint : p.text, cursor: o.soldOut ? \"not-allowed\" : \"pointer\", transition: \"color 0.2s ease, border-color 0.2s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n                  >\n                    {on && <motion.span layoutId={`${uid}-${g.name}-chip`} className=\"absolute inset-0 rounded-xl\" style={{ background: p.chipOn }} transition={spring} />}\n                    <span className=\"relative\" style={{ textDecoration: o.soldOut ? \"line-through\" : undefined }}>\n                      {o.label}\n                    </span>\n                    {o.stock !== undefined && o.stock <= lowStock && !o.soldOut && <span className=\"absolute top-1.5 right-1.5 block rounded-full\" style={{ width: 5, height: 5, background: p.warn }} aria-hidden=\"true\" />}\n                  </button>\n                );\n              })}\n            </div>\n          </div>\n        );\n      })}\n    </div>\n  );\n}\n\nexport default VariantSwatches;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}