-
Notifications
You must be signed in to change notification settings - Fork 2
/
useOnOutsideClick.tsx
99 lines (96 loc) · 3.46 KB
/
useOnOutsideClick.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Utilities
*/
import * as React from "react";
import { hasPointerEventsSupport } from "../../base/PointerEvents";
import { Timer } from "../Timer";
/** @public */
export type OutsideClickEvent = PointerEvent | MouseEvent | TouchEvent;
/** Invokes onOutsideClick handler when user clicks outside of referenced element.
* @public
*/
export function useOnOutsideClick<T extends Element>(
onOutsideClick?: () => void,
/** Invoked for intermediate events. Return `false` to prevent outside click. */
outsideEventPredicate?: (e: OutsideClickEvent) => boolean
) {
const handleMouseEvents = React.useRef(true);
const handleMouseEventsTimer = React.useRef(new Timer(1000));
const ref = React.useRef<T>(null);
const isDownOutside = React.useRef(false);
React.useEffect(() => {
const listener = (e: OutsideClickEvent) => {
if (e.type === "touchstart") {
// Skip mouse event handlers after touch event.
handleMouseEvents.current = false;
handleMouseEventsTimer.current.start();
} else if (e.type === "mousedown" && !handleMouseEvents.current) {
return;
}
const isOutsideEvent = !outsideEventPredicate || outsideEventPredicate(e);
isDownOutside.current =
!!ref.current &&
e.target instanceof Node &&
!ref.current.contains(e.target) &&
isOutsideEvent;
};
if (hasPointerEventsSupport()) {
document.addEventListener("pointerdown", listener);
} else {
document.addEventListener("mousedown", listener);
document.addEventListener("touchstart", listener);
}
return () => {
if (hasPointerEventsSupport()) {
document.removeEventListener("pointerdown", listener);
} else {
document.removeEventListener("mousedown", listener);
document.removeEventListener("touchstart", listener);
}
};
}, [outsideEventPredicate]);
React.useEffect(() => {
const listener = (e: OutsideClickEvent) => {
if (e.type === "mouseup" && !handleMouseEvents.current) {
return;
}
onOutsideClick &&
isDownOutside.current &&
(!outsideEventPredicate || outsideEventPredicate(e)) &&
ref.current &&
e.target instanceof Node &&
!ref.current.contains(e.target) &&
onOutsideClick();
isDownOutside.current = false;
};
if (hasPointerEventsSupport()) {
document.addEventListener("pointerup", listener);
} else {
document.addEventListener("mouseup", listener);
document.addEventListener("touchend", listener);
}
return () => {
if (hasPointerEventsSupport()) {
document.removeEventListener("pointerup", listener);
} else {
document.removeEventListener("mouseup", listener);
document.removeEventListener("touchend", listener);
}
};
}, [onOutsideClick, outsideEventPredicate]);
React.useEffect(() => {
const listener = () => {
handleMouseEvents.current = true;
};
const timer = handleMouseEventsTimer.current;
timer.setOnExecute(listener);
return () => {
timer.setOnExecute(undefined);
};
}, []);
return ref;
}