Repository navigation
fix: resolve interact-outside targets across shadow boundaries - #722
Conversation
`createInteractOutside` listens on the document, so `event.target` is retargeted to the outermost shadow host for any interaction originating inside a shadow tree. A layer rendered in a shadow root therefore saw each of its own clicks as the host — an ancestor, not a descendant — and `contains(ref(), target)` was false, so the layer dismissed itself on pointerdown. Resolve the target via `composedPath()[0]` and walk shadow boundaries when testing containment and matching ancestors, so the checks answer questions about the rendered page rather than one node tree. This also hands the real element to `shouldExcludeElement`, which is what makes `excludedElements` (the trigger, nested layers) work in a shadow root. Closes kobaltedev#445 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
✅ Deploy Preview for kobalte ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
The primitive-level tests pin the retargeting behaviour, but kobaltedev#445 is reported against `Select`, whose reopen loop runs through `DismissableLayer`'s `excludedElements` — the trigger is excluded, yet a retargeted event reports the shadow host, which matches neither the exclusion list nor the layer, so the layer dismissed itself. Cover that path directly: a layer and its excluded trigger inside a shadow root. Both "should not dismiss" cases fail without the fix; the third asserts a genuine outside click still dismisses, so the fix cannot over-correct into a layer that never closes. DismissableLayer is shared by Select, Combobox, Popover, Menu and Dialog, so this covers the reported component and its siblings at once. The `Select` suite itself is `describe.skip`ped in this repo, so an equivalent test there would not run. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Worth flagging for the v2 line: const target = e.target;
if (!ownerDoc.contains(target)) return false;
if (el.contains(target)) return false;I checked that package's Happy to port the same fix to |
|
Looks good thanks for the fix!
Would be appreciated! |

Problem
Closes #445.
createInteractOutsideregisters itspointerdown/focusinlisteners on the document. Events crossing a shadow boundary are retargeted, so by the time they reach a document listenerevent.targetis the outermost shadow host, not the element that was actually interacted with.That breaks every containment check in the primitive. A layer rendered inside a shadow root sees each of its own clicks reported as the host — which is an ancestor of the layer, not a descendant — so
contains(ref(), target)isfalseand the layer treats its own content as "outside". It dismisses onpointerdown, the content unmounts, andpointerupnever lands on anything. The symptom is a menu that closes when you click an item, without the item ever activating.This is the root cause @derekrjones identified in #445:
Fix
Three small composed-tree helpers, used by
createInteractOutside:getEventTarget(event)—composedPath()[0]instead ofevent.target. For an event that crosses no shadow boundary the two are identical, so this is a no-op outside shadow DOM.containsComposed(parent, child)— likecontains, but continues from the host when the walk reaches the top of a shadow tree. Needed becauseNode.prototype.containsonly walks one node tree; evendocument.contains(elementInAShadowRoot)isfalse.closestComposed(element, selector)— same idea forclosest, so thedata-top-layerlookup still finds an ancestor in an outer tree.Because
isEventOutsidenow resolves the real target before callingshouldExcludeElement, this also fixes theexcludedElementspath inDismissableLayer— the trigger and nested layers — which is the specific mechanism behind the Select reopen loop.Tests
At the primitive level (
create-interact-outside), three cases:mainmainmain, included as a regression guard so the fix can't over-correct into never dismissingAt the
DismissableLayerlevel, three more — this is the path #445 actually reports, with the trigger passed viaexcludedElements:main, and this is precisely the Select reopen loopmainmain, regression guardI added the second group because the primitive-level tests pin the retargeting behaviour but don't demonstrate the reported symptom.
DismissableLayeris shared by Select, Combobox, Popover, Menu and Dialog, so covering it exercises the reported component and its siblings at once. A test againstSelectdirectly wasn't an option — that suite isdescribe.skipped in this repo (and currently fails 42/57 if unskipped, unrelated to this change).createPointerEventin@kobalte/testsneeded a small change to support these: it built the event and thenObject.assign-ed the options onto it, butbubbles,cancelableandcomposedare read-only onEvent, so they have to go through the constructor.composed: trueis essential here — without it the event never crosses the shadow boundary at all. Defaults are unchanged (bubbles: true,cancelable: true,composed: false) and no existing caller passes any of the three, so every current use is unaffected.One thing I'd like your call on: where these helpers live
I put them in
packages/core/src/primitives/create-interact-outside/utils.ts, but they're generic DOM utilities andpackages/utils/src/dom.ts— right next tocontains— is arguably their proper home. I started there and moved them, for a reason worth flagging:packages/coredepends on"@kobalte/utils": "^0.9.2", a registry range rather thanworkspace:*, and the committed lockfile resolves it to the published tarball. pnpm 11 also no longer readslink-workspace-packagesfrom.npmrc(that setting moved topnpm-workspace.yaml, where it isn't set). Sopackages/corebuilds and tests against the published@kobalte/utils, not the one in this repo. Anything added topackages/utilsis invisible to core until utils is released — putting the helpers there made the wholecreate-interact-outsidetest file fail withgetEventTarget is not a function.That looked like it might be unintentional, so I kept this PR to a single package rather than touching dependency wiring. But if you'd prefer the helpers in
packages/utilsI'm glad to redo it that way — it'd mean switching core toworkspace:^(and a changeset covering both packages), which is a bigger change than a bug fix should smuggle in, especially with@kobalte/[email protected]out there. Happy to split that into its own PR instead if it's something you want fixed.Verification
create-interact-outside+dismissable-layersuites: 25/25 pass; the four new "should not" tests all fail against the parent commit@kobalte/coresuite otherwise unchanged (tabs,number-fieldandtoggle-groupflake intermittently onmainindependently of this PR — which is presumably why CI already wrapspnpm testinnick-fields/retry)biome checkcleanpnpm -F @kobalte/core buildsucceeds