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

Hooks

The headless layer. Each returns refs, styles, and state to spread onto your own elements. See Headless usage for the full picture.

useContainerWidth

Measures a container’s width with a ResizeObserver (SSR-safe). Replaces react-grid-layout’s WidthProvider.

const { width, mounted, containerRef } = useContainerWidth({ initialWidth: 1024 });
TypeDescription
options.initialWidthnumberWidth before measurement. Default 1280.
widthnumberMeasured width (or initialWidth before mount).
mountedbooleantrue once measured at least once.
containerRef(el: HTMLElement | null) => voidAttach to the element to measure.

useGridContainer

Registers the droppable surface and returns props to spread onto your container.

const { containerProps, isDropTarget } = useGridContainer(); return <div {...containerProps}>{/* items */}</div>;

containerProps has ref, a positioning style (relative + width + auto-sized height), and a data-drop-target attribute present while a compatible draggable is over the grid.

useGridItem

Wires a single tile: drag activation, positioning style, and drag state.

const { ref, style, isDragging, item } = useGridItem(id); return <div ref={ref} style={style}>{item?.i}</div>;
TypeDescription
ref(el: Element | null) => voidAttach to the tile element.
styleCSSPropertiesposition/left/top/width/height + reflow transition.
isDraggingbooleanTrue while this tile is the active drag source.
itemLayoutItem | undefinedThe tile’s current (possibly reflowed) entry.

useGridResizeHandle

Models a resize handle as its own draggable. Position and style it however you like.

const { ref, handleProps, isResizing } = useGridResizeHandle(itemId, "se"); return <span ref={ref} {...handleProps} />;

handleProps carries the marker attribute that excludes the handle from item-drag activation.

useGridPlaceholder

Returns where the landing-cell marker should render, or null when idle.

const placeholder = useGridPlaceholder(); {placeholder && <div style={placeholder.style} />}

useGridDragOverlay

Returns the floating preview’s item + fixed-position style for the source grid, else null. Render it in a portal at document.body (or use GridDragOverlay).

useResponsiveLayout

The headless engine behind ResponsiveGridLayout. Resolves the active breakpoint, columns, and layout from the container width.

const { breakpoint, cols, layout, onLayoutChange } = useResponsiveLayout({ width, layouts, onLayoutChange: (active, all) => setLayouts(all), onBreakpointChange: (bp, cols) => {}, });
OptionTypeDescription
widthnumberCurrent container width.
layoutsResponsiveLayoutsPer-breakpoint layout map.
breakpointsBreakpointsOptional; defaults to DEFAULT_BREAKPOINTS.
colsBreakpointColsOptional; defaults to DEFAULT_BREAKPOINT_COLS.
compactorCompactorUsed when generating a missing breakpoint’s layout.
onLayoutChange(layout, layouts) => voidActive layout + updated map.
onBreakpointChange(breakpoint, cols) => voidOn breakpoint change.

Returns the active breakpoint, its cols, the resolved layout, and an onLayoutChange to pass to the grid (it updates the active breakpoint’s entry).

Last updated on