Skip to content

useDrop & useDrag ​

A pair of hooks to help you manage data transfer between drag and drop

useDrop can be used alone to accept file, text or uri dropping.

useDrag should be used along with useDrop.

Paste into the drop area will also be treated as content drop.

Code demonstration ​

API ​

useDrag ​

typescript
useDrag<T>(
  data: any,
  target: (() => Element) | Element | MutableRefObject<Element>,
  options?: DragOptions
);

Params ​

PropertyDescriptionTypeDefault
dataDrag dataany-
targetDOM element or ref() => Element | Element | MutableRefObject<Element>-
optionsMore configDragOptions-

DragOptions ​

PropertyDescriptionTypeDefault
onDragStartOn drag start callback(e: DragEvent) => void-
onDragEndOn drag end callback(e: DragEvent) => void-

useDrop ​

typescript
useDrop<T>(
  target: (() => Element) | Element | MutableRefObject<Element>,
  options?: DropOptions
);

Params ​

参数DescriptionTypeDefault
targetDOM element or ref() => Element | Element | MutableRefObject<Element>-
optionsMore configDragOptions-

DropOptions ​

参数DescriptionTypeDefault
onTextThe callback when text is dropped or pasted(text: string, e: DragEvent) => void-
onFilesThe callback when file is dropped or pasted(files: File[], e: DragEvent) => void-
onUrlThe callback when uri is dropped or pasted(text: string, e: DragEvent) => void-
onDomThe callback when DOM is dropped or pasted(content: any, e: DragEvent) => void-
onDropThe callback when any is dropped(e: DragEvent) => void-
onPasteThe callback when any is pasted(e: DragEvent) => void-
onDragEnterOn drag enter callback(e: DragEvent) => void-
onDragOverOn drag over callback(e: DragEvent) => void-
onDragLeaveOn drag leave callback(e: DragEvent) => void-

Source ​

Source ¡ Document ¡ Demo

Released under the MIT License.