Skip to main content

Overview

The Svelte adapter provides functions and components that integrate drag and drop functionality into your Svelte 5 applications using runes and the modern Svelte API.

Installation

Install the Svelte package and its dependencies:
Requirements: Svelte 5.29.0 or higher

Getting Started

1

Add DragDropProvider

Wrap your application with the DragDropProvider component:
2

Create draggable components

Use the createDraggable function to make elements draggable:
3

Create droppable zones

Use the createDroppable function to create drop targets:
4

Handle drag events

Listen to events via the provider props:

Complete Example

Here’s a full drag and drop implementation:

Sortable Lists

Create sortable lists with the createSortable function:

Function APIs

createDraggable

The createDraggable function signature from /home/daytona/workspace/source/packages/svelte/src/core/draggable/createDraggable.svelte.ts:13:

createDroppable

The createDroppable function signature from /home/daytona/workspace/source/packages/svelte/src/core/droppable/createDroppable.svelte.ts:13:

createSortable

The createSortable function signature from /home/daytona/workspace/source/packages/svelte/src/sortable/createSortable.svelte.ts:14:

Advanced Features

Drag Handles

Use separate elements for dragging:

Modifiers

Constrain drag behavior:

Sensors

Customize activation:

Type Safety

Define custom data types:

Svelte 5 Runes

The adapter uses Svelte 5’s runes for reactivity:

Actions (use:directive)

All attach functions return cleanup functions, making them perfect for use with Svelte actions:

Best Practices

  • Stable IDs: Use consistent, unique IDs for draggables and droppables
  • Key blocks: Always use proper keys in {#each} blocks for sortables
  • Runes: Leverage Svelte 5 runes for reactive inputs
  • Actions: Use use:attach directives for clean element binding
  • Cleanup: Actions automatically handle cleanup when components unmount

Next Steps

Sortable Lists

Build sortable lists with animations

Multiple Containers

Drag between multiple containers

Sensors

Configure interaction methods

Events

Handle drag and drop events