Skip to content

Commit 10efaa9

Browse files
authored
Ensure the main tree and parent Dialog components are marked as inert (#2290)
* drop `@ts-expect-error`, because `inert` is available now * fix logical error We want to apply `inert` when we _don't_ have nested dialogs, because if we _do_ have nested dialogs, then the inert should be applied from the nested dialog (or visually the top most dialog). * update changelog * replace `useInertOthers` with `useInert` * add `assertInert` and `assertNotInert` accessibility assertion helpers * ensure the `main tree` root is marked as inert As well as the parent dialogs in case of nested dialogs.
1 parent 619d103 commit 10efaa9

File tree

14 files changed

+560
-869
lines changed

14 files changed

+560
-869
lines changed

packages/@headlessui-react/CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Ensure the main tree and parent `Dialog` components are marked as `inert` ([#2290](https://github.com/tailwindlabs/headlessui/pull/2290))
1113

1214
## [1.7.11] - 2023-02-15
1315

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

+26-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React, {
33
createContext,
44
createRef,
5+
useCallback,
56
useContext,
67
useEffect,
78
useMemo,
@@ -25,7 +26,6 @@ import { Keys } from '../keyboard'
2526
import { isDisabledReactIssue7711 } from '../../utils/bugs'
2627
import { useId } from '../../hooks/use-id'
2728
import { FocusTrap } from '../../components/focus-trap/focus-trap'
28-
import { useInertOthers } from '../../hooks/use-inert-others'
2929
import { Portal } from '../../components/portal/portal'
3030
import { ForcePortalRoot } from '../../internal/portal-force-root'
3131
import { Description, useDescriptions } from '../description/description'
@@ -38,6 +38,7 @@ import { useEventListener } from '../../hooks/use-event-listener'
3838
import { Hidden, Features as HiddenFeatures } from '../../internal/hidden'
3939
import { useEvent } from '../../hooks/use-event'
4040
import { useDocumentOverflowLockedEffect } from '../../hooks/document-overflow/use-document-overflow'
41+
import { useInert } from '../../hooks/use-inert'
4142

4243
enum DialogStates {
4344
Open,
@@ -216,11 +217,33 @@ let DialogRoot = forwardRefWithAs(function Dialog<
216217

217218
// Ensure other elements can't be interacted with
218219
let inertOthersEnabled = (() => {
219-
if (!hasNestedDialogs) return false
220+
// Nested dialogs should not modify the `inert` property, only the root one should.
221+
if (hasParentDialog) return false
220222
if (isClosing) return false
221223
return enabled
222224
})()
223-
useInertOthers(internalDialogRef, inertOthersEnabled)
225+
let resolveRootOfMainTreeNode = useCallback(() => {
226+
return (Array.from(ownerDocument?.querySelectorAll('body > *') ?? []).find((root) => {
227+
// Skip the portal root, we don't want to make that one inert
228+
if (root.id === 'headlessui-portal-root') return false
229+
230+
// Find the root of the main tree node
231+
return root.contains(mainTreeNode.current) && root instanceof HTMLElement
232+
}) ?? null) as HTMLElement | null
233+
}, [mainTreeNode])
234+
useInert(resolveRootOfMainTreeNode, inertOthersEnabled)
235+
236+
// This would mark the parent dialogs as inert
237+
let inertParentDialogs = (() => {
238+
if (hasNestedDialogs) return true
239+
return enabled
240+
})()
241+
let resolveRootOfParentDialog = useCallback(() => {
242+
return (Array.from(ownerDocument?.querySelectorAll('[data-headlessui-portal]') ?? []).find(
243+
(root) => root.contains(mainTreeNode.current) && root instanceof HTMLElement
244+
) ?? null) as HTMLElement | null
245+
}, [mainTreeNode])
246+
useInert(resolveRootOfParentDialog, inertParentDialogs)
224247

225248
let resolveContainers = useEvent(() => {
226249
// Third party roots

packages/@headlessui-react/src/hooks/use-inert-others.test.tsx

-295
This file was deleted.

0 commit comments

Comments
 (0)