Skip to content

Drag and Drop Hooks

Hooks for managing drag and drop state, built on @dnd-kit.


useDragAndDropList

Manages drag and drop within a single list.

Signature

ts
function useDragAndDropList<T>(
  options: UseDragAndDropListOptions<T>
): UseDragAndDropListResult<T>

UseDragAndDropListOptions<T>

PropertyTypeRequiredDescription
itemsT[]YesArray of items
getItemId(item: T) => UniqueIdentifierYesID extractor
onReorder(items: T[], activeId, overId) => voidNoReorder callback

UseDragAndDropListResult<T>

PropertyTypeDescription
itemsT[]Current items
itemIdsUniqueIdentifier[]Item IDs
activeItemT | nullCurrently dragged item
handleDragStart(id) => voidDrag start handler
handleDragEnd(activeId, overId) => voidDrag end handler
handleDragCancel() => voidCancel handler
reorderItems(activeId, overId) => T[]Get reordered array

Example

tsx
const { items, activeItem, handleDragStart, handleDragEnd } = useDragAndDropList({
  items: tasks,
  getItemId: (task) => task.id,
  onReorder: (reordered) => setTasks(reordered),
})

useDragAndDropMultiList

Manages drag and drop across multiple lists (Kanban-style).

Signature

ts
function useDragAndDropMultiList<T, L>(
  options: UseDragAndDropMultiListOptions<T, L>
): UseDragAndDropMultiListResult<T, L>

UseDragAndDropMultiListOptions<T, L>

PropertyTypeRequiredDescription
listsL[]YesArray of lists
getListId(list: L) => UniqueIdentifierYesList ID extractor
getListItems(list: L) => T[]YesGet items from list
getItemId(item: T) => UniqueIdentifierYesItem ID extractor
onMoveWithinList(listId, activeId, overId, newIndex) => voidNoSame-list move
onMoveBetweenLists(sourceId, targetId, itemId, newIndex) => voidNoCross-list move
onReorderLists(activeId, overId) => voidNoList reorder

UseDragAndDropMultiListResult<T, L>

PropertyTypeDescription
listsL[]Current lists
listIdsUniqueIdentifier[]List IDs
activeItemT | nullCurrently dragged item
activeListL | nullCurrently dragged list
activeType'item' | 'list' | nullType being dragged
handleDragStart(id, type) => voidDrag start handler
handleDragOver(activeId, overId) => voidDrag over handler
handleDragEnd(activeId, overId) => voidDrag end handler
handleDragCancel() => voidCancel handler
findItem(id) => { item, list } | nullFind item by ID
findList(id) => L | nullFind list by ID

Utility Functions

reorderArray

ts
function reorderArray<T>(array: T[], fromIndex: number, toIndex: number): T[]

moveItemBetweenArrays

ts
function moveItemBetweenArrays<T>(
  sourceArray: T[],
  targetArray: T[],
  sourceIndex: number,
  targetIndex: number
): { source: T[]; target: T[] }

See also