Skip to main content
Sensors detect and handle user input for drag operations. While dnd-kit provides built-in sensors for pointer, keyboard, and native drag events, you can create custom sensors for specialized interactions.

Understanding Sensors

Sensors extend the base Sensor 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:
When using multiple sensors, ensure they don’t conflict. For example, both PointerSensor and DoubleClickSensor listening to click events might interfere.

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:
When building custom sensors, use the effect function from @dnd-kit/state to automatically clean up when elements are removed from the DOM.

Best Practices

1

Always Clean Up

Return cleanup functions from bind() and implement destroy() to prevent memory leaks
2

Respect Disabled State

Check source.disabled before initiating drag operations
3

Handle Edge Cases

Test with missing elements, rapid interactions, and simultaneous input
4

Provide Configuration

Use the configurator helper to allow per-instance customization

Next 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