Touch and Keyboard Support
The dnd-kit library provides robust sensor implementations for pointer (mouse/touch/pen) and keyboard interactions. This guide shows you how to configure and customize these sensors for optimal user experience across all input methods.Understanding Sensors
Sensors detect and initiate drag operations. dnd-kit includes two primary sensors:- PointerSensor: Handles mouse, touch, and pen interactions
- KeyboardSensor: Handles keyboard-based dragging
Basic Sensor Setup
packages/dom/src/core/sensors/pointer/PointerSensor.ts
PointerSensor Configuration
The PointerSensor handles mouse, touch, and pen inputs with intelligent activation constraints.Default Activation Constraints
By default, the PointerSensor uses different activation constraints based on input type:packages/dom/src/core/sensors/pointer/PointerSensor.ts
Custom Activation Constraints
Configure activation constraints globally:Per-Draggable Activation Constraints
Set different constraints for individual draggables:Dynamic Activation Constraints
Use a function to determine constraints based on the event:packages/dom/src/core/sensors/pointer/PointerSensor.ts
Touch-Specific Optimizations
Preventing Scroll During Touch Drag
The PointerSensor automatically prevents scrolling during touch drag operations:packages/dom/src/core/sensors/pointer/PointerSensor.ts
Touch Delay Tolerance
The tolerance parameter allows small movements before canceling activation:Preventing Unwanted Activation
Prevent activation on specific elements:packages/dom/src/core/sensors/pointer/PointerSensor.ts
KeyboardSensor Configuration
The KeyboardSensor enables full keyboard accessibility.Default Keyboard Controls
packages/dom/src/core/sensors/keyboard/KeyboardSensor.ts
Custom Keyboard Codes
Custom Movement Offset
Control how far items move with each keypress:packages/dom/src/core/sensors/keyboard/KeyboardSensor.ts
Shift Key Multiplier
The KeyboardSensor automatically multiplies movement by 5 when Shift is held:packages/dom/src/core/sensors/keyboard/KeyboardSensor.ts
Multiple Activator Elements
Use multiple elements to activate dragging:packages/dom/src/core/sensors/pointer/PointerSensor.ts
Complete Example: Mobile-Friendly Sortable List
Handling Different Pointer Types
Detect and handle different input methods:Scroll Into View
The KeyboardSensor automatically scrolls elements into view when keyboard dragging starts:packages/dom/src/core/sensors/keyboard/KeyboardSensor.ts
AutoScroller Integration
The KeyboardSensor automatically disables AutoScroller during keyboard dragging to prevent conflicts:packages/dom/src/core/sensors/keyboard/KeyboardSensor.ts
Best Practices
- Always include both sensors: Use both PointerSensor and KeyboardSensor for maximum accessibility.
- Test on real devices: Touch behavior varies across devices - test on actual phones and tablets.
- Use appropriate delays for touch: 250-400ms delays help distinguish dragging from scrolling.
- Add visual feedback for long press: Show a visual indicator during the activation delay.
- Provide drag handles on mobile: Small drag handles are easier to activate on touch devices.
- Consider pointer coalescing: On high-refresh-rate displays, consider throttling pointer events.
- Test with keyboard only: Ensure all functionality works without a mouse.
- Respect reduced motion: The Feedback plugin automatically handles this for keyboard transitions.
- Handle activation cancellation: Provide clear feedback when activation is canceled (e.g., moved too far during delay).
- Use semantic HTML: Proper elements (buttons, etc.) work better with assistive technologies.
Preventing Native Behaviors
The PointerSensor automatically prevents:- Native drag and drop
- Text selection during drag
- Context menus during drag
- Click events after successful drag
- Scroll during touch drag