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
Overview
The@dnd-kit/svelte package provides a set of Svelte primitives and components that you can use to build drag and drop interfaces. It is a thin Svelte integration layer built on top of the vanilla library, so all of the same concepts are shared and can be used. You can refer to the vanilla documentation of these concepts, such as plugins, modifiers, and sensors.
@dnd-kit/svelte requires Svelte 5.29 or later.Installation
Before getting started, make sure you install@dnd-kit/svelte in your project:
Making elements draggable
Let’s get started by creating draggable elements that can be dropped over droppable targets. To do so, we’ll be using thecreateDraggable primitive.
The createDraggable primitive requires a unique id. It returns an object with an attach function that you can use with the {@attach} directive to connect an element to the drag system.
Creating droppable elements
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 thecreateDroppable primitive.
Like createDraggable, the createDroppable primitive requires a unique id.
Putting all the pieces together
Now that we have both draggable and droppable elements, we can put them together to create a simple drag and drop interaction. We’ll be using theDragDropProvider component to wrap our draggable and droppable elements. This component handles the drag and drop interactions and allows us to listen to drag and drop events.
App.svelte
Next steps
Now that you have a basic understanding of how to make elements draggable and droppable, you can explore the concepts covered in this quickstart guide in more detail:DragDropProvider
Create drag and drop contexts for your draggable and droppable elements.
createDraggable
Learn how to make elements draggable with the
createDraggable primitive.createDroppable
Learn how to create droppable targets with the
createDroppable primitive.createSortable
Learn how to create sortable elements with the
createSortable primitive.