Row DnD Table
Reorder rows by dragging, using composable pieces.
Lets users reorder rows by drag and drop, using DataTableRowDndProvider and a drag-handle column. Use it for task lists, boards, and anything where order matters. Important: don’t combine row DnD with sorting or filtering. The user’s manual order fights the table’s computed order, so leave those add-ons out. Column resize is fine: add <DataTableColumnResize /> as in other tables, and keep enableResizing: false on the drag-handle and # columns.
Overview
Section titled “Overview”Preview with Controlled State
Virtualized Row DnD
Section titled “Virtualized Row DnD”For large datasets, use DataTableVirtualizedFlexHeader with DataTableVirtualizedDndBody. They combine row virtualization with drag and drop, so only visible rows render. The same <DataTableColumnResize /> marker works here. The grips appear on the flex header.
Introduction
Section titled “Introduction”Row DnD lets users reorder table rows by dragging. It is built with @dnd-kit and composable pieces that follow the shadcn/ui open-code pattern: copy the code and change it freely.
Installation
Section titled “Installation”Install the DataTable core and the row DnD add-on:
pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-row-dnd @niko-table/data-table-column-resize @niko-table/data-table-virtualized @niko-table/data-table-virtualized-row-dndFirst time using
@niko-table? See the Installation Guide to set up the registry.
For other add-ons or manual copy-paste, see the Installation Guide.
Prerequisites
Section titled “Prerequisites”We’ll build a task board with draggable rows:
type Task = { id: string title: string status: "todo" | "in-progress" | "done" | "cancelled" priority: "low" | "medium" | "high"}Basic Row DnD
Section titled “Basic Row DnD”Three components work together:
DataTableRowDndProvider: wraps the table with DnD context and sensorsDataTableDndBody: renders rows as draggable itemsDataTableRowDragHandle: a grip icon button to drag with
import { DataTableDndBody } from "@/components/niko-table/core/data-table-row-dnd-structure"
const [data, setData] = React.useState(initialData)
const columns: DataTableColumnDef<Task>[] = [ { id: "drag-handle", size: 40, header: () => null, cell: ({ row }) => <DataTableRowDragHandle rowId={row.id} />, enableSorting: false, enableHiding: false, enableResizing: false, }, // ... other columns]
return ( <DataTableRoot data={data} columns={columns} getRowId={(row) => row.id}> <DataTableColumnResize /> <DataTableRowDndProvider data={data} onReorder={setData}> <DataTable> <DataTableHeader /> <DataTableDndBody /> </DataTable> </DataTableRowDndProvider> </DataTableRoot>)Key Points
Section titled “Key Points”getRowIdis required. Use stable, unique IDs (for example, database IDs), not array indexes. Indexes break DnD after a reorder, because the index no longer matches the original item.DataTableRowDndProvidermust wrap outside<DataTable>. DnD context creates<div>elements, which can’t go inside<table>.onReorderreceives the new data array afterarrayMove. PasssetDatato it.
Drag Handle Column
Section titled “Drag Handle Column”The drag handle is its own column with DataTableRowDragHandle:
{ id: "drag-handle", size: 40, header: () => null, cell: ({ row }) => <DataTableRowDragHandle rowId={row.id} />, enableSorting: false, enableHiding: false,}Controlled State
Section titled “Controlled State”Track the data order outside the table:
import { DataTableDndBody } from "@/components/niko-table/core/data-table-row-dnd-structure"
const [data, setData] = React.useState(initialData)
// Reset to original orderconst resetData = () => setData(initialData)
return ( <DataTableRoot data={data} columns={columns} getRowId={(row) => row.id}> <DataTableColumnResize /> <DataTableRowDndProvider data={data} onReorder={setData}> <DataTable> <DataTableHeader /> <DataTableDndBody /> </DataTable> </DataTableRowDndProvider> </DataTableRoot>)Best Practices
Section titled “Best Practices”Don’t combine sorting or filtering with row DnD. They override the manual row order. If a user drags row 3 to position 1 and then a sort or filter runs, the reorder is lost.
- Avoid
DataTableColumnSortMenu,DataTableSearchFilter, andDataTableFacetedFilterin DnD tables - If you need search, filter the source data before you pass it to the table
- Column DnD is safe with sorting and filtering, because column order is independent of data order
When to Use
Section titled “When to Use”✅ Use Row DnD when:
- Users need to manually prioritize or reorder items
- You are building kanban boards, task lists, or playlist managers
- Order matters and should be saved
❌ Consider other options when:
- The data has a natural sort order (use sorting instead)
- The table is read-only and order doesn’t matter
Next Steps
Section titled “Next Steps”- Column DnD Table: drag-and-drop column reordering
- Row Selection Table: combine with row selection