Skip to main content
Modifiers transform the coordinates of a drag operation, enabling features like axis constraints, snap-to-grid, boundary restrictions, and custom transformations.

Overview

Modifiers:
  • Transform drag coordinates before rendering
  • Run in a pipeline (each modifier receives the output of the previous)
  • Can be configured globally or per-draggable
  • Support options for customization
  • Are reactive and can be enabled/disabled dynamically

How modifiers work

During a drag operation:
  1. User drags an element
  2. Sensor updates position
  3. Position delta is calculated: {x: currentX - initialX, y: currentY - initialY}
  4. Each modifier transforms the coordinates in sequence
  5. Final transform is applied for rendering

Modifier base class

All modifiers extend the Modifier base class:
Override the apply method to transform coordinates:

Built-in modifiers

Axis constraints

Restrict movement to a single axis: Vertical axis:
Fixes x-coordinate to 0, allowing only vertical movement. Horizontal axis:
Fixes y-coordinate to 0, allowing only horizontal movement. Implementation:

Snap to grid

Snap coordinates to a grid:
Implementation:

Bounding rectangle

Constrain movement within a rectangle:
Implementation:

DOM-specific modifiers

The @dnd-kit/dom package provides DOM-specific modifiers: Restrict to window:
Prevents dragged elements from leaving the viewport. Restrict to element:
Constrains movement within a specific DOM element.

Configuration

Global modifiers

Apply to all draggables:

Per-draggable modifiers

Override for specific draggables:
Per-draggable modifiers replace the manager’s modifiers for that draggable.

Dynamic modifiers

You can change modifiers during runtime:
The drag operation automatically updates to use the new modifiers.

Modifier pipeline

Modifiers run in sequence, each receiving the output of the previous:
Order matters:
Put general transforms (snap, scale) before boundary constraints (restrict to window/element) for best results.

Custom modifiers

Create custom modifiers for specific behaviors:

Simple example

Momentum modifier

Conditional modifier

Operation snapshot

Modifiers receive a snapshot of the current drag operation:
Use this data to make informed decisions:

Lifecycle

Modifiers follow the plugin lifecycle:

Enable/disable

Modifiers can be enabled or disabled:
Disabled modifiers still run but can check their status:

Combining modifiers

You can create complex behaviors by combining modifiers:
Result: Vertical-only movement, snapped to 20px grid, constrained within bounds.

Performance

Modifiers run on every position update during a drag. Keep them lightweight and avoid heavy computations.
Use memoization if your modifier performs expensive calculations:

Best practices

  1. Order matters: Place general transforms before constraints
  2. Keep it simple: Modifiers run frequently, avoid complex logic
  3. Use options: Make modifiers configurable with options
  4. Clean up: Implement destroy() if you allocate resources
  5. Test combinations: Ensure modifiers work well together

Modifiers API

API reference

DOM modifiers

DOM-specific modifiers

Geometry

Coordinate utilities

Plugins

Plugin system