apps/docs/react/components/drag-drop-provider.mdx
The DragDropProvider component creates a context that enables drag and drop interactions for its children. It manages the drag and drop state and coordinates between draggable and droppable elements.
Wrap your application or a section with DragDropProvider:
import {DragDropProvider} from '@dnd-kit/react';
function App() {
return (
<DragDropProvider>
<YourDraggableContent />
</DragDropProvider>
);
}
Listen to drag and drop events to respond to user interactions:
function App() {
return (
<DragDropProvider
onBeforeDragStart={({source, event}) => {
// Optionally prevent dragging
if (shouldPreventDrag(source)) {
event.preventDefault();
}
}}
onDragStart={({source}) => {
console.log('Started dragging', source.id);
}}
onDragMove={({operation}) => {
const {position} = operation;
console.log('Current position:', position);
}}
onDragOver={({source, target}) => {
console.log(`${source.id} is over ${target.id}`);
}}
onDragEnd={({source, target}) => {
if (target) {
console.log(`Dropped ${source.id} onto ${target.id}`);
}
}}
>
<YourDraggableContent />
</DragDropProvider>
);
}
You can create multiple independent drag and drop contexts:
function App() {
return (
<div>
<DragDropProvider>
<FileList />
</DragDropProvider>
<DragDropProvider>
<TaskList />
</DragDropProvider>
</div>
);
}
Customize behavior with plugins, sensors, and modifiers. Each accepts either an array (which replaces the defaults) or a function that receives the defaults.
Use the function form to add to or configure the defaults without replacing them:
import {DragDropProvider} from '@dnd-kit/react';
import {Feedback} from '@dnd-kit/dom';
import {RestrictToWindow} from '@dnd-kit/dom/modifiers';
function App() {
return (
<DragDropProvider
// Add a plugin alongside defaults
plugins={(defaults) => [
...defaults,
Feedback.configure({ dropAnimation: null }),
]}
// Add a modifier
modifiers={(defaults) => [...defaults, RestrictToWindow]}
>
<YourDraggableContent />
</DragDropProvider>
);
}
Pass an array to fully replace the defaults:
import {DragDropProvider} from '@dnd-kit/react';
import {PointerSensor, KeyboardSensor} from '@dnd-kit/dom';
function App() {
return (
<DragDropProvider
sensors={[
PointerSensor,
KeyboardSensor,
]}
plugins={[
AutoScroller,
Accessibility,
]}
modifiers={[
RestrictToWindow,
]}
>
<YourDraggableContent />
</DragDropProvider>
);
}