-
Notifications
You must be signed in to change notification settings - Fork 95
/
index.ts
51 lines (44 loc) · 1.4 KB
/
index.ts
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
import {type MutableRefObject, type RefObject, useEffect} from 'react';
import {useSyncedRef} from '../useSyncedRef/index.js';
import {off, on} from '../util/misc.js';
const DEFAULT_EVENTS = ['mousedown', 'touchstart'];
/**
* Triggers a callback when the user clicks outside a target element.
*
* @param ref React ref object containing the target HTML element.
* @param callback Callback invoked when the user clicks outside the target element.
* @param events List of events that will be used as triggers for the outside click. Default:
* 'mousedown', 'touchstart'
*/
export function useClickOutside<T extends HTMLElement>(
ref: RefObject<T> | MutableRefObject<T>,
callback: EventListener,
events: string[] = DEFAULT_EVENTS,
): void {
const cbRef = useSyncedRef(callback);
const refRef = useSyncedRef(ref);
useEffect(() => {
function handler(this: HTMLElement, event: Event) {
if (!refRef.current.current) {
return;
}
const {target: evtTarget} = event;
const cb = cbRef.current;
if (
!evtTarget ||
(Boolean(evtTarget) && !refRef.current.current.contains(evtTarget as Node))
) {
cb.call(this, event);
}
}
for (const name of events) {
on(document, name, handler, {passive: true});
}
return () => {
for (const name of events) {
off(document, name, handler, {passive: true});
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [...events]);
}