-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseOutsideClick.tsx
34 lines (32 loc) · 984 Bytes
/
useOutsideClick.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
import { RefObject, useEffect } from "react";
/**
* Hook that alerts clicks outside of the passed ref
* @param ref - The ref of the target element to detect outside click
* @param callback - The callback function to invoke when there is a click outside of the element
*/
export const useOutsideClick = (
ref: RefObject<HTMLElement>,
callback?: () => void
) => {
useEffect(() => {
// If there is no callback, return
if (callback) {
/**
* Invoke callback if there is a click on outside of element
* @param event MouseEvent
* @returns void
*/
const handleClickOutside = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
callback();
}
};
// Bind the event listener
document.addEventListener("mousedown", handleClickOutside);
return () => {
// Unbind the event listener on clean up
document.removeEventListener("mousedown", handleClickOutside);
};
}
}, [ref, callback]);
};