Skip to content
dnd-grid

useDndGrid

Headless hook for building custom grid wrappers.

useDndGrid exposes the grid logic without rendering. Use it when you want a custom wrapper, extra markup around the grid, or to control how items render.

Example

import * as React from "react";
import { GridItem, type Layout, useDndGrid } from "@dnd-grid/react";
import "@dnd-grid/react/styles.css";

const initialLayout: Layout = [
  { id: "a", x: 0, y: 0, w: 2, h: 2 },
  { id: "b", x: 2, y: 0, w: 2, h: 2 },
];

export function HeadlessGrid() {
  const [layout, setLayout] = React.useState(initialLayout);
  const children = layout.map((item) => (
    <div key={item.id} className="card">
      {item.id.toUpperCase()}
    </div>
  ));

  const { gridProps, itemProps, liveRegionElement } = useDndGrid({
    layout,
    onLayoutChange: setLayout,
    width: 800,
    cols: 12,
    rowHeight: 40,
    children,
  });

  const droppingProps = itemProps.getDroppingItemProps();
  const placeholderProps = itemProps.getPlaceholderProps();

  return (
    <section>
      <div {...gridProps}>
        {liveRegionElement}
        {React.Children.map(children, (child) => {
          const props = itemProps.getItemProps(child);
          return props ? <GridItem {...props} /> : null;
        })}
        {droppingProps && <GridItem {...droppingProps} />}
        {placeholderProps && <GridItem {...placeholderProps} />}
      </div>
    </section>
  );
}

Options

OptionTypeDescription
layoutLayoutControlled layout array.
widthnumberRequired width in pixels.
childrenReactNodeItems to render; each child key must match layout.id.
onLayoutChange(layout) => voidCalled when the layout changes.

All other grid behavior props are supported (not the auto-width wrapper options). See the DndGrid props for the full list.

Returns

ValueDescription
gridPropsProps to spread onto the grid container, including ref, roles, and drop handlers.
itemPropsHelpers for items: getItemProps, getDroppingItemProps, getPlaceholderProps.
stateInternal grid state (layout, activeDrag, resizing, and more).
apiImperative API (measure, scroll, move, resize, commit, containerHeight).
liveRegionaria-live props + current message for announcing drag/resize events.
liveRegionElementReady-to-render live region element.