Back to Dnd Kit

DragDropProvider

apps/docs/svelte/components/drag-drop-provider.mdx

latest2.2 KB
Original Source

Overview

The DragDropProvider component is the root component for drag and drop interactions. It creates a DragDropManager instance and makes it available to all descendant components via Svelte's context API.

Usage

svelte
<script>
  import {DragDropProvider} from '@dnd-kit/svelte';

  function onDragEnd(event) {
    // Handle drag end
  }
</script>

<DragDropProvider {onDragEnd}>
  <!-- Your draggable and droppable elements -->
</DragDropProvider>

Events

The DragDropProvider accepts callback props for all drag and drop lifecycle stages:

PropDescription
onBeforeDragStartFired before a drag operation begins. Can be used to prepare state.
onDragStartFired when a drag operation starts.
onDragMoveFired when the dragged element moves.
onDragOverFired when the dragged element moves over a droppable target. Call event.preventDefault() to prevent the default behavior of plugins that respond to this event.
onDragEndFired when a drag operation ends (dropped or canceled).
onCollisionFired when collisions are detected between draggable and droppable elements.

Props

<ParamField path="manager" type="DragDropManager" optional> An optional externally created `DragDropManager` instance. If not provided, one will be created automatically. </ParamField> <ParamField path="plugins" type="Plugin[] | (defaults: Plugin[]) => Plugin[]" optional> Plugins to use. Defaults to the default preset. Pass an array to replace defaults, or a function to extend them.
ts
// Add a plugin alongside defaults
plugins={(defaults) => [...defaults, MyPlugin]}

// Replace defaults entirely
plugins={[MyPlugin]}
</ParamField> <ParamField path="sensors" type="Sensor[] | (defaults: Sensor[]) => Sensor[]" optional> Sensors to use. Defaults to `PointerSensor` and `KeyboardSensor`. Pass an array to replace defaults, or a function to extend them. </ParamField> <ParamField path="modifiers" type="Modifier[] | (defaults: Modifier[]) => Modifier[]" optional> Modifiers to apply to drag operations. Pass an array to replace defaults, or a function to extend them. </ParamField>