Headless wrapper
dnd-grid/headless-exampleFreeComponent
Build a custom wrapper with useDndGrid.
Install it
npx shadcn@latest add https://dnd-grid.com/r/headless-example.jsonSource
1"use client";23import {4 GridItem,5 type Layout,6 useContainerWidth,7 useDndGrid,8} from "@dnd-grid/react";9import { Children, type MutableRefObject, useCallback, useState } from "react";1011const initialLayout: Layout = [12 { id: "a", x: 0, y: 0, w: 3, h: 2 },13 { id: "b", x: 3, y: 0, w: 3, h: 2 },14 { id: "c", x: 6, y: 0, w: 3, h: 2 },15];1617export function HeadlessExample() {18 const [layout, setLayout] = useState(initialLayout);19 const { width, containerRef, mounted } = useContainerWidth({20 measureBeforeMount: true,21 });22 const children = layout.map((item) => (23 <div className="grid-item" key={item.id}>24 {item.id}25 </div>26 ));2728 const { gridProps, itemProps, liveRegionElement } = useDndGrid({29 layout,30 onLayoutChange: setLayout,31 cols: 12,32 rowHeight: 50,33 width,34 children,35 });3637 const { ref: gridRef, ...restGridProps } = gridProps;38 const setGridRef = useCallback(39 (node: HTMLDivElement | null) => {40 containerRef.current = node;41 if (typeof gridRef === "function") {42 gridRef(node);43 return;44 }45 if (gridRef) {46 (gridRef as MutableRefObject<HTMLDivElement | null>).current = node;47 }48 },49 [containerRef, gridRef]50 );5152 const droppingProps = itemProps.getDroppingItemProps();53 const placeholderProps = itemProps.getPlaceholderProps();5455 return (56 <div className="space-y-2">57 <div className="text-xs text-zinc-500">Custom wrapper</div>58 <div {...restGridProps} ref={setGridRef}>59 {mounted && (60 <>61 {liveRegionElement}62 {Children.map(children, (child) => {63 const props = itemProps.getItemProps(child);64 return props ? <GridItem {...props} /> : null;65 })}66 {droppingProps && <GridItem {...droppingProps} />}67 {placeholderProps && <GridItem {...placeholderProps} />}68 </>69 )}70 </div>71 </div>72 );73}
What it pulls in
npm packages
Files it writes
More from dnd-grid
All 19 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Allow overlapallow-overlap-example | dnd-grid | Components | Free | 3 deps | |
| Aspect ratio constraintsaspect-ratio-constraints-example | dnd-grid | Components | Free | 3 deps | |
| Basic examplebasic-example | dnd-grid | Components | Free | 3 deps | |
| Boundedbounded-example | dnd-grid | Components | Free | 3 deps | |
| Compactor showcasecompactor-showcase-example | dnd-grid | Components | Free | 3 deps | |
| Compositioncomposition-example | dnd-grid | Components | Free | 3 deps | |
| Constraintsconstraints-example | dnd-grid | Components | Free | 3 deps | |
| Drag from outsidedrag-from-outside-example | dnd-grid | Components | Free | 3 deps |
