Skip to Content
snapgrid is a react-grid-layout v2 alternative built on dnd-kit. Drag, resize, repack, and drag between grids.
DocumentationAPI ReferenceCore engine & extras

Core engine & extras

@snapgridjs/core

The framework-agnostic layout engine. @snapgridjs/react is a thin binding over it, so you rarely import it directly. Reach for it when integrating snapgrid into a non-React renderer, building custom tooling, or testing layout logic in isolation.

@snapgridjs/core is a thin adapter over react-grid-layout/core (re-exported) plus a small set of original helpers that bridge to the dnd-kit interaction layer. The RGL dependency lives behind one seam, so a future engine change touches a single file.

Layout operations

Pure functions that take a layout and return the next one (the caller’s array is never mutated):

FunctionDescription
moveItemWithCompactor(layout, item, x, y, opts)Move an item to (x, y) and re-pack.
resizeItemWithCompactor(layout, item, next, opts)Apply a new position/size and re-pack.
removeItemWithCompactor(layout, id, opts)Remove an item and re-pack the remainder.
insertItemWithCompactor(layout, item, x, y, opts)Insert (or move) an item at (x, y).

opts is { compactor, cols } (move also accepts isUserAction).

Drag session

The state machine the React layer drives during an interaction:

beginDrag, beginResize, beginReceive, dragTo, dragResize, commitLayout, stripMoved, and the DragSession / DragAnchor / ResizeAnchor / DragContext types.

Geometry & helpers

toPositionParams(grid, width) builds the PositionParams the geometry helpers expect. The re-exported RGL helpers include calcGridItemPosition, calcXY, calcWH, calcGridColWidth, clamp, bottom, cloneLayout, cloneLayoutItem, getAllCollisions, collides, correctBounds, sortLayoutItemsByRowCol, getCompactor, and the responsive helpers getBreakpointFromWidth, getColsFromBreakpoint, findOrGenerateResponsiveLayout.

@snapgridjs/extras

Extra packing styles, each a Compactor you pass to a grid’s compactor prop.

Original packers

ExportBehaviour
gravityCompactorTop-left gravity — earliest free hole, row-major.
masonryCompactorMinimizes height via shortest-column placement.
shelfCompactorLeft-to-right rows, wrapping to a new shelf when full.

The underlying functions gravityCompact, masonryCompact, and shelfCompact (layout, cols) are also exported if you want to call them directly.

Re-exported from react-grid-layout

ExportBehaviour
wrapCompactorText-flow wrap — best for uniform-height items.
wrapOverlapCompactorWrap, allowing overlap.
fastVerticalCompactor / fastHorizontalCompactorO(n log n) built-in packing for large layouts.
fastVerticalOverlapCompactor / fastHorizontalOverlapCompactorFast variants that allow overlap.

The original packers (masonry/gravity/shelf) repack all items and do not preserve static or enforce maxRows. Use a built-in compactor when you need either. See Compaction.

Last updated on