@@ -219,15 +219,16 @@ export function useModalDismissSignal(
219
219
return ( ) => { } ;
220
220
}
221
221
222
- const handleDocumentKeyDown = ( event : any ) => {
222
+ const handleRootNodeKeyDown = ( event : KeyboardEvent ) => {
223
223
if ( event . key === 'Escape' ) {
224
224
dismissCallback ( ) ;
225
225
}
226
226
} ;
227
227
228
- const handleDocumentClick = ( event : any ) => {
228
+ const handleRootNodeClick : MouseEventHandler = event => {
229
229
if (
230
230
modalRef . current !== null &&
231
+ event . target instanceof Node &&
231
232
! modalRef . current . contains ( event . target )
232
233
) {
233
234
event . stopPropagation ( ) ;
@@ -237,7 +238,7 @@ export function useModalDismissSignal(
237
238
}
238
239
} ;
239
240
240
- let ownerDocument = null ;
241
+ let modalRootNode = null ;
241
242
242
243
// Delay until after the current call stack is empty,
243
244
// in case this effect is being run while an event is currently bubbling.
@@ -248,12 +249,12 @@ export function useModalDismissSignal(
248
249
// It's important to listen to the ownerDocument to support the browser extension.
249
250
// Here we use portals to render individual tabs (e.g. Profiler),
250
251
// and the root document might belong to a different window.
251
- const div = modalRef . current ;
252
- if ( div != null ) {
253
- ownerDocument = div . ownerDocument ;
254
- ownerDocument . addEventListener ( 'keydown' , handleDocumentKeyDown ) ;
252
+ const modalDOMElement = modalRef . current ;
253
+ if ( modalDOMElement != null ) {
254
+ modalRootNode = modalDOMElement . getRootNode ( ) ;
255
+ modalRootNode . addEventListener ( 'keydown' , handleRootNodeKeyDown ) ;
255
256
if ( dismissOnClickOutside ) {
256
- ownerDocument . addEventListener ( 'click' , handleDocumentClick , true ) ;
257
+ modalRootNode . addEventListener ( 'click' , handleRootNodeClick , true ) ;
257
258
}
258
259
}
259
260
} , 0 ) ;
@@ -263,9 +264,9 @@ export function useModalDismissSignal(
263
264
clearTimeout ( timeoutID ) ;
264
265
}
265
266
266
- if ( ownerDocument !== null ) {
267
- ownerDocument . removeEventListener ( 'keydown' , handleDocumentKeyDown ) ;
268
- ownerDocument . removeEventListener ( 'click' , handleDocumentClick , true ) ;
267
+ if ( modalRootNode !== null ) {
268
+ modalRootNode . removeEventListener ( 'keydown' , handleRootNodeKeyDown ) ;
269
+ modalRootNode . removeEventListener ( 'click' , handleRootNodeClick , true ) ;
269
270
}
270
271
} ;
271
272
} , [ modalRef , dismissCallback , dismissOnClickOutside ] ) ;
0 commit comments