A Svelte port of the powerful @dnd-kit library - the modern, lightweight, performant, accessible and extensible drag & drop toolkit.
Install it:
npm i @dnd-kit-svelte/core
# or
yarn add @dnd-kit-svelte/core
# or
pnpm add @dnd-kit-svelte/core
This library provides a complete port of dnd-kit to Svelte, maintaining feature parity with the original React implementation while adapting to Svelte's reactivity system. All documentation and APIs from the original dnd-kit library apply here, with some Svelte-specific adaptations.
The main difference lies in how reactive values are handled. Since Svelte components don't rerender the same way React components do, we've adapted the API to work with Svelte's reactivity system.
In hooks like useSortable
, useDraggable
, etc., you can pass a function to any field that needs to be reactive. The function will be called whenever the value needs to be accessed, ensuring you always get the latest value from Svelte's reactive scope.
Example:
// React dnd-kit
import {useSortable} from '@dnd-kit/sortable';
useSortable({
id: item.id,
data: item,
});
// Svelte dnd-kit
import {useSortable} from '@dnd-kit-svelte/sortable';
useSortable({
// Static value
id: item.id,
// Reactive value using a function
data: () => item, // Access reactive state value
});
In React, components re-render when their state changes, so hooks can return values directly. However, since Svelte components don't re-render, all non-function properties returned from hooks use a .current
getter to ensure you always access the latest value.
Example:
// React dnd-kit
const { attributes, listeners, isDragging } = useSortable({ id });
<div {...attributes} {...listeners}>
{isDragging ? 'Dragging' : 'Not dragging'}
</div>
// Svelte dnd-kit
const { attributes, listeners, isDragging } = useSortable({ id });
<div {...attributes.current} {...listeners.current}>
{isDragging.current ? 'Dragging' : 'Not dragging'}
</div>
This pattern is used consistently across all hooks:
useDraggable
useDroppable
useSortable
Properties that use .current
include:
isDragging
, isOver
, etc.)attributes
, listeners
)All core concepts from dnd-kit remain the same:
For detailed documentation on these concepts, please refer to the original dnd-kit documentation.
MIT © Haniel Ubogu