Skip to main content
The helpers package provides utility functions for common drag and drop operations, particularly for managing array and list mutations.

move

Moves items in an array or grouped record based on drag and drop events, intelligently handling both simple arrays and grouped data structures.

Parameters

Items | Record<UniqueIdentifier, Items>
required
The items to reorder. Can be:
  • An array of identifiers: UniqueIdentifier[]
  • An array of objects with id properties: {id: UniqueIdentifier}[]
  • A record of groups, each containing an array of items: Record<UniqueIdentifier, Items>
DragDropEvent
required
A dragover or dragend event from the DragDropManager containing source and target information

Returns

T
A new array or record with items moved to their new positions. Returns the original items unchanged if the operation is invalid or canceled.

Usage Example

Behavior

  • Simple Arrays: Moves the source item to the target position
  • Grouped Records: Handles both reordering within a group and moving items between groups
  • Optimistic Updates: Reconciles optimistic UI updates with actual data positions
  • Invalid Operations: Returns original items unchanged and calls event.preventDefault() if:
    • Source or target is missing
    • Operation was canceled
    • Indices are out of bounds
    • Source equals target with no actual movement

swap

Swaps two items in an array or grouped record based on drag and drop events.

Parameters

Items | Record<UniqueIdentifier, Items>
required
The items to reorder. Can be:
  • An array of identifiers: UniqueIdentifier[]
  • An array of objects with id properties: {id: UniqueIdentifier}[]
  • A record of groups, each containing an array of items: Record<UniqueIdentifier, Items>
DragDropEvent
required
A dragover or dragend event from the DragDropManager containing source and target information

Returns

T
A new array or record with items swapped. Returns the original items unchanged if the operation is invalid or canceled.

Usage Example

Difference from move

  • move: Shifts items between positions (inserting at target)
  • swap: Exchanges the items at source and target positions

arrayMove

Moves an array item to a different position. Returns a new array with the item moved to the new position.

Parameters

T extends any[]
required
The source array
number
required
The source index
number
required
The target index

Returns

T
A new array with the item moved. Returns the original array if from === to.

Usage Example


arraySwap

Swaps two items in an array. Returns a new array with the items swapped.

Parameters

T extends any[]
required
The source array
number
required
The first index
number
required
The second index

Returns

T
A new array with the items swapped. Returns the original array if from === to.

Usage Example


Types

UniqueIdentifier

Items

DragDropEvent

Best Practices

Using with dragover events

For real-time reordering during drag:

Using with dragend events

For committing changes only after drop:

Handling grouped data

Preventing unwanted mutations

The helper functions return new arrays/objects, so your original data remains unchanged: