Skip to content

Commit cddc418

Browse files
Viktor-the-greattaion
authored andcommitted
use ownerDocument to get ref to parent document (#746) (#751)
* use ownerDocument to get ref to parent document (746) * ref is defined (#746)
1 parent 3db0db2 commit cddc418

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/useRootClose.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { useCallback, useEffect, useRef } from 'react';
55
import useEventCallback from '@restart/hooks/useEventCallback';
66
import warning from 'warning';
77

8+
import ownerDocument from './utils/ownerDocument';
9+
810
const escapeKeyCode = 27;
911
const noop = () => {};
1012

@@ -69,23 +71,25 @@ function useRootClose(
6971
useEffect(() => {
7072
if (disabled || ref == null) return undefined;
7173

74+
const doc = ownerDocument(ref.current);
75+
7276
// Use capture for this listener so it fires before React's listener, to
7377
// avoid false positives in the contains() check below if the target DOM
7478
// element is removed in the React mouse callback.
7579
const removeMouseCaptureListener = listen(
76-
document,
80+
doc,
7781
clickTrigger,
7882
handleMouseCapture,
7983
true,
8084
);
8185

82-
const removeMouseListener = listen(document, clickTrigger, handleMouse);
83-
const removeKeyupListener = listen(document, 'keyup', handleKeyUp);
86+
const removeMouseListener = listen(doc, clickTrigger, handleMouse);
87+
const removeKeyupListener = listen(doc, 'keyup', handleKeyUp);
8488

8589
let mobileSafariHackListeners = [];
86-
if ('ontouchstart' in document.documentElement) {
90+
if ('ontouchstart' in doc.documentElement) {
8791
mobileSafariHackListeners = [].slice
88-
.call(document.body.children)
92+
.call(doc.body.children)
8993
.map(el => listen(el, 'mousemove', noop));
9094
}
9195

test/useRootCloseSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import useRootClose from '../src/useRootClose';
88

99
const escapeKeyCode = 27;
1010

11-
describe('RootCloseWrapper', () => {
11+
describe('useRootClose', () => {
1212
let attachTo;
1313

1414
beforeEach(() => {

0 commit comments

Comments
 (0)