@vimeo/iris
Version:
Vimeo Design System
28 lines (23 loc) • 1.07 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var React = require('react');
function useOutsideClick(refs, onClick, options) {
React.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]);
}
exports.useOutsideClick = useOutsideClick;