Understanding Sensors
Sensors extend the baseSensor class and implement input detection:
Built-in Sensors
Pointer Sensor
Handles mouse, touch, and pen input:Keyboard Sensor
Enables keyboard-based drag operations:Drag Sensor
Uses native HTML drag and drop API:Creating a Custom Sensor
Let’s build a custom sensor that activates drag with a double-click:Step 1: Define the Sensor Class
Step 2: Use the Custom Sensor
Activation Constraints
Add constraints to control when dragging activates:Delay Constraint
Distance Constraint
Combined Constraints
Sensor Utilities
The@dnd-kit/dom/utilities package provides helpful utilities:
Event Coordinates
Event Listeners Manager
Type Guards
Using Multiple Sensors
Combine multiple sensors for comprehensive input support:Per-Entity Sensor Configuration
Configure sensors differently for specific draggable items:Advanced: Gamepad Sensor Example
Here’s a more complex example using the Gamepad API:Best Practices
1
Always Clean Up
Return cleanup functions from
bind() and implement destroy() to prevent memory leaks2
Respect Disabled State
Check
source.disabled before initiating drag operations3
Handle Edge Cases
Test with missing elements, rapid interactions, and simultaneous input
4
Provide Configuration
Use the
configurator helper to allow per-instance customizationNext Steps
1
Explore Source Code
Study the built-in sensors in
packages/dom/src/core/sensors/2
Test Thoroughly
Test custom sensors across different devices and input methods
3
Share Your Sensor
Consider publishing reusable sensors as npm packages