Skip to content

Commit

Permalink
use ownerDocument to get ref to parent document (#746) (#751)
Browse files Browse the repository at this point in the history
* use ownerDocument to get ref to parent document (746)

* ref is defined (#746)
  • Loading branch information
Viktor-the-great authored and taion committed Dec 10, 2019
1 parent 3db0db2 commit cddc418
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/useRootClose.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { useCallback, useEffect, useRef } from 'react';
import useEventCallback from '@restart/hooks/useEventCallback';
import warning from 'warning';

import ownerDocument from './utils/ownerDocument';

const escapeKeyCode = 27;
const noop = () => {};

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

const doc = ownerDocument(ref.current);

// Use capture for this listener so it fires before React's listener, to
// avoid false positives in the contains() check below if the target DOM
// element is removed in the React mouse callback.
const removeMouseCaptureListener = listen(
document,
doc,
clickTrigger,
handleMouseCapture,
true,
);

const removeMouseListener = listen(document, clickTrigger, handleMouse);
const removeKeyupListener = listen(document, 'keyup', handleKeyUp);
const removeMouseListener = listen(doc, clickTrigger, handleMouse);
const removeKeyupListener = listen(doc, 'keyup', handleKeyUp);

let mobileSafariHackListeners = [];
if ('ontouchstart' in document.documentElement) {
if ('ontouchstart' in doc.documentElement) {
mobileSafariHackListeners = [].slice
.call(document.body.children)
.call(doc.body.children)
.map(el => listen(el, 'mousemove', noop));
}

Expand Down
2 changes: 1 addition & 1 deletion test/useRootCloseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import useRootClose from '../src/useRootClose';

const escapeKeyCode = 27;

describe('RootCloseWrapper', () => {
describe('useRootClose', () => {
let attachTo;

beforeEach(() => {
Expand Down

0 comments on commit cddc418

Please sign in to comment.