{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "reactive-cube-grid",
  "title": "Reactive Cube Grid",
  "description": "An isometric field of cubes that rises under the pointer like a pin-art toy: cubes spring up, overshoot and settle, glow in an accent colour as they climb, and a click sends a ring-shaped wave rolling out across the grid.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/reactive-cube-grid/reactive-cube-grid.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface ReactiveCubeGridProps extends React.ComponentPropsWithoutRef<\"div\"> {\n  /** Width of one cube's top face, in px. */\n  size?: number;\n  /** How high cubes rise under the pointer, in px. */\n  lift?: number;\n  /** How far the pointer's pull reaches, in cubes. */\n  radius?: number;\n  /** Colour of cubes at rest. */\n  color?: string;\n  /** Colour cubes turn as they rise. */\n  accent?: string;\n  background?: string;\n  /** Let a soft hotspot wander across the grid while no pointer is over it. */\n  idle?: boolean;\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\nfunction rgb(hex: string): [number, number, number] {\n  const h = hex.replace(\"#\", \"\");\n  const full = h.length === 3 ? h.split(\"\").map((c) => c + c).join(\"\") : h.slice(0, 6);\n  const n = parseInt(full, 16) || 0;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\ntype Ripple = { i: number; j: number; t: number };\n\nconst REST = 6;\n\nexport function ReactiveCubeGrid({ size = 48, lift = 64, radius = 2.6, color = \"#23252d\", accent = \"#7c6cff\", background = \"#0d0e12\", idle = true, className, style, children, ...props }: ReactiveCubeGridProps) {\n  const reduce = useReducedMotion();\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const live = React.useRef({ size, lift, radius, color, accent, idle });\n  const kick = React.useRef<() => void>(() => {});\n  React.useEffect(() => {\n    live.current = { size, lift, radius, color, accent, idle };\n    kick.current();\n  }, [size, lift, radius, color, accent, idle]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!root || !canvas || !ctx) return;\n    let w = 0;\n    let h = 0;\n    let raf = 0;\n    let visible = true;\n    let last = 0;\n    let time = 0;\n    // Cells in back-to-front order (by i + j), with each cube's height and vertical speed.\n    let ci = new Int16Array(0);\n    let cj = new Int16Array(0);\n    let z = new Float32Array(0);\n    let vz = new Float32Array(0);\n    let builtFor = \"\";\n    const ripples: Ripple[] = [];\n    const hot = { x: 0, y: 0, px: 0, py: 0, hover: false, pull: 0 };\n\n    const build = () => {\n      const L = live.current;\n      const key = `${w}x${h}@${L.size}:${L.lift}`;\n      if (key === builtFor) return;\n      builtFor = key;\n      const tw = L.size;\n      const th = L.size / 2;\n      // Grid coordinates of the four screen corners, to find which cells can show.\n      const toGrid = (x: number, y: number) => {\n        const a = (x - w / 2) / (tw / 2);\n        const b = (y - h / 2) / (th / 2);\n        return [(a + b) / 2, (b - a) / 2];\n      };\n      const cs = [toGrid(0, 0), toGrid(w, 0), toGrid(0, h + L.lift + th * 2), toGrid(w, h + L.lift + th * 2)];\n      const i0 = Math.floor(Math.min(...cs.map((c) => c[0]))) - 1;\n      const i1 = Math.ceil(Math.max(...cs.map((c) => c[0]))) + 1;\n      const j0 = Math.floor(Math.min(...cs.map((c) => c[1]))) - 1;\n      const j1 = Math.ceil(Math.max(...cs.map((c) => c[1]))) + 1;\n      const cells: [number, number][] = [];\n      for (let i = i0; i <= i1; i++)\n        for (let j = j0; j <= j1; j++) {\n          const X = w / 2 + (i - j) * (tw / 2);\n          const Y = h / 2 + (i + j) * (th / 2);\n          if (X < -tw || X > w + tw || Y < -th || Y > h + L.lift + th * 2) continue;\n          cells.push([i, j]);\n        }\n      cells.sort((a, b) => a[0] + a[1] - (b[0] + b[1]));\n      ci = Int16Array.from(cells, (c) => c[0]);\n      cj = Int16Array.from(cells, (c) => c[1]);\n      z = new Float32Array(cells.length).fill(REST);\n      vz = new Float32Array(cells.length);\n    };\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = root.clientWidth;\n      h = root.clientHeight;\n      canvas.width = Math.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      build();\n    };\n    const gridAt = (x: number, y: number) => {\n      const L = live.current;\n      const a = (x - w / 2) / (L.size / 2);\n      // Aim a little below the pointer: raised cubes sit higher on screen than their footprint.\n      const b = (y + L.lift * 0.45 - h / 2) / (L.size / 4);\n      return { i: (a + b) / 2, j: (b - a) / 2 };\n    };\n\n    const step = (dt: number) => {\n      const L = live.current;\n      build();\n      time += dt;\n      // The hotspot: the pointer, or a slow wandering point when idle; `pull` eases between them.\n      const wx = w / 2 + Math.sin(time * 0.45) * w * 0.3;\n      const wy = h / 2 + Math.sin(time * 0.7 + 1) * h * 0.22;\n      hot.pull += ((hot.hover ? 1 : 0) - hot.pull) * (1 - Math.exp(-dt * 4));\n      hot.x = hot.px * hot.pull + wx * (1 - hot.pull);\n      hot.y = hot.py * hot.pull + wy * (1 - hot.pull);\n      const g = gridAt(hot.x, hot.y);\n      const strength = L.idle ? 0.55 + 0.45 * hot.pull : hot.pull;\n      const s2 = 2 * L.radius * L.radius;\n      for (let r = ripples.length - 1; r >= 0; r--) {\n        ripples[r].t += dt;\n        if (ripples[r].t > 2.6) ripples.splice(r, 1);\n      }\n      const k = reduce ? 0 : 170;\n      const damp = reduce ? 0 : 2 * Math.sqrt(170) * 0.42;\n      for (let n = 0; n < z.length; n++) {\n        const di = ci[n] - g.i;\n        const dj = cj[n] - g.j;\n        let target = REST + L.lift * strength * Math.exp(-(di * di + dj * dj) / s2);\n        // Click ripples: a ring of lifted cubes running outward, fading as it goes.\n        for (const rp of ripples) {\n          const d = Math.hypot(ci[n] - rp.i, cj[n] - rp.j) - rp.t * 9;\n          target += L.lift * 0.75 * Math.exp(-d * d * 0.7) * Math.exp(-rp.t * 1.3);\n        }\n        if (reduce) {\n          z[n] = target;\n          continue;\n        }\n        // Each cube is a little spring, so it overshoots and settles instead of sliding.\n        vz[n] += ((target - z[n]) * k - vz[n] * damp) * dt;\n        z[n] += vz[n] * dt;\n      }\n    };\n\n    const draw = () => {\n      const L = live.current;\n      const [br, bg, bb] = rgb(L.color);\n      const [ar, ag, ab] = rgb(L.accent);\n      const tw = L.size;\n      const th = L.size / 2;\n      // Draw the faces slightly smaller than the cell so a hairline of shadow separates the cubes.\n      const hw = (tw / 2) * 0.94;\n      const hh = (th / 2) * 0.94;\n      ctx.clearRect(0, 0, w, h);\n      const shade = (m: number, t: number, glow: number) =>\n        `rgb(${Math.min(255, (br + (ar - br) * t) * m + glow) | 0},${Math.min(255, (bg + (ag - bg) * t) * m + glow) | 0},${Math.min(255, (bb + (ab - bb) * t) * m + glow) | 0})`;\n      for (let n = 0; n < z.length; n++) {\n        const i = ci[n];\n        const j = cj[n];\n        const X = w / 2 + (i - j) * (tw / 2);\n        const G = h / 2 + (i + j) * (th / 2);\n        const Z = Math.max(0, z[n]);\n        const T = G - Z;\n        const t = Math.min(1, Math.max(0, (Z - REST) / L.lift));\n        const tc = Math.pow(t, 0.75);\n        // Left face.\n        ctx.fillStyle = shade(0.72, tc, 0);\n        ctx.beginPath();\n        ctx.moveTo(X - hw, T);\n        ctx.lineTo(X, T + hh);\n        ctx.lineTo(X, G + hh);\n        ctx.lineTo(X - hw, G);\n        ctx.fill();\n        // Right face, in shadow.\n        ctx.fillStyle = shade(0.5, tc, 0);\n        ctx.beginPath();\n        ctx.moveTo(X, T + hh);\n        ctx.lineTo(X + hw, T);\n        ctx.lineTo(X + hw, G);\n        ctx.lineTo(X, G + hh);\n        ctx.fill();\n        // Top face, lit, glowing a little as the cube rises.\n        ctx.fillStyle = shade(1.12, tc, t * 28);\n        ctx.beginPath();\n        ctx.moveTo(X, T - hh);\n        ctx.lineTo(X + hw, T);\n        ctx.lineTo(X, T + hh);\n        ctx.lineTo(X - hw, T);\n        ctx.fill();\n      }\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.033, last ? (now - last) / 1000 : 1 / 60);\n      last = now;\n      step(dt);\n      draw();\n      if (visible && !document.hidden && !reduce) raf = requestAnimationFrame(frame);\n      else last = 0;\n    };\n    const start = () => {\n      cancelAnimationFrame(raf);\n      raf = requestAnimationFrame(frame);\n    };\n    kick.current = start;\n\n    const local = (event: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      const s = r.width / root.offsetWidth || 1;\n      return { x: (event.clientX - r.left) / s, y: (event.clientY - r.top) / s };\n    };\n    const onMove = (event: PointerEvent) => {\n      const p = local(event);\n      hot.px = p.x;\n      hot.py = p.y;\n      hot.hover = true;\n      if (reduce) start();\n    };\n    const onLeave = (event: PointerEvent) => {\n      if (event.pointerType === \"touch\") return;\n      hot.hover = false;\n      if (reduce) start();\n    };\n    const onDown = (event: PointerEvent) => {\n      onMove(event);\n      if (reduce) return;\n      const p = local(event);\n      const g = gridAt(p.x, p.y);\n      ripples.push({ i: g.i, j: g.j, t: 0 });\n      if (ripples.length > 4) ripples.shift();\n    };\n    const ro = new ResizeObserver(() => {\n      resize();\n      start();\n    });\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) start();\n    });\n    io.observe(root);\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"pointerdown\", onDown);\n    resize();\n    start();\n    return () => {\n      cancelAnimationFrame(raf);\n      kick.current = () => {};\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"pointerdown\", onDown);\n    };\n  }, [reduce]);\n\n  return (\n    <div ref={rootRef} className={cn(\"relative isolate overflow-hidden\", className)} style={{ background, ...style }} {...props}>\n      <canvas ref={canvasRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 -z-10 h-full w-full\" />\n      {children}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}