You will learn
- How to make elements draggable
- How to set up droppable targets
- How to listen to drag and drop events to move elements
Installation
Install@dnd-kit/dom
in your project:
Creating draggable elements
Let’s get started by creating draggable elements that can be dropped over droppable targets. In this example, we’ll be using the Draggable class to create a draggable element. We’ll also need to create a DragDropManager instance to manage the drag and drop interactions.Configuration for the draggable element:
id
(required): Unique identifierelement
: The DOM element to make draggablehandle
: Optional drag handle elementdisabled
: Whether dragging is disabled
Creating droppable targets
In order for our draggable elements to have targets where they can be dropped, we need to create droppable elements. To do so, we’ll be using the Droppable class. Like the Draggable class, the Droppable class requires an element and an id as arguments.Configuration for the droppable target:
id
(required): Unique identifierelement
: The DOM element to make droppableaccepts
: Types of draggable elements to acceptdisabled
: Whether dropping is disabled