Quick Example

useDragDrop Usage

ตัวอย่าง import และการใช้งานแบบสั้นสำหรับหน้า `use-drag-drop`

Code

                <script lang="ts">
 import { useDragDrop } from 'svelora';

 const dragDrop = useDragDrop();

 let columns = $state({
   todo: ['Wireframes'],
   done: ['Kickoff']
 });

 function moveToDone(id: string) {
   const task = columns.todo.find((item) => item === id);
   if (!task) return;
   columns = {
     todo: columns.todo.filter((item) => item !== id),
     done: [...columns.done, task]
   };
 }
</script>

<dragDrop.Provider>
 <div class="grid gap-4 md:grid-cols-2">
  <div
    use:dragDrop.droppable={{
      id: 'todo',
      onDrop: ({ data }) => console.log('dropped on todo', data)
    }}
    class="rounded-lg border p-4"
  >
   {#each columns.todo as task (task)}
    <div use:dragDrop.draggable={{ id: task, container: 'todo', data: task }} class="rounded-md border p-2">{task}</div>
   {/each}
  </div>

  <div
    use:dragDrop.droppable={{ id: 'done', onDrop: ({ data }) => moveToDone(String(data)) }}
    class="rounded-lg border p-4"
  >
   {#each columns.done as task (task)}
    <div use:dragDrop.draggable={{ id: task, container: 'done', data: task }} class="rounded-md border p-2">{task}</div>
   {/each}
  </div>
 </div>
</dragDrop.Provider>
            

useDragDrop

Cross-container drag and drop powered by @dnd-kit/svelte. Wrap markup in dragDrop.Provider, use use:dragDrop.draggable on sources and use:dragDrop.droppable on targets. Browse all demos in the Drag & Drop playground. For reordering within one list, use useSortable.

When to use what

useDragDrop — kanban, trays, moving cards between columns. useSortablereorder inside one list. FileUploadOS file drops. Editorblock reorder in rich text.

Kanban board

To do

Design tokens
Sortable hook
Docs page

Done

accept — rejects empty labels

Kickoff meeting

API Reference

draggable options

id

string

Unique id for the draggable element.

container

string

Source zone id — must match the droppable id of the column this item is in.

data

unknown

Payload passed to droppable onDrop.

disabled

boolean | (() => boolean)

Prevent dragging.

handle

string

Optional CSS selector for a drag handle.

droppable options

id

string

Zone id — drops from the same id are ignored.

onDrop

(payload) => void

Called with { id, data? } from the draggable.

accept

(payload) => boolean

Optional filter — return false to reject the drop.

disabled

boolean | (() => boolean)

Prevent dropping on this zone.

Return value

Provider

Component

Required wrapper — mounts @dnd-kit/svelte DragDropProvider.

draggable

Action<HTMLElement, UseDraggableOptions>

Attach to drag sources.

droppable

Action<HTMLElement, UseDroppableOptions>

Attach to drop targets.

draggingId

string | null

Id of the item currently being dragged.