{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tilt-blocks","type":"registry:ui","title":"Tilt Blocks","description":"A field of 3D cubes that tilt toward the cursor and flip in a glowing ripple when clicked.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/tilt-blocks.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface TiltBlocksProps {\n  /** Cubes per row and column. */\n  grid?: number;\n  /** Max tilt in degrees. */\n  maxAngle?: number;\n  /** Radius of the cursor's influence, in cells. */\n  radius?: number;\n  /** Gap between cubes in px. */\n  gap?: number;\n  /** Color the cubes flash as a ripple passes. */\n  rippleColor?: string;\n  /** Ripple travel speed multiplier. */\n  rippleSpeed?: number;\n  /** Wander and ripple on their own while the pointer is away. */\n  autoAnimate?: boolean;\n  className?: string;\n}\n\n// Face transform + a shade so the sides read darker than the front.\nconst FACES: [string, number][] = [\n  [\"translateZ(var(--half))\", 0],\n  [\"rotateY(180deg) translateZ(var(--half))\", 0.2],\n  [\"rotateY(90deg) translateZ(var(--half))\", 0.35],\n  [\"rotateY(-90deg) translateZ(var(--half))\", 0.35],\n  [\"rotateX(90deg) translateZ(var(--half))\", 0.15],\n  [\"rotateX(-90deg) translateZ(var(--half))\", 0.5],\n];\n\nexport function TiltBlocks({\n  grid = 8,\n  maxAngle = 45,\n  radius = 3,\n  gap = 8,\n  rippleColor = \"#c6ff3d\",\n  rippleSpeed = 1,\n  autoAnimate = true,\n  className,\n}: TiltBlocksProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n\n  useEffect(() => {\n    const root = rootRef.current!;\n    const cubes = Array.from(root.querySelectorAll<HTMLElement>(\"[data-cube]\"));\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const cur = cubes.map(() => ({ rx: 0, ry: 0, z: 0 }));\n    const ripples: { x: number; y: number; t0: number }[] = [];\n    const pointer = { x: 0, y: 0, in: false };\n    let raf = 0;\n    let visible = true;\n    let lastAuto = 0;\n\n    const ro = new ResizeObserver(() => {\n      const cell = cubes[0]?.parentElement?.offsetWidth ?? 0;\n      root.style.setProperty(\"--half\", `${cell / 2}px`);\n    });\n    ro.observe(root);\n\n    const loop = (now: number) => {\n      const t = now / 1000;\n      let px = pointer.x;\n      let py = pointer.y;\n      const auto = !pointer.in && autoAnimate && !reduced;\n      if (auto) {\n        px = grid / 2 + Math.sin(t * 0.6) * grid * 0.38;\n        py = grid / 2 + Math.cos(t * 0.83) * grid * 0.38;\n        if (now - lastAuto > 4200) {\n          lastAuto = now;\n          ripples.push({ x: px, y: py, t0: now });\n        }\n      }\n      const active = pointer.in || auto;\n      for (let i = ripples.length - 1; i >= 0; i--) if (now - ripples[i].t0 > 2600) ripples.splice(i, 1);\n\n      cubes.forEach((el, i) => {\n        const cx = (i % grid) + 0.5;\n        const cy = Math.floor(i / grid) + 0.5;\n        const dx = px - cx;\n        const dy = py - cy;\n        const infl = active ? Math.max(0, 1 - Math.hypot(dx, dy) / radius) : 0;\n        const e = infl * infl * (3 - 2 * infl);\n        const c = cur[i];\n        const k = reduced ? 1 : 0.14;\n        // Face the pointer: full tilt a cell away, easing to flat right under it.\n        const dist = Math.max(Math.hypot(dx, dy), 0.9);\n        c.rx += ((-dy / dist) * maxAngle * e - c.rx) * k;\n        c.ry += ((dx / dist) * maxAngle * e - c.ry) * k;\n        c.z += (e - c.z) * k;\n        let rx = c.rx;\n        let ry = c.ry;\n        let z = c.z;\n        let glow = c.z * 0.35;\n        for (const r of ripples) {\n          const age = (now - r.t0) / 1000;\n          const rdx = cx - r.x;\n          const rdy = cy - r.y;\n          const d = Math.hypot(rdx, rdy) || 1;\n          const front = age * 9 * rippleSpeed;\n          const w = Math.exp(-((d - front) ** 2) / 1.4) * Math.max(0, 1 - age / 2.4);\n          // Flip each cube around the axis perpendicular to the wave's travel.\n          rx += (-rdy / d) * w * 120;\n          ry += (rdx / d) * w * 120;\n          z += w * 0.9;\n          glow += w * 0.85;\n        }\n        el.style.transform = `translateZ(calc(var(--half) * ${z.toFixed(3)})) rotateX(${rx.toFixed(2)}deg) rotateY(${ry.toFixed(2)}deg)`;\n        el.style.setProperty(\"--glow\", `${Math.min(100, glow * 100).toFixed(1)}%`);\n      });\n      raf = visible ? requestAnimationFrame(loop) : 0;\n    };\n\n    const cellFromEvent = (e: PointerEvent | MouseEvent) => {\n      const r = root.getBoundingClientRect();\n      return { x: ((e.clientX - r.left) / r.width) * grid, y: ((e.clientY - r.top) / r.height) * grid };\n    };\n    const onMove = (e: PointerEvent) => {\n      Object.assign(pointer, cellFromEvent(e), { in: true });\n    };\n    const onLeave = () => {\n      pointer.in = false;\n    };\n    const onClick = (e: MouseEvent) => {\n      if (!reduced) ripples.push({ ...cellFromEvent(e), t0: performance.now() });\n    };\n\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf) raf = requestAnimationFrame(loop);\n    });\n    io.observe(root);\n    root.addEventListener(\"pointermove\", onMove, { passive: true });\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"click\", onClick);\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"click\", onClick);\n    };\n  }, [grid, maxAngle, radius, rippleSpeed, autoAnimate]);\n\n  return (\n    <div\n      ref={rootRef}\n      aria-hidden\n      className={className}\n      style={{\n        display: \"grid\",\n        gridTemplateColumns: `repeat(${grid}, 1fr)`,\n        gap,\n        aspectRatio: \"1\",\n        perspective: \"1000px\",\n        transformStyle: \"preserve-3d\",\n      }}\n    >\n      {Array.from({ length: grid * grid }, (_, i) => (\n        <div key={i} style={{ position: \"relative\", aspectRatio: \"1\", transformStyle: \"preserve-3d\" }}>\n          <div data-cube style={{ position: \"absolute\", inset: 0, transformStyle: \"preserve-3d\", willChange: \"transform\" }}>\n            {FACES.map(([transform, shade]) => (\n              <div\n                key={transform}\n                style={{\n                  position: \"absolute\",\n                  inset: 0,\n                  transform,\n                  border: `1px solid color-mix(in oklab, var(--border, rgba(255,255,255,.1)), ${rippleColor} var(--glow, 0%))`,\n                  background: `linear-gradient(145deg, rgba(255,255,255,.06), rgba(0,0,0,${shade + 0.1})), color-mix(in oklab, var(--card, #18181b), ${rippleColor} var(--glow, 0%))`,\n                  backfaceVisibility: \"hidden\",\n                }}\n              />\n            ))}\n          </div>\n        </div>\n      ))}\n    </div>\n  );\n}\n"}]}