@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.
28 lines (23 loc) • 697 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
const useClickOutside = (ref, handler) => {
(0, _react.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]);
};
var _default = useClickOutside;
exports.default = _default;