{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"squares-grid","type":"registry:ui","title":"Squares Grid","description":"An endlessly scrolling grid; the square under the cursor fills in.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/squares-grid.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface SquaresGridProps {\n  /** Px per square. */\n  squareSize?: number;\n  /** Px per frame; 0 holds the grid still. */\n  speed?: number;\n  direction?: \"diagonal\" | \"up\" | \"down\" | \"left\" | \"right\";\n  borderColor?: string;\n  hoverFill?: string;\n  className?: string;\n}\n\nconst vectors = {\n  diagonal: [-1, -1],\n  up: [0, -1],\n  down: [0, 1],\n  left: [-1, 0],\n  right: [1, 0],\n} as const;\n\nexport function SquaresGrid({\n  squareSize = 40,\n  speed = 0.5,\n  direction = \"diagonal\",\n  borderColor = \"#27272a\",\n  hoverFill = \"#1f2a12\",\n  className,\n}: SquaresGridProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const [vx, vy] = vectors[direction];\n    const moving = speed > 0 && !reduced;\n    const mouse = { x: -1, y: -1 };\n    let w = 0;\n    let h = 0;\n    let ox = 0;\n    let oy = 0;\n    let raf = 0;\n    let visible = true;\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.strokeStyle = borderColor;\n      ctx.lineWidth = 1;\n      const sx = ((ox % squareSize) + squareSize) % squareSize;\n      const sy = ((oy % squareSize) + squareSize) % squareSize;\n      for (let x = sx - squareSize; x < w; x += squareSize) {\n        for (let y = sy - squareSize; y < h; y += squareSize) {\n          if (mouse.x >= x && mouse.x < x + squareSize && mouse.y >= y && mouse.y < y + squareSize) {\n            ctx.fillStyle = hoverFill;\n            ctx.fillRect(x, y, squareSize, squareSize);\n          }\n          ctx.strokeRect(x + 0.5, y + 0.5, squareSize, squareSize);\n        }\n      }\n    };\n\n    const loop = () => {\n      ox += vx * speed;\n      oy += vy * speed;\n      draw();\n      raf = visible ? requestAnimationFrame(loop) : 0;\n    };\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\n      canvas.width = w * dpr;\n      canvas.height = h * dpr;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      draw();\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      mouse.x = e.clientX - r.left;\n      mouse.y = e.clientY - r.top;\n      if (!moving) draw();\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && moving && !raf) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    window.addEventListener(\"pointermove\", onMove, { passive: true });\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      window.removeEventListener(\"pointermove\", onMove);\n    };\n  }, [squareSize, speed, direction, borderColor, hoverFill]);\n\n  return (\n    <canvas\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\" }}\n    />\n  );\n}\n"}]}