Skip to main content

Getting Started

Dnd-Layout is a drag-and-drop layout system that automatically adjusts its positioning based on changes in item size.

Installation

npm install dnd-layout

Quick Start

import { createColumnLayoutAlgorithm, DndLayout, useLayout } from "dnd-layout";

export function GettingStartSample() {
const layout = useLayout(() => {
return {
algorithm: createColumnLayoutAlgorithm(),
initialItems: [
{ id: "1", column: 0, columnSpan: 1, height: 100 },
{ id: "2", column: 1, columnSpan: 1, height: 100 },
{ id: "3", column: 2, columnSpan: 1, height: 100 },
{ id: "4", column: 0, columnSpan: 1, height: 100 },
{ id: "5", column: 1, columnSpan: 1, height: 100 },
{ id: "6", column: 2, columnSpan: 1, height: 100 },
],
};
});
return (
<DndLayout
layout={layout}
itemRender={(item) => {
return (
<div
style={{
height: 100,
}}
>
{item.id}
</div>
);
}}
/>
);
}

API Reference

DndLayoutProps

PropertyDescriptionType
*layoutLayout store instance created by useLayout, used to update layout.ILayoutStore<T extends LayoutItem>
*itemRenderRenders a single item node.(item: T) => React.ReactNode
classNameClass name for the root container.string
styleStyle for the root container.React.CSSProperties
layoutConfigLayout render options (e.g. gap, fit container width/height to content).LayoutConfig
dragConfigDrag behavior config. Set enable=false to disable dragging; use draggableSelector as a drag handle filter.DragConfig
constraintsDrag constraints applied during movement to clamp/adjust positions.Constraint[]
placeholderRenderCustom placeholder renderer shown while dragging or external dropping.(item: T) => React.ReactNode
onLayoutChangeCalled after layout changes.(items: T[]) => void
onDragEnterCalled when external drag enters the container. Return a T to accept/insert a temporary item, or false to reject.(event: React.DragEvent, id: string) => T | false
onDropCalled when external drop happens. Return final T to confirm, or false to cancel and remove the temporary item.(event: React.DragEvent, item: T) => T | false