- Spec files
npm-react-aria
Describes: pkg:npm/react-aria@3.43.x
- Description
- Comprehensive library of unstyled React hooks providing accessible UI primitives with full WAI-ARIA compliance and internationalization support.
- Author
- tessl
- Last updated
drag-drop.md docs/
1# Drag and Drop23Comprehensive drag and drop system for building interactive interfaces with accessible keyboard navigation, screen reader support, and cross-platform compatibility. Supports both individual draggable items and full collection-based drag and drop operations.45## Capabilities67### Individual Drag and Drop89Basic drag and drop functionality for individual elements.1011```typescript { .api }12/**13* Provides drag behavior for an element14* @param options - Drag configuration options15* @returns Drag result with props and state16*/17function useDrag(options: DragOptions): DragResult;1819/**20* Provides drop behavior for an element21* @param options - Drop configuration options22* @returns Drop result with props and state23*/24function useDrop(options: DropOptions): DropResult;2526interface DragOptions {27/** Handler called when drag starts */28onDragStart?: (e: DragStartEvent) => void;29/** Handler called when drag ends */30onDragEnd?: (e: DragEndEvent) => void;31/** Data to transfer during drag operation */32getItems: () => DragItem[];33/** Preview element or render function */34preview?: DragPreview;35/** Whether drag is allowed */36isDisabled?: boolean;37}3839interface DropOptions {40/** Handler called when items are dropped */41onDrop?: (e: DropEvent) => void;42/** Handler called when drag enters the drop zone */43onDragEnter?: (e: DragEnterEvent) => void;44/** Handler called when drag leaves the drop zone */45onDragLeave?: (e: DragLeaveEvent) => void;46/** Function to determine if drop is allowed */47shouldAcceptDrop?: (types: Set<string>) => boolean;48/** Whether drop is allowed */49isDisabled?: boolean;50}5152interface DragResult {53/** Props to spread on the draggable element */54dragProps: DOMAttributes<Element>;55/** Whether the element is currently being dragged */56isDragging: boolean;57}5859interface DropResult {60/** Props to spread on the drop target element */61dropProps: DOMAttributes<Element>;62/** Whether a drag is currently over the drop zone */63isDropTarget: boolean;64}65```6667### Collection Drag and Drop6869Advanced drag and drop for collections like lists and grids with reordering and multi-selection support.7071```typescript { .api }72/**73* Provides drag and drop behavior for collections74* @param props - Draggable collection configuration75* @returns Collection drag utilities76*/77function useDraggableCollection(props: DraggableCollectionProps): DraggableCollectionResult;7879/**80* Provides drop behavior for collections81* @param props - Droppable collection configuration82* @returns Collection drop utilities83*/84function useDroppableCollection(props: DroppableCollectionProps): DroppableCollectionResult;8586interface DraggableCollectionProps {87/** Selection manager for the collection */88selectionManager: SelectionManager;89/** Handler called when drag starts */90onDragStart?: (e: DragStartEvent) => void;91/** Handler called when drag ends */92onDragEnd?: (e: DragEndEvent) => void;93/** Function to get drag data for items */94getItems: (keys: Set<Key>) => DragItem[];95/** Preview configuration */96preview?: DragPreview;97}9899interface DroppableCollectionProps {100/** Collection state */101collection: Collection<any>;102/** Selection manager */103selectionManager: SelectionManager;104/** Handler for drop operations */105onDrop?: (e: DropEvent) => void;106/** Handler for reorder operations */107onReorder?: (e: ReorderEvent) => void;108/** Function to determine accepted drop types */109shouldAcceptDrop?: (target: DropTarget, types: Set<string>) => boolean;110}111```112113### Item-Level Drag and Drop114115Hooks for individual items within draggable/droppable collections.116117```typescript { .api }118/**119* Provides drag behavior for individual collection items120* @param props - Draggable item configuration121* @returns Item drag utilities122*/123function useDraggableItem(props: DraggableItemProps): DraggableItemResult;124125/**126* Provides drop behavior for individual collection items127* @param props - Droppable item configuration128* @returns Item drop utilities129*/130function useDroppableItem(props: DroppableItemProps): DroppableItemResult;131132/**133* Provides drop indicator for showing drop positions134* @param props - Drop indicator configuration135* @returns Drop indicator utilities136*/137function useDropIndicator(props: DropIndicatorProps): DropIndicatorResult;138139interface DraggableItemProps {140/** Item key */141key: Key;142/** Whether dragging is disabled for this item */143isDisabled?: boolean;144/** Whether the item has a drag handle */145hasDragHandle?: boolean;146}147148interface DroppableItemProps {149/** Item key */150key: Key;151/** Drop target configuration */152target: DropTarget;153/** Whether dropping is disabled for this item */154isDisabled?: boolean;155}156157interface DropIndicatorProps {158/** Drop target for the indicator */159target: DropTarget;160/** Whether the indicator is disabled */161isDisabled?: boolean;162}163```164165### Clipboard Integration166167Utilities for clipboard operations integrated with drag and drop.168169```typescript { .api }170/**171* Provides clipboard functionality with drag and drop integration172* @param options - Clipboard configuration173* @returns Clipboard utilities174*/175function useClipboard(options: ClipboardOptions): ClipboardResult;176177interface ClipboardOptions {178/** Handler called when items are copied */179onCopy?: (items: ClipboardItem[]) => void;180/** Handler called when items are cut */181onCut?: (items: ClipboardItem[]) => void;182/** Handler called when items are pasted */183onPaste?: (items: ClipboardItem[]) => void;184/** Function to get clipboard data */185getItems?: () => ClipboardItem[];186}187188interface ClipboardResult {189/** Copy selected items to clipboard */190copy: () => void;191/** Cut selected items to clipboard */192cut: () => void;193/** Paste items from clipboard */194paste: () => void;195/** Whether clipboard operations are available */196isSupported: boolean;197}198```199200### Drag Preview Component201202Component for customizing drag preview appearance.203204```typescript { .api }205/**206* Component for rendering custom drag previews207*/208function DragPreview(props: DragPreviewProps): JSX.Element;209210interface DragPreviewProps {211/** Preview content to render */212children: ReactNode;213/** Offset from cursor position */214offset?: { x: number; y: number };215/** Whether to use default browser preview */216useDefaultPreview?: boolean;217}218```219220## Types221222```typescript { .api }223interface DragItem {224/** Drag data type */225type: string;226/** Item data */227data: any;228/** Text representation */229textValue?: string;230}231232interface DragStartEvent {233/** Items being dragged */234items: DragItem[];235/** Pointer type */236pointerType: 'mouse' | 'touch' | 'pen' | 'keyboard';237}238239interface DragEndEvent {240/** Whether the drop was successful */241isSuccessful: boolean;242/** Operation performed */243operation: 'move' | 'copy' | 'link' | 'cancel';244}245246interface DropEvent {247/** Items being dropped */248items: DropItem[];249/** Drop target */250target: DropTarget;251/** Drop operation */252operation: 'move' | 'copy' | 'link';253}254255interface DropTarget {256/** Target type */257type: 'item' | 'collection' | 'root';258/** Target key (for item targets) */259key?: Key;260/** Drop position relative to target */261dropPosition?: 'before' | 'after' | 'on';262}263264interface ReorderEvent {265/** Keys of items being reordered */266keys: Set<Key>;267/** Target drop position */268target: DropTarget;269}270271type DIRECTORY_DRAG_TYPE = 'application/vnd.react-aria.directory';272273function isDirectoryDropItem(item: DropItem): boolean;274function isFileDropItem(item: DropItem): boolean;275function isTextDropItem(item: DropItem): boolean;276```277278**Usage Example:**279280```typescript281import { useDrag, useDrop } from "react-aria";282283function DraggableItem({ item }) {284let { dragProps, isDragging } = useDrag({285getItems: () => [{ type: 'text/plain', data: item.text }],286onDragEnd: (e) => console.log('Drag ended:', e.isSuccessful)287});288289return (290<div291{...dragProps}292className={`draggable-item ${isDragging ? 'dragging' : ''}`}293>294{item.text}295</div>296);297}298299function DropZone({ onDrop }) {300let { dropProps, isDropTarget } = useDrop({301onDrop: (e) => onDrop(e.items),302shouldAcceptDrop: (types) => types.has('text/plain')303});304305return (306<div307{...dropProps}308className={`drop-zone ${isDropTarget ? 'drop-target' : ''}`}309>310Drop items here311</div>312);313}314```