Skip to main content

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โ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
DataTransferโœ… 3+โœ… 12+โœ… 3.5+ [1]โœ… 4+โœ… 3.2+Widely available
HTMLElement.draggableโœ… 4+โœ… 12+โœ… 2+โœ… 5+โœ… 4+Widely available
  1. 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 files
  • makeDraggable(element, data, effectAllowed?) โ€” makes an element draggable with a data payload
  • enableTextDrop(element, onDrop) โ€” makes an element a drop target for plain text
  • disableDragAndDrop(element) โ€” removes drag-and-drop listeners by cloning and replacing the element

See the API reference for full signatures.