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, disableDragAndDrop } from '@rtorcato/browser-common/draganddrop'
enableFileDrop(dropzone, (files) => upload(files))
makeDraggable(card, 'card-42')
// later:
disableDragAndDrop(dropzone)
Exportsโ
enableFileDrop(element, onDrop)โ makes an element a drop target for filesmakeDraggable(element, data, effectAllowed?)โ makes an element draggable with a data payloadenableTextDrop(element, onDrop)โ makes an element a drop target for plain textdisableDragAndDrop(element)โ removes drag-and-drop listeners by cloning and replacing the element
See the API reference for full signatures.