Row drag
Grab the handle with enableRowDrag to move rows up and down (a new array through onRowsChange, undoable)
With enableRowDrag, grab the handle column (⋮⋮) at the start and move rows up and down (like AG Grid's managed row dragging). The result comes back as a new array through onRowsChange, like a normal edit, so your app only replaces rows. It goes through the history wrapper, so Ctrl/Cmd+Z undoes it.
Demo (task priorities)
Current order: T-101 → T-102 → T-103 → T-104 → T-105 → T-106 → T-107 → T-108
- Press the handle (⋮⋮) at the start of each row and drag up or down. The demo uses
rowDragMotion="live": the grabbed row follows the pointer, and the rows it passes make room on the spot. "Style" at the top left switches to the default'ghost'(a horizontal guide line at the insert position; on release the affected rows slide into their new places). - Rows whose status is "Locked" have no handle through
isRowDraggable(their order is fixed). - Turn on "canDropRow", and rows can't be dropped above the "Locked" rows (no guide line appears where dropping isn't allowed, and releasing there moves nothing. Since 1.2.0).
- Sorting by a column header or filtering the status column makes the handles faint and unusable (the tooltip shows why). Clearing it brings them back.
- "Move T-108 to the top" is the imperative handle's
moveRow(), and "Undo" isundo(). Both callonRowMove.
Usage
const [rows, setRows] = useState(initialRows);
<SpreadsheetGrid
rows={rows}
onRowsChange={setRows}
columns={columns}
rowKeyGetter={(row) => row.id}
enableRowDrag
isRowDraggable={(row) => !row.locked} // per-row permission (all rows if omitted)
onRowMove={({ rowKey, fromIndex, toIndex, rows }) => {
saveOrder(rows); // notified after the move (right after onRowsChange)
}}
/>The params passed to onRowMove are { rowKey, fromIndex, toIndex, rows }. fromIndex / toIndex are indexes in the original rows array, and rows is the same new array reference passed to onRowsChange.
Deciding where rows can be dropped (canDropRow)
To move rows only within a group of label rows (categories) and the like, return false from canDropRow for positions where dropping isn't allowed (since 1.2.0).
// move only within the section of a label row
const sectionOf = useMemo(() => {
const map = new Map<string, string>();
let section = '';
for (const row of rows) {
if (isLabel(row)) section = row.id;
else map.set(row.id, section);
}
return map;
}, [rows]);
<SpreadsheetGrid
enableRowDrag
canDropRow={({ row, rowAbove }) =>
rowAbove !== undefined && (isLabel(rowAbove) ? rowAbove.id : sectionOf.get(rowAbove.id)) === sectionOf.get(row.id)
}
...
/>paramsare{ row, rowKey, fromIndex, toIndex, rowAbove, rowBelow, rows }.fromIndex/toIndexare the same asonRowMove;rowAbove/rowBeloware the rows that would be right above / below the grabbed row after the drop (undefinedat the top / bottom);rowsis the array before the move.- At positions returning
false, no guide line is shown (the ghost looks faint, as outside the frame), and releasing moves nothing. WithrowDragMotion="live", the row stays at the last allowed position, and releasing there moves it there. - It's called every time the drop position changes, so do heavy computation ahead of time with
useMemo(sectionOfin the example above). - It doesn't apply to the imperative
moveRow()(moves decided by your app's code are done as they are).
Drag style (rowDragMotion)
<SpreadsheetGrid enableRowDrag rowDragMotion="live" ... /> // 'ghost' (default) | 'live''ghost'(default): a ghost label follows the pointer, and a horizontal guide line shows the insert position. On release, the affected rows slide into their new places (the original behavior).'live': the grabbed row itself follows the pointer, and the rows it passes move up or down on the spot. The drop target shows as a "gap" rather than a "line", which reduces off-by-one mistakes. Releasing outside the frame still commits to the last position, andEscreturns it to its original place. Withmotion="off", it behaves like'ghost'.- Column reordering has the same styles (
columnDragMotion; see Column drag).
Moving from code (moveRow)
gridRef.current?.moveRow('T-108', 0); // move row key 'T-108' to the topmoveRow(rowKey, toIndex) moves in the original array regardless of the display state (sorting / filtering) and calls onRowsChange → onRowMove in that order (canDropRow doesn't apply). Unknown keys / the same position / out of range do nothing.
Key points
- clientSide only — it works with
rows+onRowsChange. WithdataSource(serverSide), during row grouping, or withoutonRowsChange, the handle column isn't inserted.readOnlydoesn't matter (reordering isn't a cell edit). To stop reordering when read-only too, passenableRowDrag={!readOnly}. - Disabled while sorted / filtered — the display order wouldn't match the order of
rows, so the handles become faint with a tooltip giving the reason. The column stays, so the layout doesn't jump. - The handle column is a synthetic column — 28px wide with no title; when there are left-pinned columns it goes on the left-pinned side, even before the detail toggle column. It's excluded from the column menu / sorting / column drag / autoSize / export.
- Cancelling —
Escape, or the window losing focus during a drag, cancels (nothing changes). Releasing outside the grid's frame depends on the style:'ghost'cancels,'live'commits to the last visible position. Releasing right above / below the grabbed row (a position where it wouldn't move) calls nothing. - Auto-scroll — it scrolls near the top and bottom edges, so rows can be carried to virtualized rows off screen.
- With detail rows — an expanded master row moves together with its detail panel (with
rowDragMotion="live"too, the grabbed row's panel follows the pointer, and panels of rows making room move with them. When dragging down, the drop position is decided by the bottom edge of the panel, so the opening gap overlaps the grabbed row + panel). Over a panel counts as "below the master row". - Look — a guide line (insert position) + a slide after dropping (instant with
prefers-reduced-motion). The same experience as column reordering. To have surrounding rows make room on the spot during the drag, chooserowDragMotion="live"("Drag style" above). - Styles —
.ssg-row-drag-handle(--disabledwhen disabled) /.ssg-row-drop-indicator/ the row being dragged.ssg-body-row[data-ssg-row-dragging]. Colors can be changed with the--ssg-drop-indicator/--ssg-glyph-*/--ssg-ghost-*tokens.
See the API reference for moveRow, and props for all props.