{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"swatch-fan-deck","type":"registry:component","title":"Swatch Fan Deck","description":"A fan deck of paint chips that spreads in your hand on hover or focus — a real radio group, with arrow keys and the colour's name, not its hex.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/swatch-fan-deck.tsx","content":"\"use client\";\n\nimport { useRef, useState } from \"react\";\n\n/**\n * SwatchFanDeck — a fan deck of paint chips that spreads in your hand.\n *\n * At rest the chips sit in a loose stack. Hover, touch or tab into the deck and\n * they fan out around a pivot below the deck, like a hand of cards; pick one and\n * it lifts clear of the others. It is a colour picker for the trades that sell\n * colour — decorators, nail bars, tattoo studios, apparel — where a row of\n * circles says nothing and a physical chip says \"we have this in stock\".\n *\n * IT IS A RADIO GROUP. Every chip is a real button with role=\"radio\", so the\n * arrow keys move the selection, Home and End jump to the ends, and only the\n * selected chip is in the tab order (roving tabindex). Screen readers hear the\n * colour's name and code, never a hex value.\n *\n * THE FAN IS GEOMETRY, NOT A GUESS. The deck's box is sized from the spread\n * angle and the pivot radius, so the outer chips never clip, whatever the\n * chip size. Only transforms animate (620ms, slow enough to read as a hand\n * moving), and nothing animates under prefers-reduced-motion.\n */\n\nexport type Swatch = {\n  name: string;\n  /** Any CSS colour: this is what the chip is painted with. */\n  colour: string;\n  /** Printed under the name — a paint code, a shade number. */\n  code?: string;\n};\n\nconst EASE = \"cubic-bezier(0.22, 1, 0.36, 1)\";\n\nexport function SwatchFanDeck({\n  swatches,\n  value,\n  defaultValue,\n  onChange,\n  spread = 14,\n  chipWidth = 64,\n  chipHeight = 150,\n  label = \"Colour\",\n  paper = \"#f7f5f0\",\n  ink = \"#0e0e0c\",\n  className = \"\",\n}: {\n  swatches: Swatch[];\n  /** Controlled: the selected swatch's colour. */\n  value?: string;\n  defaultValue?: string;\n  onChange?: (swatch: Swatch) => void;\n  /** Degrees between neighbouring chips when fanned. */\n  spread?: number;\n  chipWidth?: number;\n  chipHeight?: number;\n  /** Accessible name for the group. */\n  label?: string;\n  /** The chip's card stock. */\n  paper?: string;\n  ink?: string;\n  className?: string;\n}) {\n  const [inner, setInner] = useState(defaultValue ?? swatches[0]?.colour ?? \"\");\n  const [open, setOpen] = useState(false);\n  const selected = value ?? inner;\n  const buttons = useRef<(HTMLButtonElement | null)[]>([]);\n\n  const n = swatches.length;\n  const mid = (n - 1) / 2;\n  const pivot = chipHeight + 56; // rotation radius, from the chip's top edge\n  const maxAngle = (Math.abs(mid * spread) * Math.PI) / 180;\n  const boxW = Math.ceil(2 * pivot * Math.sin(maxAngle) + chipWidth + 12);\n  const boxH = Math.ceil(chipHeight + pivot * (1 - Math.cos(maxAngle)) + 8);\n\n  const current = swatches.find((s) => s.colour === selected) ?? swatches[0];\n\n  function choose(i: number) {\n    const s = swatches[i];\n    if (!s) return;\n    setInner(s.colour);\n    onChange?.(s);\n    buttons.current[i]?.focus();\n  }\n\n  function onKeyDown(e: React.KeyboardEvent) {\n    const i = Math.max(\n      0,\n      swatches.findIndex((s) => s.colour === selected),\n    );\n    let j = i;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowDown\") j = (i + 1) % n;\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowUp\") j = (i - 1 + n) % n;\n    else if (e.key === \"Home\") j = 0;\n    else if (e.key === \"End\") j = n - 1;\n    else return;\n    e.preventDefault();\n    choose(j);\n  }\n\n  return (\n    <div\n      className={\"inline-flex flex-col items-center \" + className}\n      onPointerEnter={() => setOpen(true)}\n      onPointerLeave={() => setOpen(false)}\n      onFocus={() => setOpen(true)}\n      onBlur={(e) => {\n        if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setOpen(false);\n      }}\n    >\n      <div\n        role=\"radiogroup\"\n        aria-label={label}\n        data-fan-deck=\"\"\n        onKeyDown={onKeyDown}\n        className=\"relative\"\n        style={{ width: boxW, height: boxH }}\n      >\n        {swatches.map((s, i) => {\n          const isSelected = s.colour === selected;\n          // Closed: a 1.6° fan so the stack reads as a deck rather than one chip.\n          const angle = (i - mid) * (open ? spread : 1.6);\n          const lift = open && isSelected ? -16 : 0;\n          return (\n            <button\n              key={s.colour + \"-\" + i}\n              ref={(el) => {\n                buttons.current[i] = el;\n              }}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={isSelected}\n              aria-label={s.code ? s.name + \", \" + s.code : s.name}\n              tabIndex={isSelected ? 0 : -1}\n              onClick={() => choose(i)}\n              className=\"absolute top-0 left-1/2 flex flex-col overflow-hidden rounded-[3px] text-left outline-none focus-visible:ring-2 focus-visible:ring-offset-2\"\n              style={{\n                width: chipWidth,\n                height: chipHeight,\n                marginLeft: -chipWidth / 2,\n                background: paper,\n                color: ink,\n                boxShadow: isSelected && open\n                  ? \"0 14px 28px -12px rgba(0,0,0,0.55), 0 0 0 1px rgba(0,0,0,0.08)\"\n                  : \"0 2px 4px -2px rgba(0,0,0,0.35), 0 0 0 1px rgba(0,0,0,0.08)\",\n                transform: \"rotate(\" + angle + \"deg) translateY(\" + lift + \"px)\",\n                transformOrigin: \"50% \" + pivot + \"px\",\n                transition:\n                  \"transform 620ms \" + EASE + \", box-shadow 620ms \" + EASE,\n                zIndex: isSelected ? n + 1 : i,\n              }}\n            >\n              <span aria-hidden=\"true\" className=\"relative m-1 block flex-1 rounded-[2px]\" style={{ background: s.colour }}>\n                {/* The punch hole for the ring. */}\n                <span\n                  className=\"absolute top-1.5 left-1/2 size-2 -translate-x-1/2 rounded-full\"\n                  style={{ background: \"rgba(0,0,0,0.5)\", boxShadow: \"inset 0 1px 1px rgba(0,0,0,0.6)\" }}\n                />\n              </span>\n              <span aria-hidden=\"true\" className=\"block px-1.5 pt-0.5 pb-1.5 font-mono text-[9px] leading-[1.2] tracking-[0.06em] uppercase\">\n                <span className=\"block truncate\">{s.name}</span>\n                {s.code && <span className=\"block opacity-60\">{s.code}</span>}\n              </span>\n            </button>\n          );\n        })}\n        {/* Static text only; no prop reaches this style element. */}\n        <style>{\"@media (prefers-reduced-motion: reduce){[data-fan-deck] > button{transition:none!important}}\"}</style>\n      </div>\n\n      {current && (\n        <p className=\"mt-3 flex items-center gap-2 font-mono text-[11px] tracking-[0.04em]\" aria-live=\"polite\">\n          <span aria-hidden=\"true\" className=\"inline-block size-2.5 rounded-full\" style={{ background: current.colour }} />\n          <span>{current.name}</span>\n          {current.code && <span className=\"opacity-60\">{current.code}</span>}\n        </p>\n      )}\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/swatch-fan-deck.tsx"}],"meta":{"tier":"free","tags":["picker","colour","radio","keyboard","local-business"],"href":"https://viberdy.dev/library/swatch-fan-deck","aiPrompt":"Create a React + Tailwind component named SwatchFanDeck: a fan deck of paint chips that spreads out like a hand of cards on hover, touch or focus, and works as an accessible colour picker.\n\nData: an array of swatches, each { name, colour (any CSS colour), code? }. Props: swatches, value (controlled colour), defaultValue, onChange(swatch), spread (degrees between neighbours, default 14), chipWidth (64), chipHeight (150), label (group name), paper (card stock colour, default #f7f5f0), ink (#0e0e0c), className.\n\nLook:\n- Each chip is a button on card stock: a painted area filling the chip with 4px of paper around it, a small dark punch hole near the top (a 8px circle, rgba(0,0,0,0.5) with an inset shadow), and beneath the paint the name and code in 9px uppercase monospace, tracking 0.06em, the code at 60% opacity. Rounded 3px, a hairline ring via box-shadow.\n- All chips are absolutely positioned at top 0, horizontally centred (left 50% with a negative margin of half the width), and rotated about a pivot 56px BELOW the chip's bottom edge (transform-origin \"50% <chipHeight + 56>px\"). Chip i is rotated (i − mid) × spread degrees when open, and (i − mid) × 1.6 degrees when closed so the stack still reads as a deck. The selected chip, while open, also translates up 16px, gets a deeper shadow and the highest z-index.\n- Size the container from the geometry so nothing clips: width = 2 × pivot × sin(maxAngle) + chipWidth + 12, height = chipHeight + pivot × (1 − cos(maxAngle)) + 8, where pivot = chipHeight + 56 and maxAngle = |mid × spread| in radians.\n- Only transform and box-shadow animate, 620ms cubic-bezier(0.22, 1, 0.36, 1). Under prefers-reduced-motion, no transition (a style rule in the component).\n- Under the deck, a line in 11px monospace with a colour dot, the selected name and code, marked aria-live=\"polite\".\n\nBehaviour:\n- Open on pointerenter, focus (any chip), close on pointerleave and on blur when focus leaves the whole component (check relatedTarget against currentTarget).\n- The deck is role=\"radiogroup\" with aria-label; each chip is role=\"radio\" with aria-checked and an aria-label of \"name, code\" (never the hex). Roving tabindex: only the selected chip has tabIndex 0. Arrow Right/Down move to the next chip, Left/Up to the previous (wrapping), Home and End to the ends; choosing focuses that chip's button and calls onChange.\n- Support controlled (value) and uncontrolled (defaultValue) use.\n- Keep button refs in an array ref written from ref callbacks; do not read refs during render.\n\nOutput one self-contained \"use client\" component with no dependencies beyond React, exporting the Swatch type.","license":"https://viberdy.dev/license"}}