React Aria and React Spectrum have a new home!
useDraggableCollectionState
Manages state for a draggable collection.
| install | yarn add react-stately |
|---|---|
| version | 3.42.0 |
| usage | import {useDraggableCollectionState} from 'react-stately' |
API#
useDraggableCollectionState<T = object>(
(props: DraggableCollectionStateOptions<T>
)): DraggableCollectionState
Interface#
Properties
| Name | Type | Description |
collection | Collection<Node<unknown>> | A collection of items. |
selectionManager | MultipleSelectionManager | An interface for reading and updating multiple selection state. |
draggedKey | Key | null | The key of the item that initiated a drag. |
draggingKeys | Set<Key> | The keys of the items that are currently being dragged. |
isDisabled | boolean | Whether drag events are disabled. |
preview | RefObject<DragPreviewRenderer | null> | The ref of the element that will be rendered as the drag preview while dragging. |
getAllowedDropOperations | () => DropOperation[] | Function that returns the drop operations that are allowed for the dragged items. If not provided, all drop operations are allowed. |
Methods
| Method | Description |
isDragging(
(key: Key
)): boolean | Returns whether the given key is currently being dragged. |
getKeysForDrag(
(key: Key
)): Set<Key> | Returns the keys of the items that will be dragged with the given key (e.g. selected items). |
getItems(
(key: Key
)): DragItem[] | Returns the items to drag for the given key. |
startDrag(
(key: Key,
, event: DragStartEvent
)): void | Begins a drag for the given key. This triggers the onDragStart event. |
moveDrag(
(event: DragMoveEvent
)): void | Triggers an onDragMove event. |
endDrag(
(event: DraggableCollectionEndEvent
)): void | Ends the current drag, and emits an onDragEnd event. |
Example#
See the docs for useDraggableCollection in react-aria for an example of useDraggableCollectionState.