UNPKG

@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) 602 B
import { useEffect } from "react"; var useClickOutside = function useClickOutside(ref, handler) { useEffect(function () { var handleClose = function handleClose(event) { if (ref.current && !ref.current.contains(event.target)) { handler(event); } }; window.addEventListener("mousedown", handleClose); window.addEventListener("touchstart", handleClose); return function () { window.removeEventListener("mousedown", handleClose); window.removeEventListener("touchstart", handleClose); }; }, [handler, ref]); }; export default useClickOutside;