{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "card-stack-drag",
  "type": "registry:component",
  "title": "Stack Drag",
  "description": "A deck of cards where you drag or flick the top card away with momentum and it drops to the back. Arrow keys cycle too.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "components",
    "cards",
    "stack",
    "drag",
    "flick",
    "swipe",
    "deck"
  ],
  "docs": "https://hyperblur.dev/effects/card-stack-drag",
  "files": [
    {
      "path": "registry/hyperblur/card-stack-drag.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/card-stack-drag.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface StackCard {\n  title: string;\n  meta?: string;\n  text?: string;\n  color?: string;\n}\n\nexport interface CardStackDragProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n  cards?: StackCard[];\n  value?: number;\n  defaultValue?: number;\n  onChange?: (index: number) => void;\n  ratio?: number;\n  idle?: boolean;\n}\n\nconst css = `\n.hb-qka {\n  position: relative;\n  display: block;\n  width: 100%;\n  aspect-ratio: var(--hb-qka-r);\n  font-family: inherit;\n  box-sizing: border-box;\n  outline: none;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-qka *, .hb-qka *::before, .hb-qka *::after { box-sizing: border-box; }\n.hb-qka:focus-visible { outline: 2px solid #2f7bff; outline-offset: 4px; border-radius: 16px; }\n.hb-qka-c {\n  position: absolute;\n  left: 17%;\n  right: 17%;\n  top: 0;\n  bottom: 34px;\n  display: flex;\n  flex-direction: column;\n  justify-content: space-between;\n  padding: 14px 15px 14px;\n  overflow: hidden;\n  border-radius: 18px;\n  border: 1px solid rgba(255, 255, 255, 0.14);\n  background:\n    radial-gradient(120% 70% at 15% 0%, color-mix(in srgb, var(--hb-qka-c) 65%, #ffffff) 0%, transparent 62%),\n    linear-gradient(165deg, var(--hb-qka-c), color-mix(in srgb, var(--hb-qka-c) 26%, #050508));\n  color: #ffffff;\n  box-shadow: 0 16px 28px -14px rgba(0, 0, 0, 0.9);\n  z-index: var(--hb-qka-z);\n  transform-origin: 50% 92%;\n  transform: translateY(calc(var(--hb-qka-d) * 9px)) scale(calc(1 - var(--hb-qka-d) * 0.055)) rotate(var(--hb-qka-r0));\n  filter: brightness(calc(1 - var(--hb-qka-d) * 0.14));\n  opacity: 1;\n  cursor: default;\n  user-select: none;\n  touch-action: pan-y;\n  transition: transform 0.5s cubic-bezier(0.3, 1.4, 0.5, 1), filter 0.4s ease, opacity 0.3s ease;\n}\n.hb-qka-c[data-top=\"true\"] { cursor: grab; }\n.hb-qka-c[data-drag=\"true\"] { cursor: grabbing; }\n.hb-qka-c[data-hide=\"true\"] { opacity: 0; pointer-events: none; }\n.hb-qka-c[data-fly=\"true\"] { z-index: 99; transition: none; filter: none; pointer-events: none; }\n.hb-qka-c[data-enter=\"true\"] { animation: hb-qka-in 0.55s cubic-bezier(0.22, 1.2, 0.4, 1) backwards; }\n@keyframes hb-qka-in {\n  from { transform: translateX(-120%) rotate(-14deg); opacity: 0; }\n  60% { opacity: 1; }\n}\n.hb-qka-meta {\n  align-self: flex-start;\n  padding: 3px 8px;\n  border-radius: 999px;\n  background: rgba(8, 8, 12, 0.4);\n  font-size: 10px;\n  font-weight: 500;\n  line-height: 1;\n  letter-spacing: 0.1em;\n  text-transform: uppercase;\n  color: #f4f4f4;\n}\n.hb-qka-title {\n  margin: 0;\n  font-size: 22px;\n  font-weight: 600;\n  line-height: 1.08;\n  letter-spacing: -0.01em;\n  color: #ffffff;\n  text-shadow: 0 1px 10px rgba(0, 0, 0, 0.3);\n}\n.hb-qka-text {\n  margin: 6px 0 0;\n  font-size: 12px;\n  line-height: 1.35;\n  color: rgba(255, 255, 255, 0.82);\n}\n.hb-qka-dots {\n  position: absolute;\n  left: 0;\n  right: 0;\n  bottom: 4px;\n  z-index: 120;\n  display: flex;\n  justify-content: center;\n  gap: 6px;\n  pointer-events: none;\n}\n.hb-qka-dot {\n  width: 6px;\n  height: 6px;\n  border-radius: 999px;\n  background: #3a3a3a;\n  transition: width 0.3s ease, background-color 0.3s ease;\n}\n.hb-qka-dot[data-on=\"true\"] { width: 18px; background: #ededed; }\n.hb-qka-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  overflow: hidden;\n  clip-path: inset(50%);\n  white-space: nowrap;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-qka-c { transition: none; }\n  .hb-qka-c[data-enter=\"true\"] { animation: none; }\n}\n`;\n\nconst defaultCards: StackCard[] = [\n  { title: \"Slow mornings\", meta: \"Prompt 01\", text: \"Write down one thing you noticed before checking your phone.\", color: \"#e0553f\" },\n  { title: \"The long way\", meta: \"Prompt 02\", text: \"Take a different route home and describe the first detail that surprised you.\", color: \"#2f7bff\" },\n  { title: \"Unsent letter\", meta: \"Prompt 03\", text: \"Draft a thank-you note to someone who will never read it.\", color: \"#2fa67c\" },\n  { title: \"Small repairs\", meta: \"Prompt 04\", text: \"Fix one tiny thing today that has annoyed you for months.\", color: \"#b8841c\" },\n  { title: \"Night inventory\", meta: \"Prompt 05\", text: \"List five sounds you can hear right now, from nearest to farthest.\", color: \"#8a62d6\" },\n];\n\nfunction hash(n: number) {\n  let h = Math.imul(n + 1, 2654435761) >>> 0;\n  h ^= h >>> 15;\n  h = Math.imul(h, 2246822519) >>> 0;\n  h ^= h >>> 13;\n  return (h >>> 0) / 4294967296;\n}\n\nexport function CardStackDrag({\n  cards = defaultCards,\n  value,\n  defaultValue = 0,\n  onChange,\n  ratio = 1.7,\n  idle = true,\n  className = \"\",\n  style,\n  onPointerEnter,\n  onPointerLeave,\n  onKeyDown,\n  onFocus,\n  onBlur,\n  ...rest\n}: CardStackDragProps) {\n  const root = React.useRef<HTMLDivElement>(null);\n  const nodes = React.useRef<(HTMLDivElement | null)[]>([]);\n  const n = cards.length;\n  const [inner, setInner] = React.useState(defaultValue);\n  const controlled = value !== undefined;\n  const index = n ? ((controlled ? value : inner) % n + n) % n : 0;\n  const [flying, setFlying] = React.useState(-1);\n  const [entering, setEntering] = React.useState(-1);\n  const user = React.useRef(false);\n  const visible = React.useRef(true);\n  const busy = React.useRef(false);\n  const drag = React.useRef({ id: -1, sx: 0, sy: 0, lx: 0, lt: 0, vx: 0, moved: false });\n  const indexRef = React.useRef(index);\n\n  React.useEffect(() => {\n    indexRef.current = index;\n  }, [index]);\n\n  const commit = React.useCallback(\n    (next: number) => {\n      indexRef.current = next;\n      if (!controlled) setInner(next);\n      onChange?.(next);\n    },\n    [controlled, onChange],\n  );\n\n  const fling = React.useCallback(\n    (card: number, dir: number, vx0: number, dx0: number, dy0: number) => {\n      const el = nodes.current[card];\n      const host = root.current;\n      if (!el || !host || busy.current) return;\n      busy.current = true;\n      el.style.transition = \"none\";\n      setFlying(card);\n      commit((indexRef.current + 1) % n);\n      const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n      const limit = host.clientWidth * 0.8 + el.offsetWidth * 0.6;\n      let x = dx0;\n      let y = dy0;\n      let vx = dir * Math.max(Math.abs(vx0), 900);\n      const spin = dir * 40;\n      let last = 0;\n      const finish = () => {\n        el.style.transition = \"none\";\n        el.style.transform = \"\";\n        el.style.opacity = \"0\";\n        setFlying(-1);\n        busy.current = false;\n        requestAnimationFrame(() =>\n          requestAnimationFrame(() => {\n            el.style.transition = \"\";\n            el.style.opacity = \"\";\n          }),\n        );\n      };\n      if (reduce) {\n        finish();\n        return;\n      }\n      const frame = (ms: number) => {\n        const dt = Math.min(0.04, last ? (ms - last) / 1000 : 0.016);\n        last = ms;\n        vx += dir * 1400 * dt;\n        x += vx * dt;\n        y += dt * 60;\n        const rot = x * 0.06 + spin * Math.min(1, Math.abs(x) / limit);\n        el.style.transform = `translate(${x.toFixed(1)}px, ${y.toFixed(1)}px) rotate(${rot.toFixed(2)}deg)`;\n        el.style.opacity = String(Math.max(0, 1 - Math.max(0, Math.abs(x) - limit * 0.55) / (limit * 0.45)));\n        if (Math.abs(x) >= limit) {\n          finish();\n          return;\n        }\n        requestAnimationFrame(frame);\n      };\n      requestAnimationFrame(frame);\n    },\n    [commit, n],\n  );\n\n  const next = React.useCallback(() => {\n    if (n < 2) return;\n    fling(indexRef.current, 1, 1100, 0, 0);\n  }, [fling, n]);\n\n  const prev = React.useCallback(() => {\n    if (n < 2 || busy.current) return;\n    const target = (indexRef.current - 1 + n) % n;\n    setEntering(target);\n    commit(target);\n  }, [commit, n]);\n\n  React.useEffect(() => {\n    const el = root.current;\n    if (!el || !idle || controlled || n < 2) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const io = new IntersectionObserver(([entry]) => {\n      visible.current = entry.isIntersecting;\n    });\n    io.observe(el);\n    const timer = window.setInterval(() => {\n      if (user.current || !visible.current || busy.current) return;\n      next();\n    }, 2800);\n    return () => {\n      io.disconnect();\n      window.clearInterval(timer);\n    };\n  }, [idle, controlled, n, next]);\n\n  const down = (e: React.PointerEvent<HTMLDivElement>, card: number) => {\n    if (e.button !== 0 || card !== indexRef.current || busy.current) return;\n    const el = e.currentTarget;\n    el.setPointerCapture(e.pointerId);\n    el.style.transition = \"none\";\n    el.dataset.drag = \"true\";\n    drag.current = { id: e.pointerId, sx: e.clientX, sy: e.clientY, lx: e.clientX, lt: e.timeStamp, vx: 0, moved: false };\n  };\n\n  const move = (e: React.PointerEvent<HTMLDivElement>) => {\n    const d = drag.current;\n    if (d.id !== e.pointerId) return;\n    const dx = e.clientX - d.sx;\n    const dy = e.clientY - d.sy;\n    if (Math.hypot(dx, dy) > 3) d.moved = true;\n    const dt = Math.max(1, e.timeStamp - d.lt);\n    d.vx = d.vx * 0.5 + (((e.clientX - d.lx) / dt) * 1000) * 0.5;\n    d.lx = e.clientX;\n    d.lt = e.timeStamp;\n    e.currentTarget.style.transform = `translate(${dx.toFixed(1)}px, ${(dy * 0.6).toFixed(1)}px) rotate(${(dx * 0.06).toFixed(2)}deg)`;\n  };\n\n  const up = (e: React.PointerEvent<HTMLDivElement>, card: number) => {\n    const d = drag.current;\n    if (d.id !== e.pointerId) return;\n    d.id = -1;\n    const el = e.currentTarget;\n    if (el.hasPointerCapture(e.pointerId)) el.releasePointerCapture(e.pointerId);\n    delete el.dataset.drag;\n    const dx = e.clientX - d.sx;\n    const dy = (e.clientY - d.sy) * 0.6;\n    const width = el.offsetWidth;\n    if (d.moved && (Math.abs(dx) > width * 0.28 || Math.abs(d.vx) > 650)) {\n      const dir = Math.abs(d.vx) > 650 ? Math.sign(d.vx) : Math.sign(dx) || 1;\n      fling(card, dir, d.vx, dx, dy);\n    } else {\n      el.style.transition = \"\";\n      el.style.transform = \"\";\n    }\n  };\n\n  const nav = (dir: number) => {\n    user.current = true;\n    if (dir > 0) next();\n    else prev();\n  };\n\n  return (\n    <div\n      ref={root}\n      role=\"group\"\n      aria-roledescription=\"card stack\"\n      aria-label=\"Card stack. Use the arrow keys to move between cards.\"\n      tabIndex={0}\n      className={`hb-qka ${className}`}\n      style={{ \"--hb-qka-r\": ratio, ...style } as React.CSSProperties}\n      onPointerEnter={(e) => {\n        onPointerEnter?.(e);\n        user.current = true;\n      }}\n      onPointerLeave={(e) => {\n        onPointerLeave?.(e);\n        user.current = false;\n      }}\n      onFocus={(e) => {\n        onFocus?.(e);\n        user.current = true;\n      }}\n      onBlur={(e) => {\n        onBlur?.(e);\n        user.current = false;\n      }}\n      onKeyDown={(e) => {\n        onKeyDown?.(e);\n        if (e.defaultPrevented) return;\n        if (e.key === \"ArrowRight\" || e.key === \"ArrowDown\") {\n          e.preventDefault();\n          nav(1);\n        } else if (e.key === \"ArrowLeft\" || e.key === \"ArrowUp\") {\n          e.preventDefault();\n          nav(-1);\n        }\n      }}\n      {...rest}\n    >\n      <style href=\"hb-card-stack-drag\" precedence=\"default\">{css}</style>\n      {cards.map((card, i) => {\n        const depth = (i - index + n) % n;\n        const top = depth === 0;\n        return (\n          <div\n            key={`${card.title}-${i}`}\n            ref={(node) => {\n              nodes.current[i] = node;\n            }}\n            className=\"hb-qka-c\"\n            data-top={top ? \"true\" : \"false\"}\n            data-hide={depth > 3 && i !== flying ? \"true\" : \"false\"}\n            data-fly={i === flying ? \"true\" : \"false\"}\n            data-enter={i === entering ? \"true\" : \"false\"}\n            aria-hidden={top ? undefined : true}\n            style={\n              {\n                \"--hb-qka-c\": card.color ?? \"#2f7bff\",\n                \"--hb-qka-z\": n - depth,\n                \"--hb-qka-d\": Math.min(depth, 3),\n                \"--hb-qka-r0\": `${top ? 0 : ((hash(i) - 0.5) * 7).toFixed(2)}deg`,\n              } as React.CSSProperties\n            }\n            onPointerDown={(e) => down(e, i)}\n            onPointerMove={move}\n            onPointerUp={(e) => up(e, i)}\n            onPointerCancel={(e) => up(e, i)}\n            onAnimationEnd={() => {\n              if (i === entering) setEntering(-1);\n            }}\n          >\n            {card.meta ? <span className=\"hb-qka-meta\">{card.meta}</span> : <span />}\n            <div>\n              <h3 className=\"hb-qka-title\">{card.title}</h3>\n              {card.text ? <p className=\"hb-qka-text\">{card.text}</p> : null}\n            </div>\n          </div>\n        );\n      })}\n      <div className=\"hb-qka-dots\" aria-hidden=\"true\">\n        {cards.map((card, i) => (\n          <span className=\"hb-qka-dot\" key={`${card.title}-${i}`} data-on={i === index ? \"true\" : \"false\"} />\n        ))}\n      </div>\n      <span className=\"hb-qka-sr\" role=\"status\" aria-live=\"polite\">\n        {cards[index] ? `Card ${index + 1} of ${n}: ${cards[index].title}` : \"\"}\n      </span>\n    </div>\n  );\n}\n"
    }
  ]
}