Overview
Sensors:- Detect user input (pointer events, keyboard events, etc.)
- Validate activation constraints
- Initiate drag operations by calling manager actions
- Handle movement and completion
- Clean up event listeners when done
Built-in sensors
dnd-kit provides two primary sensors in@dnd-kit/dom:
PointerSensor
Handles mouse, touch, and pen input:PointerSensor uses the Pointer Events API, which unifies mouse, touch, and pen interactions.
KeyboardSensor
Handles keyboard navigation:KeyboardSensor enables accessibility by allowing users to drag items using arrow keys.
Default configuration
By default, both sensors are enabled:Sensor architecture
Base class
All sensors extend the abstractSensor base class:
Lifecycle
- Binding: The sensor binds to each draggable element
- Activation: User interaction triggers activation constraints
- Start: Constraints are met, drag operation starts
- Move: Sensor tracks movement and updates position
- End: User completes or cancels the drag
- Cleanup: Event listeners are removed
PointerSensor
ThePointerSensor handles pointer-based input.
Activation constraints
Activation constraints prevent accidental drags:Built-in constraints
Delay constraint:Default behavior
ThePointerSensor has smart defaults:
Configuration options
KeyboardSensor
TheKeyboardSensor enables keyboard-based dragging.
Activation
By default:- Press
SpaceorEnteron a draggable to start dragging - Use arrow keys to move
- Press
SpaceorEnteragain to drop - Press
Escapeto cancel
Configuration
Custom sensors
You can create custom sensors by extending theSensor base class.
Basic example
Using manager actions
Sensors interact with the manager through actions:Activation constraints
Activation constraints determine when a drag should start:ActivationController
TheActivationController manages constraints:
Per-draggable sensors
You can configure sensors per draggable:Configuration pattern
Sensors use the configurator pattern for options:How it works
Theconfigure static method returns a descriptor:
Sensor implementation details
Binding to draggables
When a draggable is registered, sensors bind to it:Movement tracking
Sensors track movement and update the manager:Cleanup
Sensors clean up when destroyed:Best practices
Test on multiple input types: Make sure to test your drag and drop implementation with mouse, touch, and keyboard to ensure all sensors work correctly.
Related
Sensors API
Sensors API reference
Custom sensors
Build your own sensors
Accessibility
Keyboard navigation
Touch devices
Touch-specific configuration