{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"squish-flip","type":"registry:component","title":"Squish Flip","description":"A list whose items squash and stretch as they move: reorder, filter or add and each item travels to its new place stretched along its path by its speed, squashes as it lands, and settles; new items pop in.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/squish-flip.tsx","content":"\"use client\";\n\nimport { useEffect, useLayoutEffect, useRef, useSyncExternalStore } from \"react\";\nimport type { ReactNode } from \"react\";\n\n/**\n * SquishFlip — a list whose items squash and stretch as they move to their new places.\n *\n * Reorder, filter or add items and each travels on a spring, stretched\n * along its path by its speed (area kept), squashing as it lands. New\n * items pop in from small. Any keyed list: items, a key, a render function.\n *\n * Part of the Tactile Maximalism kit: dough #f7f0e4, warm ink #17130f and\n * one loud accent, bubblegum #ff2d78, filled once per screen; grain fixed to\n * the material; soft shadows tinted by what casts them, never a hard offset.\n * Everything soft keeps its area as it squashes and settles inside 450ms.\n * Unbounded through var(--font-unbounded) for display over Geist. Respects\n * prefers-reduced-motion. No dependencies beyond React. Paste it as its own\n * file: it repeats the kit's small helpers, which would clash in one module.\n * Load Unbounded with next/font (variable: \"--font-unbounded\") on a\n * parent, or from Google Fonts or @fontsource.\n */\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\n/** True when the visitor asked for reduced motion. False on the server. */\nfunction useReducedMotion(): boolean {\n  return useSyncExternalStore(\n    subscribeMotion,\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\ntype TmFlipState = { x: number; y: number; vx: number; vy: number; s: number; sv: number; th: number; pop: number; popv: number };\n\n/**\n * SquishFlip — a list whose items squash and stretch as they move to their\n * new places.\n *\n * Reorder, filter or add items and each one travels from where it was to\n * where it is now on a spring, stretched along its path in proportion to its\n * speed (its area kept: longer means thinner), then squashes as it lands and\n * settles, one overshoot, inside ~450ms. New items pop in from small. Works\n * for any keyed list: pass the items, a key and a render function. Reduced\n * motion moves them instantly.\n */\nexport function SquishFlip<T>({\n  items,\n  getKey,\n  render,\n  className = \"\",\n  itemClassName = \"\",\n  label,\n}: {\n  items: T[];\n  getKey: (item: T) => string;\n  render: (item: T) => ReactNode;\n  className?: string;\n  itemClassName?: string;\n  /** An accessible name for the list. */\n  label?: string;\n}) {\n  const reduced = useReducedMotion();\n  const els = useRef(new Map<string, HTMLLIElement>());\n  const prev = useRef(new Map<string, { x: number; y: number }>());\n  const sim = useRef(new Map<string, TmFlipState>());\n  const loop = useRef({ raf: 0, last: 0 });\n  const tickRef = useRef<(now: number) => void>(() => undefined);\n  const keys = items.map(getKey).join(\"|\");\n\n  useEffect(() => {\n    const lp = loop.current;\n    tickRef.current = (now: number) => {\n      lp.raf = 0;\n      const dt = lp.last ? Math.min(1 / 30, (now - lp.last) / 1000) : 1 / 60;\n      lp.last = now;\n      let moving = false;\n      sim.current.forEach((st, key) => {\n        for (let i = 0; i < 3; i++) {\n          const h = dt / 3;\n          // Travel: k 260, c 24. Stretch follows speed on its own spring (k 520, c 18), so it overshoots into a squash on landing.\n          st.vx += (-260 * st.x - 24 * st.vx) * h;\n          st.vy += (-260 * st.y - 24 * st.vy) * h;\n          st.x += st.vx * h;\n          st.y += st.vy * h;\n          const speed = Math.hypot(st.vx, st.vy);\n          st.sv += (520 * (1 + Math.min(0.32, speed * 0.00042) - st.s) - 18 * st.sv) * h;\n          st.s += st.sv * h;\n          st.popv += (420 * (1 - st.pop) - 18 * st.popv) * h;\n          st.pop += st.popv * h;\n          if (speed > 30) st.th = Math.atan2(st.vy, st.vx);\n        }\n        const el = els.current.get(key);\n        const done = Math.abs(st.x) + Math.abs(st.y) < 0.2 && Math.abs(st.vx) + Math.abs(st.vy) < 2 && Math.abs(st.s - 1) < 0.002 && Math.abs(st.sv) < 0.02 && Math.abs(st.pop - 1) < 0.002 && Math.abs(st.popv) < 0.02;\n        if (el) {\n          const deg = (st.th * 180) / Math.PI;\n          el.style.transform = done\n            ? \"\"\n            : `translate(${st.x.toFixed(2)}px, ${st.y.toFixed(2)}px) rotate(${deg.toFixed(2)}deg) scale(${(st.s * st.pop).toFixed(4)}, ${(st.pop / st.s).toFixed(4)}) rotate(${(-deg).toFixed(2)}deg)`;\n        }\n        if (done) sim.current.delete(key);\n        else moving = true;\n      });\n      if (moving) lp.raf = requestAnimationFrame((n) => tickRef.current(n));\n      else lp.last = 0;\n    };\n    return () => {\n      cancelAnimationFrame(lp.raf);\n      lp.raf = 0;\n      lp.last = 0;\n    };\n  }, []);\n\n  // After every layout change: where did each item come from? Start it there and let it spring home.\n  useLayoutEffect(() => {\n    const next = new Map<string, { x: number; y: number }>();\n    els.current.forEach((el, key) => {\n      if (!el.isConnected) {\n        els.current.delete(key);\n        return;\n      }\n      // Measure without the running transform.\n      const st = sim.current.get(key);\n      const x = el.offsetLeft;\n      const y = el.offsetTop;\n      next.set(key, { x, y });\n      const was = prev.current.get(key);\n      if (reduced) return;\n      if (!was) {\n        if (prev.current.size) sim.current.set(key, { x: 0, y: 0, vx: 0, vy: 0, s: 1, sv: 0, th: 0, pop: 0.35, popv: 0 });\n        return;\n      }\n      const dx = was.x + (st?.x ?? 0) - x;\n      const dy = was.y + (st?.y ?? 0) - y;\n      if (Math.abs(dx) + Math.abs(dy) < 0.5 && !st) return;\n      sim.current.set(key, { x: dx, y: dy, vx: st?.vx ?? 0, vy: st?.vy ?? 0, s: st?.s ?? 1, sv: st?.sv ?? 0, th: st?.th ?? Math.atan2(-dy, -dx), pop: st?.pop ?? 1, popv: st?.popv ?? 0 });\n    });\n    prev.current = next;\n    const lp = loop.current;\n    if (sim.current.size && !lp.raf) lp.raf = requestAnimationFrame((n) => tickRef.current(n));\n  }, [keys, reduced]);\n\n  return (\n    <ul aria-label={label} className={\"tm-scope relative m-0 flex list-none flex-wrap p-0 \" + className}>\n      {items.map((item) => {\n        const key = getKey(item);\n        return (\n          <li\n            key={key}\n            ref={(el) => {\n              if (el) els.current.set(key, el);\n            }}\n            className={\"relative \" + itemClassName}\n            style={{ willChange: \"transform\", transition: \"none\" }}\n          >\n            {render(item)}\n          </li>\n        );\n      })}\n    </ul>\n  );\n}\n","type":"registry:component","target":"components/viberdy/squish-flip.tsx"}],"meta":{"tier":"free","tags":["animation","flip","layout animation","list","squash and stretch","spring","filter"],"href":"https://viberdy.dev/library/squish-flip","aiPrompt":"Build a React component, `SquishFlip<T>({ items, getKey, render, className, itemClassName, label })`, rendering a flex-wrap <ul> of <li>s.\n\nTactile Maximalism grammar: loud but composed. A dough ground #f7f0e4 over most of the page (surfaces #fffaf1, a pressed well #efe5d3), warm ink #17130f at 92/64/42%, hairlines at 10% and 18%; ONE loud accent, bubblegum #ff2d78 (deep #d8155d), as a filled thing once per screen, and one quiet tint, lavender #e8e1f6, only on small chips, never a second loud colour. Grain at 4-6%, monochrome, static, fixed to the material. Depth is soft and tinted: a contact shadow 0 2px 6px rgba(23,19,15,0.1) plus a wide one 0 18px 40px -8px tinted by what casts it (a gum chip casts pink); pressed, both collapse to 0 1px 3px rgba(23,19,15,0.14). Never a hard offset shadow (that is neo-brutalism). Type: Unbounded at 800 through var(--font-unbounded, \"Unbounded\", ...) for one oversized headline per screen (up to ~15cqw, leading 0.92, tracking -0.045em), Geist for reading at 16-17px/1.5, Geist Mono uppercase at 11-12px for labels. Full pills for every control and chip, 28px cards, 20px nested media. Motion: everything soft keeps its area as it squashes (scaleX = 1 / scaleY about its foot), deforms in proportion to the input (where, how long, how fast), overshoots once and settles inside 450ms (a spring near k 420, c 18), and always returns to one legible rest shape. Reduced motion keeps the states and drops the springs.\n\nAfter every change of the keys (useLayoutEffect), measure each item's offsetLeft/offsetTop (layout, not transformed) and compare with the last measurement: an item that moved starts at its old place (dx, dy, carrying over any motion still running) and a new item starts at scale 0.35 (only once the list has rendered before). One rAF loop, three substeps a frame, per moving item: travel springs (k 260, c 24) to 0; a stretch s on its own spring (k 520, c 18) toward 1 + min(0.32, speed * 0.00042); a pop spring (k 420, c 18) to 1; the travel angle held once speed passes 30 px/s. Write translate(x, y) rotate(a) scale(s * pop, pop / s) rotate(-a), and clear the transform when the item is done. Reduced motion: no animation.","license":"https://viberdy.dev/license"}}