{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bouncing-card-fan",
  "title": "Bouncing Card Fan",
  "description": "A squared-up deck of photo cards that springs open into a fan on hover with a bouncy, centre-out stagger; the card under the pointer rises and nudges its neighbours aside, a click pops one up as the pick, and it opens with a tap on touch screens.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/bouncing-card-fan/bouncing-card-fan.tsx",
      "content": "\"use client\";\n/* eslint-disable @next/next/no-img-element -- Registry components work in any React app. */\n\nimport * as React from \"react\";\nimport { motion, 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 FanCard {\n  id: string;\n  image: string;\n  imageAlt?: string;\n  title: string;\n  /** Small mono detail under the title. */\n  meta?: string;\n}\n\nexport type BouncingCardFanTheme = \"dark\" | \"light\";\n\nexport interface BouncingCardFanProps extends Omit<React.ComponentPropsWithoutRef<\"div\">, \"children\" | \"onSelect\"> {\n  cards?: FanCard[];\n  /** Total angle the open fan covers, in degrees. */\n  spread?: number;\n  /** 0 = no overshoot, 1 = very springy. */\n  bounce?: number;\n  cardWidth?: number;\n  /** Card height / width. */\n  cardRatio?: number;\n  /** Keep the fan open instead of opening on hover. */\n  open?: boolean;\n  /** Controlled selected card id. */\n  selectedId?: string | null;\n  defaultSelectedId?: string | null;\n  onSelect?: (card: FanCard | null) => void;\n  radius?: number;\n  theme?: BouncingCardFanTheme;\n  label?: string;\n}\n\nconst CARDS: FanCard[] = [\n  { id: \"rooms\", image: \"/demo/2.webp\", title: \"Rooms\", meta: \"24 frames\" },\n  { id: \"faces\", image: \"/demo/38.webp\", title: \"Faces\", meta: \"31 frames\" },\n  { id: \"motion\", image: \"/demo/108.webp\", title: \"Motion\", meta: \"18 frames\" },\n  { id: \"light\", image: \"/demo/19.webp\", title: \"Light\", meta: \"12 frames\" },\n  { id: \"people\", image: \"/demo/43.webp\", title: \"People\", meta: \"27 frames\" },\n];\n\nexport function BouncingCardFan({\n  cards = CARDS,\n  spread = 46,\n  bounce = 0.5,\n  cardWidth = 190,\n  cardRatio = 1.36,\n  open: openProp,\n  selectedId: selectedProp,\n  defaultSelectedId = null,\n  onSelect,\n  radius = 16,\n  theme = \"dark\",\n  label = \"Collections\",\n  className,\n  style,\n  ...props\n}: BouncingCardFanProps) {\n  const reduce = useReducedMotion();\n  const [hoverOpen, setHoverOpen] = React.useState(false);\n  const [focusOpen, setFocusOpen] = React.useState(false);\n  const [tapOpen, setTapOpen] = React.useState(false);\n  const [hovered, setHovered] = React.useState<number | null>(null);\n  const [internalSelected, setInternalSelected] = React.useState<string | null>(defaultSelectedId);\n  const selectedId = selectedProp !== undefined ? selectedProp : internalSelected;\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const outerRef = React.useRef<HTMLDivElement>(null);\n  // The tap that opens the fan on touch must not also pick the card under the finger.\n  const openingTap = React.useRef(false);\n  const [scale, setScale] = React.useState(1);\n  const open = openProp ?? (hoverOpen || focusOpen || tapOpen);\n  const light = theme === \"light\";\n\n  const n = cards.length;\n  const cardHeight = Math.round(cardWidth * cardRatio);\n  // The open fan is an arc around a pivot well below the cards, so outer cards drop as they turn.\n  const arcRadius = cardHeight * 2.5;\n  const half = (Math.min(spread, 120) * Math.PI) / 360;\n  // Box that holds the open fan: the outer cards turn about their bottom centre, so their top outer corner\n  // reaches sin·R + (w/2)·cos + h·sin sideways; the top leaves room for a lifted or picked card.\n  const reach = arcRadius * Math.sin(half) + (cardWidth / 2) * Math.cos(half) + cardHeight * Math.sin(half);\n  const width = Math.round(2 * reach + 48);\n  // The outer cards also drop below the deck's baseline, so the deck sits that much above the bottom edge.\n  const drop = arcRadius * (1 - Math.cos(half)) + (cardWidth / 2) * Math.sin(half);\n  const baseline = Math.round(drop + 40);\n  const height = Math.round(cardHeight * 1.28 + baseline);\n\n  // Shrink the whole fan to fit narrow containers (phones) instead of overflowing them.\n  React.useEffect(() => {\n    const outer = outerRef.current;\n    if (!outer) return;\n    const observer = new ResizeObserver(() => setScale(Math.min(1, outer.clientWidth / width)));\n    observer.observe(outer);\n    return () => observer.disconnect();\n  }, [width]);\n\n  // Tapping outside closes a fan opened by touch.\n  React.useEffect(() => {\n    if (!tapOpen) return;\n    const close = (event: PointerEvent) => {\n      if (!rootRef.current?.contains(event.target as Node)) setTapOpen(false);\n    };\n    document.addEventListener(\"pointerdown\", close);\n    return () => document.removeEventListener(\"pointerdown\", close);\n  }, [tapOpen]);\n\n  const choose = (card: FanCard) => {\n    const next = selectedId === card.id ? null : card.id;\n    if (selectedProp === undefined) setInternalSelected(next);\n    onSelect?.(next ? card : null);\n  };\n\n  const poseFor = (i: number) => {\n    const t = n === 1 ? 0.5 : i / (n - 1);\n    const centred = t * 2 - 1; // -1 … 1\n    const isSelected = cards[i].id === selectedId;\n    if (!open) {\n      // Closed: a squared-up deck with just enough skew to show it's more than one card.\n      return { x: centred * 7, y: Math.abs(centred) * 3, rotate: centred * 4, scale: isSelected ? 1.02 : 1 };\n    }\n    const angle = centred * half;\n    let x = Math.sin(angle) * arcRadius;\n    let y = (1 - Math.cos(angle)) * arcRadius;\n    let scale = 1;\n    // A hovered card rises; its neighbours lean away from it, less the further they are.\n    if (hovered !== null) {\n      const d = i - hovered;\n      if (d === 0) {\n        y -= cardHeight * 0.14;\n        scale = 1.05;\n      } else x += Math.sign(d) * Math.max(0, 22 - (Math.abs(d) - 1) * 9);\n    }\n    if (isSelected) {\n      y -= cardHeight * 0.2;\n      scale = Math.max(scale, 1.07);\n    }\n    return { x, y, rotate: (angle * 180) / Math.PI, scale };\n  };\n\n  const spring = (i: number) =>\n    reduce\n      ? { duration: 0 }\n      : {\n          type: \"spring\" as const,\n          visualDuration: 0.5,\n          bounce,\n          // Stagger from the centre outward when opening, the other way when closing.\n          delay: (open ? Math.abs(i - (n - 1) / 2) : (n - 1) / 2 - Math.abs(i - (n - 1) / 2)) * 0.035,\n        };\n\n  const selected = cards.find((card) => card.id === selectedId) ?? null;\n\n  return (\n    <div ref={outerRef} className={cn(\"w-full\", className)} style={{ height: height * scale, ...style }}>\n      <div\n        ref={rootRef}\n        role=\"group\"\n        aria-label={label}\n        className=\"relative left-1/2 select-none\"\n        style={{ width, height, transform: `translateX(-50%) scale(${scale})`, transformOrigin: \"50% 0\" }}\n        onPointerLeave={() => {\n          setHoverOpen(false);\n          setHovered(null);\n        }}\n        // Only keyboard focus holds the fan open; a mouse click also focuses the card and must not pin it.\n      onFocus={(event) => (event.target as HTMLElement).matches(\":focus-visible\") && setFocusOpen(true)}\n        onBlur={(event) => {\n          if (!event.currentTarget.contains(event.relatedTarget)) {\n            setFocusOpen(false);\n            setHovered(null);\n          }\n        }}\n        {...props}\n      >\n        {cards.map((card, i) => {\n          const isSelected = card.id === selectedId;\n          const z = isSelected ? 60 : hovered === i ? 50 : 10 + i;\n          return (\n            <motion.button\n              key={card.id}\n              type=\"button\"\n              aria-pressed={isSelected}\n              aria-label={`${card.title}${card.meta ? `, ${card.meta}` : \"\"}`}\n              className=\"absolute left-1/2 cursor-pointer overflow-hidden text-left outline-none focus-visible:ring-2 focus-visible:ring-offset-2\"\n              style={{\n                width: cardWidth,\n                height: cardHeight,\n                bottom: baseline,\n                marginLeft: -cardWidth / 2,\n                borderRadius: radius,\n                zIndex: z,\n                transformOrigin: \"50% 100%\",\n                background: light ? \"#e6e6e3\" : \"#171717\",\n                boxShadow: light ? \"0 16px 34px -16px rgba(0,0,0,.35)\" : \"0 22px 44px -18px rgba(0,0,0,.85)\",\n                [\"--tw-ring-color\" as string]: light ? \"#141414\" : \"#f4f4f2\",\n                [\"--tw-ring-offset-color\" as string]: light ? \"#f1f1ef\" : \"#0a0a0a\",\n              }}\n              initial={false}\n              animate={poseFor(i)}\n              transition={spring(i)}\n              // The fan opens when the pointer reaches a card (not the empty box around the deck) and stays open\n              // while it moves between cards; leaving the whole fan area folds it back.\n              onPointerEnter={(event) => {\n                if (event.pointerType !== \"mouse\") return;\n                setHoverOpen(true);\n                setHovered(i);\n              }}\n              onPointerDown={(event) => {\n                // On touch, the first tap opens the fan; once open, taps pick cards.\n                if (event.pointerType !== \"mouse\" && !open) {\n                  openingTap.current = true;\n                  setTapOpen(true);\n                }\n              }}\n              onClick={() => {\n                if (openingTap.current) {\n                  openingTap.current = false;\n                  return;\n                }\n                choose(card);\n              }}\n              onFocus={(event) => event.currentTarget.matches(\":focus-visible\") && setHovered(i)}\n            >\n              <img src={card.image} alt={card.imageAlt ?? \"\"} draggable={false} className=\"pointer-events-none absolute inset-0 h-full w-full object-cover\" />\n              <span aria-hidden=\"true\" className=\"absolute inset-0 bg-gradient-to-t from-black/75 via-transparent to-black/15\" />\n              <span className=\"absolute top-3 left-3 font-mono text-[10px] text-white/85\">{String(i + 1).padStart(2, \"0\")}</span>\n              <span\n                aria-hidden=\"true\"\n                className=\"absolute top-3 right-3 flex size-5 items-center justify-center rounded-full border border-white/60 transition-colors\"\n                style={{ background: isSelected ? \"#ffffff\" : \"transparent\" }}\n              >\n                {isSelected && <span className=\"size-1.5 rounded-full bg-black\" />}\n              </span>\n              <span className=\"absolute inset-x-0 bottom-0 p-3.5 text-white\">\n                <span className=\"block text-[16px] leading-tight font-medium\">{card.title}</span>\n                {card.meta && <span className=\"mt-0.5 block font-mono text-[10px] text-white/65\">{card.meta}</span>}\n              </span>\n            </motion.button>\n          );\n        })}\n\n        <p aria-live=\"polite\" className=\"absolute inset-x-0 text-center font-mono text-[10.5px]\" style={{ bottom: baseline - 30, color: light ? \"#6b6b6b\" : \"#8f8f8f\" }}>\n          {selected ? (\n            <>\n              Selected · <span style={{ color: light ? \"#141414\" : \"#f4f4f2\" }}>{selected.title}</span>\n            </>\n          ) : open ? (\n            \"Pick a collection\"\n          ) : (\n            \"Hover to fan out\"\n          )}\n        </p>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}