@molecule/app-drag-drop-dndkit-svelte

Feature · drag-drop · App (browser) · v1.1.0 · Apache-2.0

Svelte binding for @molecule/app-drag-drop-dndkit — a sortable list that reorders with the pointer, touch and the keyboard, with translated screen-reader announcements.

npm install @molecule/app-drag-drop-dndkit-svelte

npm · Source on GitHub

How it works

@molecule/app-drag-drop-dndkit-svelte is a ready-made drag-drop feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

<!-- Row.svelte -->
<script lang="ts">
  import { createAttachmentKey } from 'svelte/attachments'

  import { useSortableItem } from '@molecule/app-drag-drop-dndkit-svelte'

  let { task }: { task: Task } = $props()

  const { ref, isDragging } = useSortableItem({ id: task.id })
  const attach = { [createAttachmentKey()]: ref }
</script>

<li {...attach} data-dragging={isDragging}>{task.title}</li>

Works with: @molecule/app-drag-drop, @molecule/app-drag-drop-dndkit

Reference

Svelte sortable list for @molecule/app-drag-drop-dndkit.

SortableList renders a list the user reorders with the pointer, touch or the keyboard, on @dnd-kit/svelte. Each row calls useSortableItem. The options are @molecule/app-drag-drop-dndkit's SortableListOptions, the same in the React, Vue, Solid, Svelte and Angular bindings, so a list swaps between frameworks unchanged.

useSortableItem returns Svelte attachments for the row element (ref) and an optional drag handle (handleRef), plus the row's isDragging and isDropTarget state as read-only reactive getters. Apply an attachment with Svelte's attachment tag on the element, or with createAttachmentKey() in a spread, as the example below shows.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

<!-- Row.svelte -->
<script lang="ts">
  import { createAttachmentKey } from 'svelte/attachments'

  import { useSortableItem } from '@molecule/app-drag-drop-dndkit-svelte'

  let { task }: { task: Task } = $props()

  const { ref, isDragging } = useSortableItem({ id: task.id })
  const attach = { [createAttachmentKey()]: ref }
</script>

<li {...attach} data-dragging={isDragging}>{task.title}</li>
<!-- Tasks.svelte -->
<script lang="ts">
  import { SortableList } from '@molecule/app-drag-drop-dndkit-svelte'
  import Row from './Row.svelte'

  let { tasks }: { tasks: Task[] } = $props()
</script>

<SortableList
  items={tasks}
  onReorder={(next) => (tasks = next)}
  getItemLabel={(task) => task.title}
>
  <ul>
    {#each tasks as task (task.id)}
      <Row {task} />
    {/each}
  </ul>
</SortableList>

Type

feature

Installation

npm install @molecule/app-drag-drop-dndkit-svelte @dnd-kit/svelte @molecule/app-drag-drop @molecule/app-drag-drop-dndkit svelte

API

Interfaces

SortableListProps

Props for SortableList: the options every framework's list takes, plus the list itself as children.

interface SortableListProps<T extends { id: string }> extends SortableListOptions<T> {
  /** The rendered list, whose rows call `useSortableItem`. */
  children: Snippet
}

SortableItemState

What useSortableItem returns for one row.

interface SortableItemState {
  /**
   * A Svelte attachment for the row's element: Svelte calls it with the
   * element when the row mounts, and with the returned cleanup when it is
   * removed. dnd-kit makes the element focusable and sets its `role`,
   * `aria-roledescription`, `aria-describedby`, `aria-pressed` and
   * `aria-disabled`.
   *
   * @param node - The row's element.
   * @returns The detach function Svelte calls when the row is removed.
   */
  ref: (node: HTMLElement) => () => void
  /**
   * A Svelte attachment for a drag handle inside the row (the library's
   * `attachHandle`) so only the handle starts a drag; leave it unused to drag
   * by the whole row.
   *
   * @param node - The handle's element.
   * @returns The detach function Svelte calls when the handle is removed.
   */
  handleRef: (node: HTMLElement) => () => void
  /** `true` while this row is being dragged. */
  readonly isDragging: boolean
  /** `true` while another dragged row is over this one. */
  readonly isDropTarget: boolean
}

Functions

useSortableItem(options)

Makes one row of a SortableList draggable. Call it during the row component's initialization, inside the list.

function useSortableItem(options: SortableItemOptions): SortableItemState
  • options — The row's item id, drag data and disabled flag.

Returns: The attachments to apply to the row (and its handle) plus its drag state.

Constants

SortableList

const SortableList: LegacyComponentType

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-drag-drop ^1.0.1
  • @molecule/app-drag-drop-dndkit ^2.0.0
  • svelte ^5.29.0

Runtime Dependencies

  • @dnd-kit/svelte
  • @molecule/app-drag-drop
  • @molecule/app-drag-drop-dndkit
  • svelte

The screen-reader instructions and announcements use the dragDrop.sortable.* keys; bond @molecule/app-locales-drag-drop-dndkit for their translations. Persist the order in onReorder; if saving fails, restore the previous items. Set an item's disabled option to pin one row, or the list's disabled prop to pin the whole list.

Translations

Translation strings are provided by @molecule/app-locales-drag-drop-dndkit.