Skip to content

fix: resolve interact-outside targets across shadow boundaries - #722

Merged
jer3m01 merged 2 commits into
kobaltedev:mainfrom
Nandan108:fix/interact-outside-shadow-dom
Sep 7, 2026
Merged

jer3m01 merged 2 commits into
kobaltedev:mainfrom
Nandan108:fix/interact-outside-shadow-dom

Conversation

@Nandan108

@Nandan108 Nandan108 commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #445.

createInteractOutside registers its pointerdown / focusin listeners on the document. Events crossing a shadow boundary are retargeted, so by the time they reach a document listener event.target is 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) is false and the layer treats its own content as "outside". It dismisses on pointerdown, the content unmounts, and pointerup never 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:

Select (and many other components) have DismissableLayer with excludedElements={[context.triggerRef]}. When open, and mousedown on the trigger, it tests if the trigger contains the event target and aborts if so. But when clicking within the shadowdom, the event target is the shadow host not the trigger. So the popup is closed, and then trigger opens it again.

Fix

Three small composed-tree helpers, used by createInteractOutside:

  • getEventTarget(event) — composedPath()[0] instead of event.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) — like contains, but continues from the host when the walk reaches the top of a shadow tree. Needed because Node.prototype.contains only walks one node tree; even document.contains(elementInAShadowRoot) is false.
  • closestComposed(element, selector) — same idea for closest, so the data-top-layer lookup still finds an ancestor in an outer tree.

Because isEventOutside now resolves the real target before calling shouldExcludeElement, this also fixes the excludedElements path in DismissableLayer — 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:

  • clicking the layer's own content inside a shadow root must not trigger — fails on main
  • focus moving into the layer's own content inside a shadow root must not trigger — fails on main
  • clicking a sibling in the same shadow root must still trigger — passes on main, included as a regression guard so the fix can't over-correct into never dismissing

At the DismissableLayer level, three more — this is the path #445 actually reports, with the trigger passed via excludedElements:

  • pointerdown on the excluded trigger inside a shadow root must not dismiss — fails on main, and this is precisely the Select reopen loop
  • pointerdown on the layer's own content inside a shadow root must not dismiss — fails on main
  • pointerdown genuinely outside the layer must still dismiss — passes on main, regression guard

I added the second group because the primitive-level tests pin the retargeting behaviour but don't demonstrate the reported symptom. DismissableLayer is shared by Select, Combobox, Popover, Menu and Dialog, so covering it exercises the reported component and its siblings at once. A test against Select directly wasn't an option — that suite is describe.skipped in this repo (and currently fails 42/57 if unskipped, unrelated to this change).

createPointerEvent in @kobalte/tests needed a small change to support these: it built the event and then Object.assign-ed the options onto it, but bubbles, cancelable and composed are read-only on Event, so they have to go through the constructor. composed: true is 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 and packages/utils/src/dom.ts — right next to contains — is arguably their proper home. I started there and moved them, for a reason worth flagging:

packages/core depends on "@kobalte/utils": "^0.9.2", a registry range rather than workspace:*, and the committed lockfile resolves it to the published tarball. pnpm 11 also no longer reads link-workspace-packages from .npmrc (that setting moved to pnpm-workspace.yaml, where it isn't set). So packages/core builds and tests against the published @kobalte/utils, not the one in this repo. Anything added to packages/utils is invisible to core until utils is released — putting the helpers there made the whole create-interact-outside test file fail with getEventTarget 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/utils I'm glad to redo it that way — it'd mean switching core to workspace:^ (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-layer suites: 25/25 pass; the four new "should not" tests all fail against the parent commit
  • full @kobalte/core suite otherwise unchanged (tabs, number-field and toggle-group flake intermittently on main independently of this PR — which is presumably why CI already wraps pnpm test in nick-fields/retry)
  • biome check clean
  • pnpm -F @kobalte/core build succeeds

`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]>
@netlify

netlify Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for kobalte ready!

Name Link
🔨 Latest commit a5477b6
🔍 Latest deploy log https://app.netlify.com/projects/kobalte/deploys/6a8f17754782680008ad6275
😎 Deploy Preview https://deploy-preview-722--kobalte.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 83 (🟢 up 22 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: 70 (no change from production)
View the detailed breakdown and full score reports
🤖 Make changes Run an agent on this branch

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]>
@Nandan108

Copy link
Copy Markdown
Contributor Author

Worth flagging for the v2 line: solid2 drops this primitive in favour of interactOutside from @solid-primitives/interaction (catalog-pinned at 1.0.0-next.4), and makeInteractOutside there carries the same target resolution:

const target = e.target;
if (!ownerDoc.contains(target)) return false;
if (el.contains(target)) return false;

I checked that package's next branch source as well, so it's unfixed unreleased too, and I couldn't find an existing issue for it. So #445 should still reproduce once solid2 becomes 2.0 — this PR fixes the 0.13.x line only.

Happy to port the same fix to @solid-primitives/interaction if that's useful; it transfers almost verbatim.

@jer3m01

jer3m01 commented Sep 7, 2026

Copy link
Copy Markdown
Member

Looks good thanks for the fix!

Happy to port the same fix to @solid-primitives/interaction

Would be appreciated!

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.

Clicking on the Select.Trigger inside a shadow root causes the dropdown to reopen while the dropdown is visible

2 participants