Skip to main content

Overview

Nested drag contexts enable sophisticated drag and drop interactions like sortable lists with multiple levels, tree structures, and kanban boards where both columns and cards are draggable. This guide covers advanced patterns for building hierarchical drag and drop interfaces.

What You’ll Build

A nested drag and drop interface featuring:
  • Multiple sortable containers that can themselves be reordered
  • Items that can be dragged between containers
  • Different drag types (containers vs. items)
  • Proper collision detection for nested elements
  • Real-time visual feedback at multiple levels

Prerequisites

You should be comfortable with:

Core Concepts

Drag Types and Groups

When working with nested contexts, you need to distinguish between different types of draggable elements:
Key concepts:
  • type identifies what kind of element this is
  • accept defines what types can be dropped onto this element
  • group links items to their parent container
  • Different types can have different collision priorities

Collision Priority

Nested elements need different collision priorities to ensure proper drop targets:
This ensures that when dragging over nested elements, items take precedence over their containers.

Implementation

Complete Example

Here’s a complete multi-level sortable implementation:

Styling

Advanced Patterns

Tree Structures

For tree structures with indefinite nesting, track depth and parent relationships:

Custom Sensors

Configure sensors for better control:

Key Takeaways

  • Use type and accept to differentiate between draggable element types
  • Set collisionPriority to control which elements are prioritized as drop targets
  • The group property links items to their parent containers
  • Use onDragOver for real-time updates and onDragEnd for finalization
  • The move helper from @dnd-kit/helpers handles complex nested moves
  • Drag handles provide better UX in complex layouts
  • Store snapshots for reliable cancellation behavior
  • Separate column reordering logic from item moving logic

Performance Tips

  1. Use React.memo for sortable components to prevent unnecessary re-renders
  2. Use useCallback for event handlers to maintain referential equality
  3. Consider virtualization for large lists
  4. Avoid inline functions in render for better performance

Next Steps

  • Explore drag overlays for smoother visual feedback
  • Learn about custom collision detection algorithms
  • Implement keyboard navigation for accessibility
  • Add animations and transitions for polish