{
  "$schema": "../../schema.json",
  "id": "snap-scroll",
  "type": "component",
  "title": "Snap scroll",
  "summary": "A vertical reel of cards scrolls past in 3D and snaps onto B; neighbours dim and tilt away.",
  "tags": [
    "component",
    "3d",
    "list",
    "ui"
  ],
  "version": "1.0.0",
  "license": "MIT",
  "export": "snapScroll",
  "signature": "snapScroll(a, b, p, { duration, count, gap, perspective })",
  "params": [
    {
      "name": "duration",
      "type": "number",
      "default": 1.2,
      "description": "length of the transition in seconds; springs use it to keep their feel",
      "min": 0.2,
      "max": 3,
      "step": 0.05,
      "group": "main"
    },
    {
      "name": "count",
      "type": "number",
      "default": 3,
      "description": "number of cards in the reel",
      "min": 2,
      "max": 8,
      "step": 1,
      "group": "main"
    },
    {
      "name": "gap",
      "type": "number",
      "default": 0.1,
      "description": "gap between cards (fraction)",
      "min": 0,
      "max": 0.3,
      "step": 0.01,
      "group": "main"
    },
    {
      "name": "perspective",
      "type": "number",
      "default": 1600,
      "description": "3D perspective in px",
      "min": 400,
      "max": 4000,
      "step": 50,
      "group": "main"
    }
  ],
  "install": {
    "kind": "code"
  },
  "files": [
    {
      "path": "moves/snap-scroll.js",
      "content": "import { clamp, lerp, progress } from 'paper-motion';\nimport { dims, show, sp, settle, resetAll, copy } from 'paper-motion/kit';\n\n/** A vertical reel of cards (copies of A and B between them) scrolls past in 3D and snaps onto B; neighbours dim, shrink and tilt away. */\nexport function snapScroll(a, b, p, { duration = 1.2, count = 3, gap = 0.1, perspective = 1600 } = {}) {\n  const slot = a.parentElement, [, h] = dims(a);\n  slot._pmReel ??= [];\n  const reel = slot._pmReel;\n  if (p <= 0 || p >= 1) { reel.forEach((g) => { g.style.display = 'none'; }); resetAll(a, b); show(a, b, p); return; }\n  slot.style.perspective = `${perspective}px`;\n  while (reel.length < count - 1) { const g = document.createElement('div'); g.className = 'pm-fx'; g.style.position = 'absolute'; slot.append(g); reel.push(g); }\n  const list = [a];\n  for (let j = 1; j < count; j++) { const g = reel[j - 1], src = j % 2 ? b : a; g.replaceChildren(copy(src)); Object.assign(g.style, { display: 'block', left: `${src.offsetLeft}px`, top: `${src.offsetTop}px`, width: `${src.offsetWidth}px`, height: `${src.offsetHeight}px`, zIndex: 1 }); list.push(g); }\n  list.push(b);\n  const pos = count * settle(sp(p, 0, duration, { stiffness: 55, damping: 10 }), p, 0.8), env = progress(p, 0, 0.15) * (1 - progress(p, 0.85, 1));\n  list.forEach((el, j) => {\n    const d = j - pos, foc = clamp(1 - Math.abs(d)), near = Math.abs(d) < 2.2;\n    el.style.transform = `translateY(${d * h * (1 + gap)}px) rotateX(${-d * 16}deg) scale(${0.8 + 0.2 * foc})`;\n    el.style.filter = `brightness(${0.3 + 0.7 * foc})`;\n    el.style.opacity = near ? lerp(env, 1, foc) * (1 - clamp(Math.abs(d) - 1.2)) : 0;\n    el.style.zIndex = Math.round(10 - Math.abs(d) * 3);\n  });\n}\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.0"
  },
  "usage": "import { snapScroll } from './moves/snap-scroll.js';\n\n// A and B (two states of a card) stacked in one positioned slot; 1.1 s from 2.0 s:\ns.update((t) => snapScroll(cardA, cardB, progress(t, 2.0, 3.1), { duration: 1.1 }));",
  "notes": [
    "A vertical reel of cards (copies of A and B between them) scrolls past in 3D and snaps onto B; neighbours dim, shrink and tilt away.",
    "Stack A and B in one positioned slot (their parent); give the slot overflow:hidden to keep travel inside it.",
    "Pass the real duration in seconds and call it through p = 1: that is when it hides the layers it made.",
    "Render with --motion-blur 5.",
    "Draws live copies of the card: a Paper shader canvas inside comes out blank."
  ],
  "preview": {
    "video": "previews/snap-scroll.webm",
    "poster": "previews/snap-scroll.jpg"
  },
  "mood": [
    "technical",
    "minimal"
  ],
  "energy": 3,
  "role": "swap",
  "when": [
    "lists, feeds, choosing one of many",
    "app and product UI"
  ]
}