@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
20 lines (17 loc) • 554 B
JavaScript
import { useEffect } from "react";
const useClickOutside = (ref, handler) => {
useEffect(() => {
const handleClose = event => {
if (ref.current && !ref.current.contains(event.target)) {
handler(event);
}
};
window.addEventListener("mousedown", handleClose);
window.addEventListener("touchstart", handleClose);
return () => {
window.removeEventListener("mousedown", handleClose);
window.removeEventListener("touchstart", handleClose);
};
}, [handler, ref]);
};
export default useClickOutside;