Skip to content

Commit d8b263c

Browse files
authored
Fix shadow-root bug closing Dialog containers (#2217)
* ensure we consider `html > *` as valid containers as well * update changelog
1 parent 2f13496 commit d8b263c

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Fix crash when reading `headlessuiFocusGuard` of `relatedTarget` in the `FocusTrap` component ([#2203](https://github.com/tailwindlabs/headlessui/pull/2203))
2121
- Fix `FocusTrap` in `Dialog` when there is only 1 focusable element ([#2172](https://github.com/tailwindlabs/headlessui/pull/2172))
2222
- Improve `Tabs` wrapping around when controlling the component and overflowing the `selectedIndex` ([#2213](https://github.com/tailwindlabs/headlessui/pull/2213))
23+
- Fix `shadow-root` bug closing `Dialog` containers ([#2217](https://github.com/tailwindlabs/headlessui/pull/2217))
2324

2425
### Added
2526

packages/@headlessui-react/src/components/dialog/dialog.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ let DialogRoot = forwardRefWithAs(function Dialog<
317317
let resolveContainers = useEvent(() => {
318318
// Third party roots
319319
let rootContainers = Array.from(
320-
ownerDocument?.querySelectorAll('body > *, [data-headlessui-portal]') ?? []
320+
ownerDocument?.querySelectorAll('html > *, body > *, [data-headlessui-portal]') ?? []
321321
).filter((container) => {
322+
if (container === document.body) return false // Skip `<body>`
323+
if (container === document.head) return false // Skip `<head>`
322324
if (!(container instanceof HTMLElement)) return false // Skip non-HTMLElements
323325
if (container.contains(mainTreeNode.current)) return false // Skip if it is the main app
324326
if (state.panelRef.current && container.contains(state.panelRef.current)) return false

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Fix crash when reading `headlessuiFocusGuard` of `relatedTarget` in the `FocusTrap` component ([#2203](https://github.com/tailwindlabs/headlessui/pull/2203))
2222
- Fix `FocusTrap` in `Dialog` when there is only 1 focusable element ([#2172](https://github.com/tailwindlabs/headlessui/pull/2172))
2323
- Improve `Tabs` wrapping around when controlling the component and overflowing the `selectedIndex` ([#2213](https://github.com/tailwindlabs/headlessui/pull/2213))
24+
- Fix `shadow-root` bug closing `Dialog` containers ([#2217](https://github.com/tailwindlabs/headlessui/pull/2217))
2425

2526
### Added
2627

packages/@headlessui-vue/src/components/dialog/dialog.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ export let Dialog = defineComponent({
186186
function resolveAllowedContainers() {
187187
// Third party roots
188188
let rootContainers = Array.from(
189-
ownerDocument.value?.querySelectorAll('body > *, [data-headlessui-portal]') ?? []
189+
ownerDocument.value?.querySelectorAll('html > *, body > *, [data-headlessui-portal]') ?? []
190190
).filter((container) => {
191+
if (container === document.body) return false // Skip `<body>`
192+
if (container === document.head) return false // Skip `<head>`
191193
if (!(container instanceof HTMLElement)) return false // Skip non-HTMLElements
192194
if (container.contains(dom(mainTreeNode))) return false // Skip if it is the main app
193195
if (api.panelRef.value && container.contains(api.panelRef.value)) return false

0 commit comments

Comments
 (0)