Skip to content

fix(dom): return null from getRoot while disconnected - #2597

Merged
rkaraivanov merged 2 commits into
masterfrom
dpetev/get-root-typing
Nov 12, 2026
Merged

rkaraivanov merged 2 commits into
masterfrom
dpetev/get-root-typing

Conversation

@damyanpetev

Copy link
Copy Markdown
Member

Description

getRoot cast getRootNode() to Document | ShadowRoot, but the root of a disconnected node is the node itself, a detached ancestor or a fragment. The cast hid that from the compiler: a property change on a detached virtual scroll threw, because the root had no adoptedStyleSheets, and getElementByIdFromRoot threw for a detached button until #2592.

getRoot now returns null while the node is disconnected. A connected node's root is always its document or a shadow root, also in another window, so the check needs no type test on the root. The callers that use the root's APIs handle null:

  • The virtual scroll adopts its sheet in connectedCallback instead.
  • getElementByIdFromRoot returns null, so the button no longer needs its own isConnected guard.
  • The focus and drag callers find no active element or no hit. getDeepActiveElement accepts null and uses document.activeElement.
  • The highlight drops its 'adoptedStyleSheets' in root check, which getRoot now covers.

The alert, the group registry, the radio scope and the popover anchor observer only compare or observe roots, so they call getRootNode() and keep their behavior for disconnected nodes.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Testing

Virtual scroll spec: a property change while detached, then attached again. It fails on master with Cannot read properties of undefined (reading 'includes').

Checklist

  • My code follows the project's coding standards
  • I have tested my changes locally
  • I have updated documentation if needed
  • I have added a CHANGELOG.md entry under [Unreleased]
  • I have read the contributing guidelines, including the accessibility, dependency and security rules

🤖 Generated with Claude Code

damyanpetev and others added 2 commits November 12, 2026 17:07
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the disconnected-node handling changes.

No open issues 🎉

What changed in this PR

This PR makes the shared getRoot utility return null for disconnected nodes, preventing detached-component errors while preserving callers that need raw root-node identity.

Changes:

  • Updates root API callers to handle disconnected nodes safely.
  • Skips virtual-scroll stylesheet adoption while detached, relying on connection to adopt it.
  • Adds a detach/reattach regression test and updates the specification and changelog.
File Description
src/​internals/​utils/​dom.ts Returns nullable roots and handles them in lookup and focus helpers.
src/​internals/​directives/​drag.ts Guards hit testing when the target is disconnected.
src/​internals/​controllers/​roving-focus.ts Handles a missing root during active-element lookup.
src/​internals/​controllers/​group.ts Preserves group scope identity with getRootNode().
src/​internals/​bases/​alert.ts Preserves raw root comparisons for anchoring.
src/​components/​virtualization/​virtualization.ts Skips stylesheet adoption while disconnected.
src/​components/​virtualization/​virtualization.spec.ts Tests detached updates and styling after reattachment.
src/​components/​virtualization/​spec.md Documents detached-update behavior and its test scenario.
src/​components/​virtualization/​recycle.ts Handles missing roots during focused-item lookup.
src/​components/​stepper/​stepper.ts Guards active-step lookup for disconnected steppers.
src/​components/​radio/​controller.ts Preserves radio group scopes with getRootNode().
src/​components/​popover/​position/​native.ts Keeps observing raw target roots.
src/​components/​highlight/​service.ts Uses the shared nullable-root helper for stylesheet scope.
src/​components/​button/​button-base.ts Removes the redundant connection guard from command-target lookup.
CHANGELOG.md Records the detached virtual-scroll fix.

Review effort: Balanced

@damyanpetev
damyanpetev marked this pull request as ready for review November 12, 2026 16:12
@rkaraivanov
rkaraivanov merged commit b12a0c8 into master Nov 12, 2026
11 checks passed
@rkaraivanov
rkaraivanov deleted the dpetev/get-root-typing branch November 12, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants