Skip to content

Support coordinating trees inside a shared @dnd-kit DndContext - #410

Open
ccombe wants to merge 1 commit into
jameskerr:mainfrom
ccombe:external-dnd-context
Open

ccombe wants to merge 1 commit into
jameskerr:mainfrom
ccombe:external-dnd-context

Conversation

@ccombe

@ccombe ccombe commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Closes #396.

Why

In v5.0.0, every <Tree> creates and owns its own @dnd-kit <DndContext>. That isolates standalone trees cleanly, but it blocks two common workflows that apps need once they adopt @dnd-kit:

  1. Dragging tree items onto app-level drop targets (such as an Archive bin, Trash target, sidebar panel, or canvas outside the tree). Because the tree's <DndContext> is private, external droppables never see tree drags.
  2. Coordinating multiple trees inside one <DndContext>. Even if an app tried to share a context, raw node.id values and the shared OUTER_DROP_ID would collide across trees, useDndMonitor would deliver every drag to every tree, and every tree would mount a <DragOverlay> at once.

This PR adds an explicit, opt-in externalDndContext prop and exports treeCollisionDetection so apps can share one parent <DndContext> across multiple trees and external droppables without cross-talk or ID collisions.

Public API

  • externalDndContext?: boolean on TreeProps: opts <Tree> into an ancestor @dnd-kit <DndContext> via useDndMonitor instead of creating its own. When enabled, the parent <DndContext> owns sensors, accessibility, collisionDetection, and drag-handle touch-action styles (just like custom sensors).
  • treeCollisionDetection exported from react-arborist: pass to the parent <DndContext collisionDetection={treeCollisionDetection}> so each tree prefers row droppables over its own outer list droppable, clips overscan rows to that tree's visible list rect, and leaves other trees and external app droppables intact.
  • active.data.current: each tree draggable attaches { treeId, id, node, data } (matching treeRef.current.dndId), so parent onDragEnd handlers can identify the source tree and node even when multiple trees reuse identical node IDs.

How it works

<DndContext sensors={...} collisionDetection={treeCollisionDetection} onDragEnd={...}>
  <Tree externalDndContext>  -> ExternalDndMonitor (useDndMonitor, filters to own treeId)
                             -> IDs namespaced as __react_arborist__:${treeId}:node:${id} / :outer
                             -> DragPreviewContainer mounts <DragOverlay> only for own drags
  <Tree externalDndContext>  -> independent state, zero ID/event/overlay collision
  <ArchiveDropZone />        -> normal useDroppable(), handled in parent onDragEnd

Key design details:

  • Virtualization-safe ownership checks (dnd-ids.ts): @dnd-kit/core clears active.data.current when react-window unmounts the dragged row mid-drag, while keeping active.id. Parsing the per-tree ID prefix (parseNodeDndId(tree.dndId, active.id)) keeps drags, drop targeting, accessibility announcements, and <DragOverlay> working even after the source row scrolls out of view.
  • Per-tree collision clipping (collision-detection.ts): replaces the global OUTER_DROP_ID with container metadata (data.current.treeId and data.current.isOuterDrop) so an overscan row sticking out of Tree A is clipped against Tree A's list rect without swallowing collisions on Tree B or external app droppables.
  • No sniffing of dnd-kit internals: externalDndContext is an explicit boolean prop, and ExternalDndMonitor memoizes its monitor object before passing it to useDndMonitor (which throws dnd-kit's clear error if no ancestor <DndContext> exists).
  • Showcase workspace fix (modules/showcase/next.config.js): Yarn 4 installs a nested @dnd-kit/core under modules/showcase while symlinked react-arborist resolves the hoisted root copy. Aliasing @dnd-kit/core in next.config.js ensures the /shared-dnd-context demo bundles a single React context instance (and README.md notes this for monorepo consumers).

Scope and non-goals

  • Standalone <Tree> behavior is unchanged (externalDndContext defaults to false).
  • Each tree's onMove continues to fire only for reorders within that tree. Cross-tree moves (dragging a node from Tree A into Tree B) remain under the parent <DndContext onDragEnd> so the app controls cross-tree data mutations.
  • Non-breaking additive change (type: feature, inferred 5.1.0 minor bump in .changes/396-external-dnd-context.md).

Demo and verification

  • Showcase demo: added /shared-dnd-context (modules/showcase/pages/shared-dnd-context.tsx) with two controlled trees sharing overlapping IDs ("1", "2", "3") plus an external Archive drop zone.
  • Unit tests: yarn workspace react-arborist test passes 16 suites, 218/218 tests, with 0 console warnings/errors (added tests for foreign-drag event filtering, cross-tree and external droppable collision clipping, overlapping-ID independent trees, external droppable/draggable coordination, touch-action preservation under externalDndContext, and virtualized unmount survival).
  • Cypress E2E: yarn workspace showcase build && yarn workspace e2e test passes 5 specs, 22/22 tests (including new shared-dnd-context-spec.cy.js).
  • Build, lint, format, cycles: yarn lint (0 warnings/errors), yarn fmt:check (clean), yarn workspace react-arborist build (clean), npx madge@8.0.0 --circular modules/react-arborist/dist/module/index.js (0 circular dependencies across 49 files).

@netlify

netlify Bot commented Nov 30, 2026

Copy link
Copy Markdown

👷 Deploy request for react-arborist pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit d3cdd3c

@ccombe
ccombe marked this pull request as ready for review November 30, 2026 05:39
@ccombe

ccombe commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@TrevorBurnham Thanks for shipping v5.0.0! This follow-up for #396 is ready for review whenever you have a moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support coordinating the tree with an external DndContext

1 participant