Skip to main content

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

  1. Always include both sensors: Use both PointerSensor and KeyboardSensor for maximum accessibility.
  2. Test on real devices: Touch behavior varies across devices - test on actual phones and tablets.
  3. Use appropriate delays for touch: 250-400ms delays help distinguish dragging from scrolling.
  4. Add visual feedback for long press: Show a visual indicator during the activation delay.
  5. Provide drag handles on mobile: Small drag handles are easier to activate on touch devices.
  6. Consider pointer coalescing: On high-refresh-rate displays, consider throttling pointer events.
  7. Test with keyboard only: Ensure all functionality works without a mouse.
  8. Respect reduced motion: The Feedback plugin automatically handles this for keyboard transitions.
  9. Handle activation cancellation: Provide clear feedback when activation is canceled (e.g., moved too far during delay).
  10. 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

Window Resize Handling

The KeyboardSensor automatically cancels keyboard drag operations when the window is resized, preventing layout issues.