Skip to content

[popups] Fix useDismiss when component is portaled into shadow DOM - #4230

Merged
atomiks merged 2 commits into
mui:masterfrom
dmitri-gb:fix-dismiss-in-shadow-dom
Mar 2, 2026
Merged

atomiks merged 2 commits into
mui:masterfrom
dmitri-gb:fix-dismiss-in-shadow-dom

Conversation

@dmitri-gb

@dmitri-gb dmitri-gb commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

This fixes #3603

useDismiss expects the root ancestor of the "click-outside" target to contain a "marker" (an element matching [data-base-ui-inert]), otherwise the click will be ignored. For components that have backdrops (modal dialogs, modal menu popups etc) this means that the backdrop must also satisfy this condition. These components include such a marker via the InternalBackdrop component. However, useDismiss uses document.querySelectorAll to find all these markers, which means it will not find any that live inside the shadow DOM. Passing container={shadowRoot} to Dialog.Portal, Menu.Portal etc where shadowRoot is attached to a direct child of <body> makes this marker invisible to useDismiss.

As a result, modal dialogs, menu popups etc that are portaled into a top-level ShadowRoot cannot be dismissed by a "click-outside".

There are multiple ways how this could be fixed. In theory, shadow roots could be nested multiple levels deep etc, but for the backdrop click to work we only need to find the one marker that was rendered by the component that called useDismiss. So I went with the smallest possible change — only inspecting the direct shadow root of the click target and appending all markers in it to the existing markers collection. The contains check on line 367 already pierces shadow roots, so it will see the added markers.

@pkg-pr-new

pkg-pr-new Bot commented Mar 2, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/@base-ui/react@4230
    
    pnpm add https://pkg.pr.new/@base-ui/utils@4230
    

commit: 21d2547

@mui-bot

mui-bot commented Mar 2, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+106B(+0.02%) 🔺+37B(+0.03%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Mar 2, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 90acba4
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69a4f8b5a66aed0008ff1649
😎 Deploy Preview https://deploy-preview-4230--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dmitri-gb
dmitri-gb force-pushed the fix-dismiss-in-shadow-dom branch from 43b9e5c to ad38582 Compare March 2, 2026 02:34
@atomiks atomiks added type: bug It doesn't behave as expected. scope: all components Widespread work has an impact on almost all components. labels Mar 2, 2026
@atomiks

atomiks commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

InternalBackdrop manually sets data-base-ui-inert="" on its JSX, but the user-provided Backdrops don't. And I'm assuming this actually regressed after fixing #2940 because the user-provided Backdrop now renders on top of the InternalBackdrop.

Given this change, could we remove the data-base-ui-inert="" prop from InternalBackdrop?

@dmitri-gb

dmitri-gb commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Hmm, no, I don't think we can remove it. We actually rely on it. This is precisely the marker that is found in useDismiss. I don't think it matters which backdrop renders on top, it only matters that the marker (InternalBackdrop) exists in the same shadow root where the user backdrop (the click target) is.

@atomiks atomiks 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.

Thanks for the contribution @dmitri-gb!

@atomiks
atomiks merged commit 047eca4 into mui:master Mar 2, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: all components Widespread work has an impact on almost all components. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[menu] Content doesn't close on interaction outside of popup in ShadowRoot

3 participants