All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.react-utils.useOnClickOutside.useOnClickOutside.js Maven / Gradle / Ivy

Go to download

A package encapsulating common code across neeto projects including initializers, utility functions, common components and hooks and so on.

There is a newer version: 4.12.3
Show newest version
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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy