2
2
import React , {
3
3
createContext ,
4
4
createRef ,
5
+ useCallback ,
5
6
useContext ,
6
7
useEffect ,
7
8
useMemo ,
@@ -25,7 +26,6 @@ import { Keys } from '../keyboard'
25
26
import { isDisabledReactIssue7711 } from '../../utils/bugs'
26
27
import { useId } from '../../hooks/use-id'
27
28
import { FocusTrap } from '../../components/focus-trap/focus-trap'
28
- import { useInertOthers } from '../../hooks/use-inert-others'
29
29
import { Portal } from '../../components/portal/portal'
30
30
import { ForcePortalRoot } from '../../internal/portal-force-root'
31
31
import { Description , useDescriptions } from '../description/description'
@@ -38,6 +38,7 @@ import { useEventListener } from '../../hooks/use-event-listener'
38
38
import { Hidden , Features as HiddenFeatures } from '../../internal/hidden'
39
39
import { useEvent } from '../../hooks/use-event'
40
40
import { useDocumentOverflowLockedEffect } from '../../hooks/document-overflow/use-document-overflow'
41
+ import { useInert } from '../../hooks/use-inert'
41
42
42
43
enum DialogStates {
43
44
Open ,
@@ -216,11 +217,33 @@ let DialogRoot = forwardRefWithAs(function Dialog<
216
217
217
218
// Ensure other elements can't be interacted with
218
219
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
220
222
if ( isClosing ) return false
221
223
return enabled
222
224
} ) ( )
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 )
224
247
225
248
let resolveContainers = useEvent ( ( ) => {
226
249
// Third party roots
0 commit comments