package.cjs.react-utils.useOnClickOutside.useOnClickOutside.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neeto-commons-frontend Show documentation
Show all versions of neeto-commons-frontend Show documentation
A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = require("react");
var useOnClickOutside = function useOnClickOutside(ref, handler) {
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref$enabled = _ref.enabled,
enabled = _ref$enabled === void 0 ? true : _ref$enabled;
(0, _react.useEffect)(function () {
var listener = function listener(event) {
// Do nothing if clicking ref's element or descendent elements
if (!ref.current || ref.current.contains(event.target)) {
return;
}
handler(event);
};
if (enabled) {
document.addEventListener("mousedown", listener);
document.addEventListener("touchstart", listener);
}
return function () {
document.removeEventListener("mousedown", listener);
document.removeEventListener("touchstart", listener);
};
}, [handler, enabled]);
};
var _default = useOnClickOutside;
exports["default"] = _default;
//# sourceMappingURL=useOnClickOutside.js.map