Skip to content
SortableExamples

React — Swap two items

comins-sortable@0.1.6React

Use swap in React to exchange only two items and inspect the completion event.

Dropping A on the center of C in [A, B, C, D] produces [C, B, A, D]. Only the two items exchange positions; B and D remain. Intervening items are not shifted as they would be for insertion.

  1. Drag the A handle to the center of C and release.
  2. Check JSON for status: "dropped", change.operation: "swap", change.itemId: "A", and change.swapItemId: "C".
  3. Start another drag and cancel with Escape; order remains unchanged.
  4. Reset returns to A, B, C, D.
Option Value/purpose
swap true — exchanges source and target within the same area.
direction "vertical" — this example uses one vertical list.
itemKey or data-sortable-id Supplies identity retained across movement.
handle ".task-handle" — restricts pointer input to handles.

SortableRoot → SortableArea · items / onItemsChange

Commit the adapter’s next array to state immediately. Do not mutate the original array or use index-based keys. Check both the onAfterDrag completion result and the displayed order.

This is a same-area, single-item swap. Grid collision, multiDrag, threshold/invertSwap combinations, and cross-area delivery remain separate scenarios. This pointer-based example does not claim full keyboard reordering simply because its handle is focusable.

Advanced sorting guide · Core lifecycle · Vanilla instance · Ordinary list transfer