Skip to content

fix(shadow-dom): shadow-safe FocusScope, per-root focus guards, retarget-safe listeners - #2794

Merged
zernonia merged 3 commits into
v3from
2725-shadow-dom-part2
Jul 13, 2026
Merged

zernonia merged 3 commits into
v3from
2725-shadow-dom-part2

Conversation

@zernonia

@zernonia zernonia commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

Summary

Second and final part of shadow-DOM support (Refs #2725). Part 1 (#2792) landed the shared helpers (getRootNode/getOwnerDocument/getEventTarget/getElementByIdFrom/injectStyle/…). This PR wires the remaining components that still assumed the light DOM, so an open shadow-rooted component actually works — most importantly the focus trap.

What changed

FocusScope — shadow-safe trap (FocusScope.vue, getRootNode.ts)

  • Attaches focusin/focusout to the container's root (the ShadowRoot in a web component) so event targets aren't retargeted to the host, plus a document focusin fallback: a focusout-driven recall loses the race to the pending focus commit, and the recall that actually wins fires as a document focusin — which a shadow-root listener never sees.
  • Adds containsComposed() for the composed-target scope-membership check across shadow boundaries.
  • Light DOM (root === ownerDocument) behavior is unchanged.

Focus guards — per-root (useFocusGuards.ts + 4 callers)

  • useFocusGuards(element?) refcounts guards per root (WeakMap) and places them at the ShadowRoot edges when scoped; the default (no arg) stays document-body for back-compat.
  • Combobox/Popover/Menu/Select content pass their currentElement so a shadow-mounted trap's edge guards live inside the same root.

Retarget-safe listeners + injected styles

  • ScrollAreaScrollbarImpl / SelectContentImpl: document-level wheel/pointer handlers read the composed target so a shadow-mounted content isn't mistaken for "outside" itself; body user-select toggles via getOwnerDocument.
  • Splitter cursor: global cursor style now goes through injectStyle (per-document, nonce-aware) instead of a module-global <style>.

Body scroll lock — intentionally unchanged. A shadow root shares the host document.body, which is the correct scroll surface, so useBodyScrollLock needs no scoping.

Known limitations (documented in CONTRIBUTING)

Portalled content still defaults to body (outside the root) unless ConfigProvider's teleportTo points inside the shadow root; closed shadow roots, cross-boundary aria idrefs for teleported content, HoverCard selection/scroll (composed: false), useHideOthers, and the iOS touchmove overflow walk remain browser-only / genuinely unfixable.

Testing

  • New shadow-root tests: FocusScope (intra-shadow moves don't yank; escapes recall into the scope; removed element recovers to container) and useFocusGuards (back-compat + per-root refcount at shadow edges).
  • Full suite green: 2033 tests / 105 files; type-check clean.

Refs #2725

Summary by CodeRabbit

  • Bug Fixes

    • Improved focus trapping and restoration for components rendered inside Shadow DOM.
    • Enhanced focus guards, outside-click detection, scrolling, and text selection behavior across shadow boundaries.
    • Improved focus handling when nested elements are removed or focus moves outside a component.
    • Added safer support for components hosted in different documents.
  • Documentation

    • Expanded Shadow DOM guidance, including Teleport behavior and known browser limitations.

zernonia and others added 3 commits July 13, 2026 01:58
…er cursor

ScrollArea: document wheel listener reads the composed target (getEventTarget) so
wheel-over-scrollbar works in a shadow root; body user-select via getOwnerDocument.
Select: document pointerup containment reads the composed target. Splitter cursor
now injects via the shared injectStyle helper (nonce-threaded). Tooltip/HoverCard
contains-sites already shadow-safe (DismissableLayer composes the target /
element-level listeners).

Refs #2725

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
FocusScope attaches focusin/focusout to the container's root (ShadowRoot in a web
component) so targets aren't retargeted to the host, PLUS a document focusin
fallback — a focusout recall loses the race to the pending focus commit, so the
recall that actually wins is the document focusin (which a shadow-root listener
never sees). Adds containsComposed() for the composed-target scope check. In light
DOM (root === ownerDocument) behavior is unchanged.

useFocusGuards gains an optional element param: guards are refcounted per root
(WeakMap) and placed at the ShadowRoot edges when scoped; FocusGuards.vue stays
document-body (back-compat). The 4 content components (Combobox/Popover/Menu/Select)
pass currentElement so a shadow-mounted trap's edge guards are visible to it.

Verified via jsdom probes: intra-shadow moves don't yank, escapes recall into the
scope, removed-element recovers. Full suite (2033) green. Body scroll lock is
unchanged — a shadow root shares the host document body, which it already locks.

Refs #2725

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… limits

Refs #2725

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3f02f8b6-1a86-4100-9dae-da6b509aa3c3

📥 Commits

Reviewing files that changed from the base of the PR and between de6b3ca and e8a1f99.

📒 Files selected for processing (13)
  • CONTRIBUTING.md
  • packages/core/src/Combobox/ComboboxContentImpl.vue
  • packages/core/src/FocusScope/FocusScope.test.ts
  • packages/core/src/FocusScope/FocusScope.vue
  • packages/core/src/Menu/MenuContentImpl.vue
  • packages/core/src/Popover/PopoverContentImpl.vue
  • packages/core/src/ScrollArea/ScrollAreaScrollbarImpl.vue
  • packages/core/src/Select/SelectContentImpl.vue
  • packages/core/src/Splitter/utils/style.ts
  • packages/core/src/shared/getRootNode.ts
  • packages/core/src/shared/index.ts
  • packages/core/src/shared/useFocusGuards.test.ts
  • packages/core/src/shared/useFocusGuards.ts

📝 Walkthrough

Walkthrough

Changes

Shadow DOM support

Layer / File(s) Summary
Root-scoped focus guards
packages/core/src/shared/getRootNode.ts, packages/core/src/shared/index.ts, packages/core/src/shared/useFocusGuards.ts, packages/core/src/shared/useFocusGuards.test.ts
Focus guards and containment checks now support document and shadow-root scopes with per-root reference counting and cleanup.
Shadow-aware FocusScope trapping
packages/core/src/FocusScope/FocusScope.vue, packages/core/src/FocusScope/FocusScope.test.ts
FocusScope listens on the appropriate root, handles composed focus targets, uses the owning document for fallback focus, and adds Shadow DOM coverage.
Component focus and event wiring
packages/core/src/{Combobox,Menu,Popover,Select,ScrollArea}/...
Interactive components scope focus guards, outside-event detection, scrolling, and selection behavior to their rendered element or owning document.
Style injection and guidance
packages/core/src/Splitter/utils/style.ts, CONTRIBUTING.md
Splitter cursor styles use shared style injection, and documentation describes Teleport configuration and known Shadow DOM limitations.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related issues

  • feat: full Shadow DOM support #2725 — Covers the Shadow DOM support plan implemented across focus guards, focus scope, root-aware DOM access, scrolling, and related components.

Possibly related PRs

  • unovue/reka-ui#2475 — Updates useForwardExpose element tracking, which directly affects focus-guard scoping.
  • unovue/reka-ui#2546 — Also changes FocusScope focus preservation and related tests.

Suggested labels: v2

Suggested reviewers: epr3

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main shadow DOM focus-safety changes to FocusScope, focus guards, and retarget-safe listeners.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 2725-shadow-dom-part2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 12, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/reka-ui@2794

commit: e8a1f99

@zernonia

Copy link
Copy Markdown
Member Author

@CodeRabbit review

@coderabbitai

coderabbitai Bot commented Jul 12, 2026 •

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@zernonia
zernonia merged commit b37e46c into v3 Jul 13, 2026
8 checks passed
zernonia added a commit that referenced this pull request Jul 18, 2026
…2796)

