HTML5 drag and drop backend for React DnD, enabling sophisticated drag-and-drop interactions using the native HTML5 Drag and Drop API
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Support for native HTML5 drag source types including files, URLs, text, and HTML content. These constants enable React DnD components to handle drag operations from external sources like the operating system or other applications.
Constants representing the four native HTML5 drag source types supported by the backend.
/**
* Native HTML5 drag source type constants
* These are used to identify different types of external drag sources
*/
namespace NativeTypes {
/** Constant for file drag operations from the file system */
const FILE: "__NATIVE_FILE__";
/** Constant for URL/link drag operations from browsers or applications */
const URL: "__NATIVE_URL__";
/** Constant for text drag operations */
const TEXT: "__NATIVE_TEXT__";
/** Constant for HTML content drag operations */
const HTML: "__NATIVE_HTML__";
}Usage Examples:
import { useDrop } from "react-dnd";
import { NativeTypes } from "react-dnd-html5-backend";
// Accept file drops
function FileDropZone() {
const [{ isOver }, drop] = useDrop({
accept: NativeTypes.FILE,
drop: (item: { files: File[] }) => {
console.log("Files dropped:", item.files);
},
collect: (monitor) => ({
isOver: monitor.isOver(),
}),
});
return (
<div ref={drop} style={{ background: isOver ? "lightblue" : "white" }}>
Drop files here
</div>
);
}
// Accept URL drops
function URLDropZone() {
const [{ isOver }, drop] = useDrop({
accept: NativeTypes.URL,
drop: (item: { urls: string[] }) => {
console.log("URLs dropped:", item.urls);
},
collect: (monitor) => ({
isOver: monitor.isOver(),
}),
});
return (
<div ref={drop} style={{ background: isOver ? "lightgreen" : "white" }}>
Drop URLs here
</div>
);
}
// Accept text drops
function TextDropZone() {
const [{ isOver }, drop] = useDrop({
accept: NativeTypes.TEXT,
drop: (item: { text: string }) => {
console.log("Text dropped:", item.text);
},
collect: (monitor) => ({
isOver: monitor.isOver(),
}),
});
return (
<div ref={drop} style={{ background: isOver ? "lightyellow" : "white" }}>
Drop text here
</div>
);
}
// Accept multiple native types
function MultiTypeDropZone() {
const [{ isOver, canDrop }, drop] = useDrop({
accept: [NativeTypes.FILE, NativeTypes.URL, NativeTypes.TEXT, NativeTypes.HTML],
drop: (item: any, monitor) => {
const itemType = monitor.getItemType();
switch (itemType) {
case NativeTypes.FILE:
console.log("Files:", item.files);
break;
case NativeTypes.URL:
console.log("URLs:", item.urls);
break;
case NativeTypes.TEXT:
console.log("Text:", item.text);
break;
case NativeTypes.HTML:
console.log("HTML:", item.html);
break;
}
},
collect: (monitor) => ({
isOver: monitor.isOver(),
canDrop: monitor.canDrop(),
}),
});
return (
<div
ref={drop}
style={{
background: isOver && canDrop ? "lightcoral" : "white",
minHeight: "100px",
border: "2px dashed #ccc"
}}
>
Drop files, URLs, text, or HTML here
</div>
);
}Each native type provides different data formats when dropped:
{ files: File[] } with array of File objects{ urls: string[] } with array of URL strings{ text: string } with plain text content{ html: string } with HTML content as stringInstall with Tessl CLI
npx tessl i tessl/npm-react-dnd-html5-backend