package.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.
import { useEffect } from "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;
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]);
};
export default useOnClickOutside;
//# sourceMappingURL=useOnClickOutside.js.map