draganddrop
Attaches drag-and-drop event listeners for file drops, text drops, and draggable elements using the native HTML Drag and Drop API.
Import: @rtorcato/browser-common/draganddrop
๐ MDN: HTML Drag and Drop API
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
DataTransfer | โ 3+ | โ 12+ | โ 3.5+ [1] | โ 4+ | โ 3.2+ | Widely available |
HTMLElement.draggable | โ 4+ | โ 12+ | โ 2+ | โ 5+ | โ 4+ | Widely available |
- Firefox: As of Firefox 52, the DataTransfer.types property returns a frozen array of DOMStrings as per spec, rather than a DOMStringList.
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: DataTransfer ยท HTMLElement.draggable
Exampleโ
import { enableFileDrop, makeDraggable } from '@rtorcato/browser-common/draganddrop'
const offDrop = enableFileDrop(dropzone, (files) => upload(files))
const offDrag = makeDraggable(card, 'card-42')
// later:
offDrop()
offDrag()
Exportsโ
enableFileDrop(element, onDrop)โ makes an element a drop target for files; returns a cleanup functionmakeDraggable(element, data, effectAllowed?)โ makes an element draggable with a data payload; returns a cleanup functionenableTextDrop(element, onDrop)โ makes an element a drop target for plain text; returns a cleanup functiondisableDragAndDrop(element)โ deprecated: replaces the element with a clone, stripping every listener on it. Call the returned cleanup functions instead
See the API reference for full signatures.