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:- User drags an element
- Sensor updates position
- Position delta is calculated:
{x: currentX - initialX, y: currentY - initialY} - Each modifier transforms the coordinates in sequence
- Final transform is applied for rendering
Modifier base class
All modifiers extend theModifier base class:
apply method to transform coordinates:
Built-in modifiers
Axis constraints
Restrict movement to a single axis: Vertical axis:Snap to grid
Snap coordinates to a grid:Bounding rectangle
Constrain movement within a rectangle:DOM-specific modifiers
The@dnd-kit/dom package provides DOM-specific modifiers:
Restrict to window:
Configuration
Global modifiers
Apply to all draggables:Per-draggable modifiers
Override for specific draggables:Dynamic modifiers
You can change modifiers during runtime:Modifier pipeline
Modifiers run in sequence, each receiving the output of the previous: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:Lifecycle
Modifiers follow the plugin lifecycle:Enable/disable
Modifiers can be enabled or disabled:Combining modifiers
You can create complex behaviors by combining modifiers:Performance
Modifiers run on every position update during a drag. Keep them lightweight and avoid heavy computations.
Best practices
- Order matters: Place general transforms before constraints
- Keep it simple: Modifiers run frequently, avoid complex logic
- Use options: Make modifiers configurable with options
- Clean up: Implement
destroy()if you allocate resources - Test combinations: Ensure modifiers work well together
Related
Modifiers API
API reference
DOM modifiers
DOM-specific modifiers
Geometry
Coordinate utilities
Plugins
Plugin system