UNPKG

@vimeo/iris

Version:
24 lines (21 loc) 977 B
import { useEffect } from 'react'; function useOutsideClick(refs, onClick, options) { useEffect(function () { if (!onClick) return; function click(event) { var targeted = function (ref) { var _a; return (_a = ref === null || ref === void 0 ? void 0 : ref.current) === null || _a === void 0 ? void 0 : _a.contains(event.target); }; var outside = ![refs].flat().some(targeted); if (outside) onClick(event); } var capture = (options === null || options === void 0 ? void 0 : options.capture) || false; document.addEventListener('mousedown', click, capture); document.addEventListener('touchstart', click, capture); return function () { document.removeEventListener('mousedown', click, capture); document.removeEventListener('touchstart', click, capture); }; }, [refs, onClick, options]); } export { useOutsideClick };