cnippet/ui
  1. ui
  2. data
  3. sortable

data

Sortable

Drag-and-drop reordering for lists, rows, grids and Kanban boards, with keyboard support, built on dnd-kit. Copy-paste ready.

API Reference

Installation

Usage

Anatomy

Good to know

  • Sortable doesn't render any element, so wrap the items in your own list, row or grid and match orientation to it: "vertical", "horizontal" or "grid".
  • getItemValue must return a unique, stable id. Don't use the array index; it changes as items move.
  • Keyboard users focus a handle (or an asHandle item), press Space to pick it up, move it with the arrow keys, and press Space again to drop it or Escape to cancel. dnd-kit announces each step to screen readers.
  • A drag only starts after the pointer moves 4px, so clicks on buttons and checkboxes inside an item still work.
  • SortableItem and SortableItemHandle work under any dnd-kit SortableContext. For drags between lists, like the Kanban example, use your own DndContext with useSortableSensors() and one SortableContext per list.

API Reference

Sortable

SortableItem

Renders a <div>; other props go on it.

SortableItemHandle

A <button> that starts the drag, with a grip icon unless you pass children. Its label is "Drag to reorder".

Helpers

useSortableSensors() returns the pointer and keyboard sensors Sortable uses, for your own DndContext. arrayMove is re-exported from dnd-kit.

Examples

Ranking

The whole row is the handle, and the rank numbers update as items move.

Horizontal

Reorder shortcut tabs in a row.

Grid

Rearrange dashboard cards in a responsive grid by their handles.

Kanban

Move cards within and between columns, including into an empty one.