{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"word-pile","type":"registry:ui","title":"Word Pile","description":"A paragraph whose words tumble down and pile up with real physics on hover, click or view, then can be grabbed and tossed around.","dependencies":["matter-js"],"registryDependencies":[],"files":[{"path":"registry/ui/word-pile.tsx","type":"registry:ui","content":"\"use client\";\n\nimport Matter from \"matter-js\";\nimport { useEffect, useRef, useState } from \"react\";\n\nexport interface WordPileProps {\n  text?: string;\n  /** Words (case-insensitive, punctuation ignored) drawn in the accent color. */\n  highlightWords?: string[];\n  /** What drops the words. `auto` drops them on mount. */\n  trigger?: \"hover\" | \"click\" | \"view\" | \"auto\";\n  /** Gravity strength. */\n  gravity?: number;\n  /** Bounciness, 0-1. */\n  restitution?: number;\n  highlightColor?: string;\n  className?: string;\n}\n\nconst clean = (w: string) => w.toLowerCase().replace(/[^\\p{L}\\p{N}]/gu, \"\");\n\n/** Remounts the physics scene whenever an option changes. */\nexport function WordPile(props: WordPileProps) {\n  const { text, trigger, gravity, restitution, highlightWords } = props;\n  return <Scene key={`${text}|${trigger}|${gravity}|${restitution}|${highlightWords?.join()}`} {...props} />;\n}\n\nfunction Scene({\n  text = \"React components that move you. Drop them in, drag them around, and ship interfaces people remember.\",\n  highlightWords = [\"move\", \"drag\", \"ship\", \"remember\"],\n  trigger = \"hover\",\n  gravity = 1,\n  restitution = 0.4,\n  highlightColor = \"#a3e635\",\n  className,\n}: WordPileProps) {\n  const root = useRef<HTMLDivElement>(null);\n  const words = useRef<(HTMLSpanElement | null)[]>([]);\n  const [started, setStarted] = useState(false);\n\n  useEffect(() => {\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    if (trigger === \"auto\") {\n      const t = setTimeout(() => setStarted(true), 400);\n      return () => clearTimeout(t);\n    }\n    if (trigger !== \"view\" || !root.current) return;\n    const io = new IntersectionObserver(([entry]) => entry.isIntersecting && setStarted(true), { threshold: 0.4 });\n    io.observe(root.current);\n    return () => io.disconnect();\n  }, [trigger]);\n\n  useEffect(() => {\n    if (!started) return;\n    const el = root.current!;\n    const { Engine, Bodies, Body, Composite, Constraint, Query } = Matter;\n    const box = el.getBoundingClientRect();\n    const W = box.width;\n    const H = box.height;\n    const engine = Engine.create({ gravity: { x: 0, y: gravity } });\n    const wall = { isStatic: true };\n    Composite.add(engine.world, [\n      Bodies.rectangle(W / 2, H + 50, W * 3, 100, wall),\n      Bodies.rectangle(-50, 0, 100, H * 4, wall),\n      Bodies.rectangle(W + 50, 0, 100, H * 4, wall),\n    ]);\n\n    // Measure every word first, then go absolute, so the paragraph does not reflow mid-measure.\n    const spans = words.current.filter((s): s is HTMLSpanElement => !!s);\n    const rects = spans.map((s) => s.getBoundingClientRect());\n    const bodies = rects.map((r) => {\n      const b = Bodies.rectangle(r.left - box.left + r.width / 2, r.top - box.top + r.height / 2, r.width + 8, r.height, {\n        restitution,\n        friction: 0.3,\n        frictionAir: 0.012,\n        chamfer: { radius: Math.min(r.height / 2, 10) },\n      });\n      Body.setVelocity(b, { x: (Math.random() - 0.5) * 4, y: -Math.random() * 2 });\n      Body.setAngularVelocity(b, (Math.random() - 0.5) * 0.06);\n      return b;\n    });\n    spans.forEach((s, i) => {\n      Object.assign(s.style, { position: \"absolute\", left: \"0\", top: \"0\", width: `${rects[i].width}px`, margin: \"0\" });\n    });\n    Composite.add(engine.world, bodies);\n\n    let grab: Matter.Constraint | null = null;\n    const local = (e: PointerEvent) => {\n      const r = el.getBoundingClientRect();\n      return { x: e.clientX - r.left, y: e.clientY - r.top };\n    };\n    const down = (e: PointerEvent) => {\n      const p = local(e);\n      const body = Query.point(bodies, p)[0];\n      if (!body) return;\n      grab = Constraint.create({\n        pointA: p,\n        bodyB: body,\n        pointB: { x: p.x - body.position.x, y: p.y - body.position.y },\n        length: 0,\n        stiffness: 0.2,\n        damping: 0.1,\n      });\n      Composite.add(engine.world, grab);\n      el.setPointerCapture(e.pointerId);\n      el.style.cursor = \"grabbing\";\n    };\n    const move = (e: PointerEvent) => {\n      if (grab) grab.pointA = local(e);\n    };\n    const up = () => {\n      if (grab) Composite.remove(engine.world, grab);\n      grab = null;\n      el.style.cursor = \"grab\";\n    };\n\n    let raf = 0;\n    let visible = true;\n    const loop = () => {\n      Engine.update(engine, 1000 / 60);\n      bodies.forEach((b, i) => {\n        const r = rects[i];\n        spans[i].style.transform = `translate(${b.position.x - r.width / 2}px, ${b.position.y - r.height / 2}px) rotate(${b.angle}rad)`;\n      });\n      raf = visible ? requestAnimationFrame(loop) : 0;\n    };\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf) raf = requestAnimationFrame(loop);\n    });\n    io.observe(el);\n    el.style.cursor = \"grab\";\n    el.addEventListener(\"pointerdown\", down);\n    el.addEventListener(\"pointermove\", move);\n    el.addEventListener(\"pointerup\", up);\n    el.addEventListener(\"pointercancel\", up);\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      el.removeEventListener(\"pointerdown\", down);\n      el.removeEventListener(\"pointermove\", move);\n      el.removeEventListener(\"pointerup\", up);\n      el.removeEventListener(\"pointercancel\", up);\n      Engine.clear(engine);\n    };\n  }, [started, gravity, restitution]);\n\n  const hl = new Set(highlightWords.map(clean));\n  const start = () => {\n    if (!window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) setStarted(true);\n  };\n  return (\n    <div\n      ref={root}\n      className={className}\n      onPointerEnter={trigger === \"hover\" ? start : undefined}\n      onClick={trigger === \"click\" ? start : undefined}\n      style={{\n        position: \"relative\",\n        width: \"100%\",\n        height: \"100%\",\n        overflow: \"hidden\",\n        display: \"grid\",\n        placeItems: \"center\",\n        cursor: !started && trigger === \"click\" ? \"pointer\" : undefined,\n        userSelect: started ? \"none\" : undefined,\n      }}\n    >\n      <span className=\"sr-only\">{text}</span>\n      <p aria-hidden style={{ margin: 0, textAlign: \"center\", maxWidth: \"min(90%, 44rem)\" }}>\n        {text.split(/\\s+/).map((w, i) => (\n          <span\n            key={i}\n            ref={(node) => {\n              words.current[i] = node;\n            }}\n            style={{\n              display: \"inline-block\",\n              margin: \"0 0.14em\",\n              whiteSpace: \"nowrap\",\n              touchAction: started ? \"none\" : undefined,\n              willChange: started ? \"transform\" : undefined,\n              color: hl.has(clean(w)) ? highlightColor : undefined,\n            }}\n          >\n            {w}\n          </span>\n        ))}\n      </p>\n    </div>\n  );\n}\n"}]}