2022-12-19 14:43:43 +00:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
|
|
|
|
const useOutsideClickDetector = (ref: React.RefObject<HTMLElement>, callback: () => void) => {
|
|
|
|
const handleClick = (event: MouseEvent) => {
|
|
|
|
if (ref.current && !ref.current.contains(event.target as Node)) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-08-19 13:20:12 +00:00
|
|
|
document.addEventListener("mousedown", handleClick);
|
2022-12-19 14:43:43 +00:00
|
|
|
|
|
|
|
return () => {
|
2023-08-19 13:20:12 +00:00
|
|
|
document.removeEventListener("mousedown", handleClick);
|
2022-12-19 14:43:43 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useOutsideClickDetector;
|