* feat(Menu): headless composables for the Menu overlay family

Converts the core Menu parts to compose a useMenu.ts module, validating the overlay
archetype for #2723 (after Switch + Tabs). Public API unchanged.

- useMenuRoot() — state-only overlay root: returns { open, onOpenChange, onClose } +
  the MenuContext/MenuRootContext objects the SFC provides. No PartSurface (the root
  renders no attrs). MenuRoot.vue composes it.
- getMenuItemBaseSurface() — the attr-bearing item render surface (role/aria/highlight
  handlers). A context-SCOPED factory (creates per-instance isFocused, needs the item
  element) — the Tabs pure-builder axiom does not hold for overlays. MenuItemImpl.vue.
- getMenuItemSelectSurface() — the close-on-select protocol via an onSelect callback
  channel (the CustomEvent token, never a merged DOM listener). MenuItem.vue.
- useMenuContent() — the overlay brain: typeahead, arrow-nav, the pointer-grace
  machine, highlightedElement ownership. Returns the role=menu surface + the content
  context; the four component wrappers and the two mount side-effects stay in the SFC.
  Cuts MenuContentImpl.vue from 405 to 191 lines; the only un-extractable seam is
  RovingFocusGroup.getItems() (a template-instance ref), injected as a getItems option.

Surfaces bind via mergeProps so consumer listeners chain; functional data-* selectors
(data-reka-menu-content) live in props, not stateToDataAttrs. useFocusGuards stays
scoped to the content root (#2794).

Menu + DropdownMenu + ContextMenu + Menubar 56/56 green; full suite 2075 green;
type-check clean; 19 useMenu unit tests.

Refs #2723

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* docs(Menu): overlay-family findings + recipe overlay section

Refs #2723

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant