Skip to content

Commit 6fdfbae

Browse files
committed
fix[devtools/useModalDismissSignal]: use getRootNode for shadow root case support
1 parent 206934f commit 6fdfbae

File tree

1 file changed

+12
-11
lines changed
  • packages/react-devtools-shared/src/devtools/views

1 file changed

+12
-11
lines changed

packages/react-devtools-shared/src/devtools/views/hooks.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,16 @@ export function useModalDismissSignal(
219219
return () => {};
220220
}
221221

222-
const handleDocumentKeyDown = (event: any) => {
222+
const handleRootNodeKeyDown = (event: KeyboardEvent) => {
223223
if (event.key === 'Escape') {
224224
dismissCallback();
225225
}
226226
};
227227

228-
const handleDocumentClick = (event: any) => {
228+
const handleRootNodeClick: MouseEventHandler = event => {
229229
if (
230230
modalRef.current !== null &&
231+
event.target instanceof Node &&
231232
!modalRef.current.contains(event.target)
232233
) {
233234
event.stopPropagation();
@@ -237,7 +238,7 @@ export function useModalDismissSignal(
237238
}
238239
};
239240

240-
let ownerDocument = null;
241+
let modalRootNode = null;
241242

242243
// Delay until after the current call stack is empty,
243244
// in case this effect is being run while an event is currently bubbling.
@@ -248,12 +249,12 @@ export function useModalDismissSignal(
248249
// It's important to listen to the ownerDocument to support the browser extension.
249250
// Here we use portals to render individual tabs (e.g. Profiler),
250251
// 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);
255256
if (dismissOnClickOutside) {
256-
ownerDocument.addEventListener('click', handleDocumentClick, true);
257+
modalRootNode.addEventListener('click', handleRootNodeClick, true);
257258
}
258259
}
259260
}, 0);
@@ -263,9 +264,9 @@ export function useModalDismissSignal(
263264
clearTimeout(timeoutID);
264265
}
265266

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);
269270
}
270271
};
271272
}, [modalRef, dismissCallback, dismissOnClickOutside]);

0 commit comments

Comments
 (0)