Repository navigation
[popups] Fix useDismiss when component is portaled into shadow DOM - #4230
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
43b9e5c to
ad38582
Compare
|
Given this change, could we remove the |
|
Hmm, no, I don't think we can remove it. We actually rely on it. This is precisely the marker that is found in |
atomiks
left a comment
There was a problem hiding this comment.
Thanks for the contribution @dmitri-gb!
This fixes #3603
useDismissexpects 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 theInternalBackdropcomponent. However,useDismissusesdocument.querySelectorAllto find all these markers, which means it will not find any that live inside the shadow DOM. Passingcontainer={shadowRoot}toDialog.Portal,Menu.Portaletc whereshadowRootis attached to a direct child of<body>makes this marker invisible touseDismiss.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. Thecontainscheck on line 367 already pierces shadow roots, so it will see the added markers.