Snap to grid
Drag a block anywhere; it springs into the nearest cell.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add snap-gridOr install the package
Don’t want the file? Import from
asta-ui/snap-gridand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { SnapGrid } from "asta-ui/snap-grid";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useRef, useState } from "react"; const CELL = 44; /** Drag the block anywhere; on release it springs to the nearest cell. The grid only shows while you drag. */ export default function SnapGrid() { const [pos, setPos] = useState({ x: CELL * 2, y: CELL * 1 }); const [dragging, setDragging] = useState(false); const area = useRef<HTMLDivElement>(null); const off = useRef({ x: 0, y: 0 }); // The area's box, measured once per drag, so pointermove never reads layout. const box = useRef<DOMRect | null>(null); const down = (e: React.PointerEvent) => { e.currentTarget.setPointerCapture(e.pointerId); box.current = area.current!.getBoundingClientRect(); off.current = { x: e.clientX - pos.x, y: e.clientY - pos.y }; setDragging(true); }; const move = (e: React.PointerEvent) => { if (!dragging) return; const r = (box.current ??= area.current!.getBoundingClientRect()); setPos({ x: Math.max(0, Math.min(r.width - CELL, e.clientX - off.current.x)), y: Math.max(0, Math.min(r.height - CELL, e.clientY - off.current.y)), }); }; const up = () => { if (!dragging) return; setDragging(false); const r = (box.current ??= area.current!.getBoundingClientRect()); const maxX = Math.floor(r.width / CELL) - 1; const maxY = Math.floor(r.height / CELL) - 1; setPos((p) => ({ x: Math.min(maxX, Math.round(p.x / CELL)) * CELL, y: Math.min(maxY, Math.round(p.y / CELL)) * CELL, })); navigator.vibrate?.(8); }; const cx = Math.round(pos.x / CELL); const cy = Math.round(pos.y / CELL); return ( <div className="flex h-full w-full flex-col items-center justify-center gap-3 p-5"> <div ref={area} className="relative h-[176px] w-[264px] border border-line" style={{ backgroundImage: "linear-gradient(color-mix(in srgb, var(--color-ink) 8%, transparent) 1px, transparent 1px), linear-gradient(90deg, color-mix(in srgb, var(--color-ink) 8%, transparent) 1px, transparent 1px)", backgroundSize: `${CELL}px ${CELL}px`, backgroundPosition: "-1px -1px", backgroundColor: "transparent", transition: "opacity .3s", }} > <div className={`pointer-events-none absolute inset-0 bg-paper transition-opacity duration-300 ${dragging ? "opacity-0" : "opacity-100"}`} /> <div className="pointer-events-none absolute left-0 top-0 border border-dashed border-signal/60 transition-[transform,opacity] duration-150" style={{ width: CELL, height: CELL, transform: `translate(${cx * CELL}px, ${cy * CELL}px)`, opacity: dragging ? 1 : 0 }} /> <div onPointerDown={down} onPointerMove={move} onPointerUp={up} onPointerCancel={up} className={`absolute left-0 top-0 grid cursor-grab place-items-center bg-signal text-paper [touch-action:none] active:cursor-grabbing ${dragging ? "scale-110 shadow-[0_10px_30px_color-mix(in_srgb,var(--color-signal)_25%,transparent)]" : "transition-transform duration-500 ease-[cubic-bezier(.5,1.8,.4,.8)]"}`} style={{ transform: `translate(${pos.x}px, ${pos.y}px)`, width: CELL, height: CELL }} > <span className="font-mono text-[11px] uppercase tracking-[0.08em]">AB</span> </div> </div> <span className="font-mono text-[11px] uppercase tracking-[0.08em] tabular-nums text-mute"> X {String(cx).padStart(2, "0")} · Y {String(cy).padStart(2, "0")} </span> </div> ); }Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Snap to grid component to my project. Drag a block anywhere; it springs into the nearest cell. It fills its parent, so give the wrapper a size. It is designed on a dark surface: put it inside an element with the `dark` class. Install it: ```sh pnpm dlx asta-ui@latest add snap-grid --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/snap-grid.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import SnapGrid from "@/components/astabyte/snap-grid"; ``` Usage example and props: https://ui.astabyte.com/snap-grid Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]
Usage
It fills its parent. Give the wrapper a size.
import SnapGrid from "@/components/astabyte/snap-grid";
export default function Page() {
// Snap to grid
return (
<div className="relative h-96 w-full">
<SnapGrid />
</div>
);
}