{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gravity-letters",
  "title": "Gravity Letters",
  "description": "Brush a letter and it falls: letters drop with real gravity, bounce on the floor, then climb back to spell the word again.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/gravity-letters/gravity-letters.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface GravityLettersProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"children\"\n> {\n  text?: string;\n  /** How far below the text the letters can fall, in px. */\n  drop?: number;\n  /** How close the pointer has to come to knock a letter loose, in px. */\n  reach?: number;\n  /** Milliseconds a letter rests before it climbs back. */\n  rest?: number;\n}\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\ninterface Letter {\n  state: \"home\" | \"fall\" | \"rest\" | \"back\";\n  x: number;\n  y: number;\n  vx: number;\n  vy: number;\n  turn: number;\n  spin: number;\n  landed: number;\n}\n\nconst GRAVITY = 2600;\n\nexport function GravityLetters({\n  text = \"Mind the drop\",\n  drop = 200,\n  reach = 44,\n  rest = 1300,\n  className,\n  style,\n  onPointerMove,\n  ...props\n}: GravityLettersProps) {\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const letterRefs = React.useRef<(HTMLSpanElement | null)[]>([]);\n  const letters = React.useRef<Letter[]>([]);\n  const start = React.useRef<() => void>(() => {});\n  const last = React.useRef({ x: 0, t: 0 });\n  const chars = Array.from(text);\n  // Letters sit inside word wrappers, so a line can only break between words.\n  const words = text.split(\" \");\n  const starts = words.map((_, w) =>\n    words.slice(0, w).reduce((n, word) => n + Array.from(word).length + 1, 0),\n  );\n\n  // Letters only cost anything while at least one of them is off its spot.\n  React.useEffect(() => {\n    letters.current = chars.map(() => ({\n      state: \"home\",\n      x: 0,\n      y: 0,\n      vx: 0,\n      vy: 0,\n      turn: 0,\n      spin: 0,\n      landed: 0,\n    }));\n    let raf = 0;\n    let prev = 0;\n    const frame = (now: number) => {\n      const dt = Math.min(0.033, prev ? (now - prev) / 1000 : 1 / 60);\n      prev = now;\n      let busy = false;\n      letters.current.forEach((l, i) => {\n        const el = letterRefs.current[i];\n        if (!el || l.state === \"home\") return;\n        busy = true;\n        if (l.state === \"fall\") {\n          l.vy += GRAVITY * dt;\n          l.x += l.vx * dt;\n          l.y += l.vy * dt;\n          l.turn += l.spin * dt;\n          if (l.y > drop) {\n            l.y = drop;\n            l.vy *= -0.38;\n            l.vx *= 0.7;\n            l.spin *= 0.6;\n            if (Math.abs(l.vy) < 90) {\n              l.vy = 0;\n              l.state = \"rest\";\n              l.landed = now;\n            }\n          }\n        } else if (l.state === \"rest\") {\n          l.turn *= 0.9;\n          if (now - l.landed > rest) l.state = \"back\";\n        } else {\n          const k = 1 - Math.exp(-dt * 7);\n          l.x -= l.x * k;\n          l.y -= l.y * k;\n          l.turn -= l.turn * k;\n          if (Math.abs(l.y) < 0.6 && Math.abs(l.x) < 0.6) {\n            l.x = 0;\n            l.y = 0;\n            l.turn = 0;\n            l.state = \"home\";\n          }\n        }\n        el.style.transform = `translate(${l.x.toFixed(1)}px, ${l.y.toFixed(1)}px) rotate(${l.turn.toFixed(1)}deg)`;\n      });\n      if (busy) raf = requestAnimationFrame(frame);\n      else {\n        raf = 0;\n        prev = 0;\n      }\n    };\n    start.current = () => {\n      if (!raf) raf = requestAnimationFrame(frame);\n    };\n    return () => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n    };\n    // The letter list only changes with the text.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [text, drop, rest]);\n\n  return (\n    <div\n      ref={rootRef}\n      className={`select-none ${className ?? \"\"}`}\n      style={{ paddingBottom: drop * 0.9, ...style }}\n      role=\"text\"\n      aria-label={text}\n      onPointerMove={(e) => {\n        onPointerMove?.(e);\n        if (reduce || e.pointerType !== \"mouse\") return;\n        const dt = Math.max(8, e.timeStamp - last.current.t);\n        // Capped, so a jump of the pointer can't throw a letter off the page.\n        const speed = Math.max(\n          -9,\n          Math.min(9, ((e.clientX - last.current.x) / dt) * 16),\n        );\n        last.current = { x: e.clientX, t: e.timeStamp };\n        letterRefs.current.forEach((el, i) => {\n          const l = letters.current[i];\n          if (!el || !l || l.state !== \"home\") return;\n          const r = el.getBoundingClientRect();\n          const dx = e.clientX - (r.left + r.width / 2);\n          const dy = e.clientY - (r.top + r.height / 2);\n          if (dx * dx + dy * dy > reach * reach) return;\n          l.state = \"fall\";\n          l.vx = speed * 22;\n          l.vy = -260;\n          l.spin = speed * 40 + (i % 2 ? 90 : -90);\n          start.current();\n        });\n      }}\n      {...props}\n    >\n      <span aria-hidden=\"true\">\n        {words.map((word, w) => (\n          <React.Fragment key={w}>\n            <span className=\"inline-block whitespace-nowrap\">\n              {Array.from(word).map((c, j) => (\n                <span\n                  key={j}\n                  ref={(el) => {\n                    letterRefs.current[starts[w] + j] = el;\n                  }}\n                  className=\"inline-block will-change-transform\"\n                >\n                  {c}\n                </span>\n              ))}\n            </span>\n            {w < words.length - 1 && \" \"}\n          </React.Fragment>\n        ))}\n      </span>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}