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
The React DnD HTML5 Backend provides the HTML5 drag and drop implementation for React DnD, enabling web applications to implement sophisticated drag-and-drop interactions using the native HTML5 Drag and Drop API. It serves as the core backend that handles browser-specific drag and drop events, manages drop zones and drag sources, and provides utilities for drag preview customization.
npm install react-dnd-html5-backendimport { HTML5Backend, getEmptyImage, NativeTypes } from "react-dnd-html5-backend";
import type { HTML5BackendOptions, HTML5BackendContext } from "react-dnd-html5-backend";For CommonJS:
const { HTML5Backend, getEmptyImage, NativeTypes } = require("react-dnd-html5-backend");import { DndProvider } from "react-dnd";
import { HTML5Backend } from "react-dnd-html5-backend";
// Basic setup with default options
function App() {
return (
<DndProvider backend={HTML5Backend}>
{/* Your drag and drop components */}
</DndProvider>
);
}
// With custom options
const backendOptions = {
rootElement: document.getElementById("drag-drop-container")
};
function AppWithOptions() {
return (
<DndProvider backend={HTML5Backend} options={backendOptions}>
{/* Your drag and drop components */}
</DndProvider>
);
}The HTML5 Backend is built around several key components:
HTML5Backend function creates backend instances configured for React DnDHTML5BackendImpl class handles all drag/drop event management and DOM interactionsNativeTypesCore factory function for creating HTML5 drag and drop backend instances that integrate with React DnD.
const HTML5Backend: BackendFactory = function createBackend(
manager: DragDropManager,
context?: HTML5BackendContext,
options?: HTML5BackendOptions,
): HTML5BackendImpl;
type HTML5BackendContext = Window | undefined;
interface HTML5BackendOptions {
/** The root DOM node to use for subscribing to events. Default=Window */
rootElement: Node;
}Built-in support for native HTML5 drag source types including files, URLs, text, and HTML content.
namespace NativeTypes {
const FILE: "__NATIVE_FILE__";
const URL: "__NATIVE_URL__";
const TEXT: "__NATIVE_TEXT__";
const HTML: "__NATIVE_HTML__";
}Utilities for customizing drag preview behavior, including empty image creation for custom drag previews.
function getEmptyImage(): HTMLImageElement;interface Backend {
setup(): void;
teardown(): void;
connectDragSource(sourceId: string, node: Element, options: any): Unsubscribe;
connectDragPreview(sourceId: string, node: Element, options: any): Unsubscribe;
connectDropTarget(targetId: string, node: HTMLElement): Unsubscribe;
profile(): Record<string, number>;
}
type BackendFactory = (
manager: DragDropManager,
context?: any,
options?: any
) => Backend;
type Unsubscribe = () => void;Install with Tessl CLI
npx tessl i tessl/npm-react-dnd-html5-backend