"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.useClickOutsideHandler = useClickOutsideHandler;

var _react = require("react");

/**
 * Listens for mouse and touch events on the document and calls the provided
 * handler function as long as the event did not originate in one of the
 * elements corresponding to the React Refs in the `insideRefs` array. For
 * example, if you want to know if the user clicked outside of a dialog or
 * its trigger, you can pass it an array containing the refs to the dialog
 * and the trigger.
 *
 * @param insideRefs - Refs to elements that are considered "inside"
 * @param handler - called when the event target was outside the "inside" elements
 */
function useClickOutsideHandler(insideRefs, handler) {
  function handleClickOutside(event) {
    if (!insideRefs.some(function (ref) {
      var _ref$current;

      return (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.contains(event.target);
    })) {
      handler(event);
    }
  }

  (0, _react.useEffect)(function () {
    document.addEventListener('mousedown', handleClickOutside);
    document.addEventListener('touchstart', handleClickOutside);
    return function () {
      document.removeEventListener('mousedown', handleClickOutside);
      document.removeEventListener('touchstart', handleClickOutside);
    };
  }, [handleClickOutside]);
}

var _default = useClickOutsideHandler;
exports.default = _default;