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 } 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 function
  • makeDraggable(element, data, effectAllowed?) โ€” makes an element draggable with a data payload; returns a cleanup function
  • enableTextDrop(element, onDrop) โ€” makes an element a drop target for plain text; returns a cleanup function
  • disableDragAndDrop(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.