SpreadsheetGrid
Guides

Column drag

Grab the grip on a column header and move the column left or right (a new column array from onColumnsChange; carrying it to a pinned pane pins it)

Pass onColumnsChange, and a grip (⠿) appears when the cursor is over a column header; grab it and drag left or right to change the column order. The result comes back as a new column array through onColumnsChange, so you just replace columns (controlled).

Demo (columns of a product list)

Carry a column to the pinned ID pane on the left and a pin icon shows on the floating column
Rows 8 of 8Columns 6 of 6Sort: —
#
ID
1
P-001
2
P-002
3
P-003
4
P-004
5
P-005
6
P-006
7
P-007
8
P-008
Rows 8 of 8Columns 6 of 6
Cell —Range —0 cells / 0 rows0 columns

Current column order: ID(pinned left) → Product → Category → Unit price → Stock → Supplier

  • Put the cursor over a column header, press the grip (⠿) on the right, and drag left or right. The demo uses the display columnDragMotion="live": the grabbed column follows the pointer, and the columns it passes make room on the spot. Switch to the default 'ghost' (a vertical line at the insert position; on release the column slides to its new position) with "Display" at the top left.
  • "ID" is pinned left. Carrying a column into the left pinned pane pins it, and carrying one from a pinned pane to the center unpins it. With 'live', crossing panes brings a copy of the column (a floating column) along, and its header shows a pin icon (to a pinned pane) or a move arrow (to the center).

Usage

const [columns, setColumns] = useState(initialColumns);

<SpreadsheetGrid
  rows={rows}
  columns={columns}
  onColumnsChange={setColumns}   // passing this shows the grip and allows reordering
  columnDragMotion="live"        // 'ghost' (default) | 'live'
/>

The array passed to onColumnsChange is reordered by pinning (left pinned → center → right pinned), matching the column order on screen. Hidden columns stay in the array too.

Display (columnDragMotion)

  • 'ghost' (default): a ghost of the column name follows the pointer, and a vertical line shows the insert position. On release, the column slides to its new position (the original behavior). Releasing outside the grid's frame cancels.
  • 'live': the grabbed column itself (header + rendered cells) follows the pointer, and the columns it passes make room left or right on the spot. The drop target shows as a "gap", not a "line". Releasing outside the frame commits to the last position.
    • When carrying across pinned panes (pinning / unpinning), the real column can't leave its pane, so a copy of the column (a floating column) follows the pointer. The original pane closes up the space the column left, and the destination is shown with a vertical line. The floating column's header gets a pin icon when carrying to a pinned pane, and a move arrow when carrying to the center (the same as the 'ghost' ghost). The floating column stays inside the table's frame and slides into its new position on release (Esc returns it to where it was). Returning to the same pane goes back to the real column following.
  • With motion="off" (or 'auto' with the OS "reduce motion" setting on), it moves the same as 'ghost'.

Reordering rows has the same displays too (rowDragMotion; see row drag).

Key points of the behavior

  • When it's on — the grip appears only when you pass onColumnsChange (otherwise column range selection and so on work as before).
  • Column groups — with column groups (columnGroups), columns can move only within the same group. The handle on a group header moves the whole group (since 1.6.0).
  • Columns excluded — the auto group column, the detail toggle column and the row drag handle column are synthetic, so they have no grip.
  • Dropping into a pinned pane — releasing over the left or right pinned pane places the column at that position and pins it. With no pinned columns at all, a thin strip (32px) at the left or right edge of the grid is the drop target for the pinned pane.
  • Canceling — Escape ends without changing anything ('live' returns the column to where it was). The same happens when the window loses focus during a drag (switching to another app and so on). Releasing at the same position doesn't call onColumnsChange.
  • Auto-scrolling — approaching the left or right edge scrolls horizontally, so you can carry columns to positions off screen. The edges double as drop targets for pinned panes, so once the target position is visible, move back inward a little before releasing.
  • Styles — the insert line is .ssg-col-drop-indicator, the ghost is [data-grid-drag-ghost] (classNames.dragGhost), and the grabbed column's cells with 'live' are [data-ssg-col-dragging="live"]. The floating column when crossing panes with 'live' is .ssg-col-drag-float (a clip layer with the same rectangle as the table's frame; the copy inside inherits the look of [data-ssg-col-dragging="live"]), and the icon on its header is .ssg-col-drag-pin (colored with the same tokens as the ghost, --ssg-ghost-bg / --ssg-ghost-text).

On this page