'use strict';

var React = require('react');
var _rollupPluginBabelHelpers = require('./_rollupPluginBabelHelpers-988dd35a.js');
var _commonjsHelpers = require('./_commonjsHelpers-aa8da8cd.js');
var index = require('./index-2472d70b.js');
var ReactDOM = require('react-dom');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var ReactDOM__default = /*#__PURE__*/_interopDefaultLegacy(ReactDOM);

var utils = _commonjsHelpers.createCommonjsModule(function (module, exports) {

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  var canUseDOM = exports.canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
});

var Portal_1 = _commonjsHelpers.createCommonjsModule(function (module, exports) {

  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _createClass = function () {
    function defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    return function (Constructor, protoProps, staticProps) {
      if (protoProps) defineProperties(Constructor.prototype, protoProps);
      if (staticProps) defineProperties(Constructor, staticProps);
      return Constructor;
    };
  }();

  var _react2 = _interopRequireDefault(React__default['default']);

  var _propTypes2 = _interopRequireDefault(index.propTypes);

  var _reactDom2 = _interopRequireDefault(ReactDOM__default['default']);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      "default": obj
    };
  }

  function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  }

  function _possibleConstructorReturn(self, call) {
    if (!self) {
      throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }

    return call && (_rollupPluginBabelHelpers._typeof(call) === "object" || typeof call === "function") ? call : self;
  }

  function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
      throw new TypeError("Super expression must either be null or a function, not " + _rollupPluginBabelHelpers._typeof(superClass));
    }

    subClass.prototype = Object.create(superClass && superClass.prototype, {
      constructor: {
        value: subClass,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });
    if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  }

  var Portal = function (_React$Component) {
    _inherits(Portal, _React$Component);

    function Portal() {
      _classCallCheck(this, Portal);

      return _possibleConstructorReturn(this, (Portal.__proto__ || Object.getPrototypeOf(Portal)).apply(this, arguments));
    }

    _createClass(Portal, [{
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        if (this.defaultNode) {
          document.body.removeChild(this.defaultNode);
        }

        this.defaultNode = null;
      }
    }, {
      key: 'render',
      value: function render() {
        if (!utils.canUseDOM) {
          return null;
        }

        if (!this.props.node && !this.defaultNode) {
          this.defaultNode = document.createElement('div');
          document.body.appendChild(this.defaultNode);
        }

        return _reactDom2["default"].createPortal(this.props.children, this.props.node || this.defaultNode);
      }
    }]);

    return Portal;
  }(_react2["default"].Component);

  Portal.propTypes = {
    children: _propTypes2["default"].node.isRequired,
    node: _propTypes2["default"].any
  };
  exports["default"] = Portal;
});

var LegacyPortal = _commonjsHelpers.createCommonjsModule(function (module, exports) {

  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _createClass = function () {
    function defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    return function (Constructor, protoProps, staticProps) {
      if (protoProps) defineProperties(Constructor.prototype, protoProps);
      if (staticProps) defineProperties(Constructor, staticProps);
      return Constructor;
    };
  }();

  var _react2 = _interopRequireDefault(React__default['default']);

  var _reactDom2 = _interopRequireDefault(ReactDOM__default['default']);

  var _propTypes2 = _interopRequireDefault(index.propTypes);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      "default": obj
    };
  }

  function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  }

  function _possibleConstructorReturn(self, call) {
    if (!self) {
      throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }

    return call && (_rollupPluginBabelHelpers._typeof(call) === "object" || typeof call === "function") ? call : self;
  }

  function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
      throw new TypeError("Super expression must either be null or a function, not " + _rollupPluginBabelHelpers._typeof(superClass));
    }

    subClass.prototype = Object.create(superClass && superClass.prototype, {
      constructor: {
        value: subClass,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });
    if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  } // This file is a fallback for a consumer who is not yet on React 16
  // as createPortal was introduced in React 16


  var Portal = function (_React$Component) {
    _inherits(Portal, _React$Component);

    function Portal() {
      _classCallCheck(this, Portal);

      return _possibleConstructorReturn(this, (Portal.__proto__ || Object.getPrototypeOf(Portal)).apply(this, arguments));
    }

    _createClass(Portal, [{
      key: 'componentDidMount',
      value: function componentDidMount() {
        this.renderPortal();
      }
    }, {
      key: 'componentDidUpdate',
      value: function componentDidUpdate(props) {
        this.renderPortal();
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        _reactDom2["default"].unmountComponentAtNode(this.defaultNode || this.props.node);

        if (this.defaultNode) {
          document.body.removeChild(this.defaultNode);
        }

        this.defaultNode = null;
        this.portal = null;
      }
    }, {
      key: 'renderPortal',
      value: function renderPortal(props) {
        if (!this.props.node && !this.defaultNode) {
          this.defaultNode = document.createElement('div');
          document.body.appendChild(this.defaultNode);
        }

        var children = this.props.children; // https://gist.github.com/jimfb/d99e0678e9da715ccf6454961ef04d1b

        if (typeof this.props.children.type === 'function') {
          children = _react2["default"].cloneElement(this.props.children);
        }

        this.portal = _reactDom2["default"].unstable_renderSubtreeIntoContainer(this, children, this.props.node || this.defaultNode);
      }
    }, {
      key: 'render',
      value: function render() {
        return null;
      }
    }]);

    return Portal;
  }(_react2["default"].Component);

  exports["default"] = Portal;
  Portal.propTypes = {
    children: _propTypes2["default"].node.isRequired,
    node: _propTypes2["default"].any
  };
});

var PortalCompat = _commonjsHelpers.createCommonjsModule(function (module, exports) {

  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _reactDom2 = _interopRequireDefault(ReactDOM__default['default']);

  var _Portal2 = _interopRequireDefault(Portal_1);

  var _LegacyPortal2 = _interopRequireDefault(LegacyPortal);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      "default": obj
    };
  }

  var Portal = void 0;

  if (_reactDom2["default"].createPortal) {
    Portal = _Portal2["default"];
  } else {
    Portal = _LegacyPortal2["default"];
  }

  exports["default"] = Portal;
});

var PortalWithState_1 = _commonjsHelpers.createCommonjsModule(function (module, exports) {

  Object.defineProperty(exports, "__esModule", {
    value: true
  });

  var _createClass = function () {
    function defineProperties(target, props) {
      for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
      }
    }

    return function (Constructor, protoProps, staticProps) {
      if (protoProps) defineProperties(Constructor.prototype, protoProps);
      if (staticProps) defineProperties(Constructor, staticProps);
      return Constructor;
    };
  }();

  var _react2 = _interopRequireDefault(React__default['default']);

  var _propTypes2 = _interopRequireDefault(index.propTypes);

  var _PortalCompat2 = _interopRequireDefault(PortalCompat);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      "default": obj
    };
  }

  function _classCallCheck(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
      throw new TypeError("Cannot call a class as a function");
    }
  }

  function _possibleConstructorReturn(self, call) {
    if (!self) {
      throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
    }

    return call && (_rollupPluginBabelHelpers._typeof(call) === "object" || typeof call === "function") ? call : self;
  }

  function _inherits(subClass, superClass) {
    if (typeof superClass !== "function" && superClass !== null) {
      throw new TypeError("Super expression must either be null or a function, not " + _rollupPluginBabelHelpers._typeof(superClass));
    }

    subClass.prototype = Object.create(superClass && superClass.prototype, {
      constructor: {
        value: subClass,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });
    if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  }

  var KEYCODES = {
    ESCAPE: 27
  };

  var PortalWithState = function (_React$Component) {
    _inherits(PortalWithState, _React$Component);

    function PortalWithState(props) {
      _classCallCheck(this, PortalWithState);

      var _this = _possibleConstructorReturn(this, (PortalWithState.__proto__ || Object.getPrototypeOf(PortalWithState)).call(this, props));

      _this.portalNode = null;
      _this.state = {
        active: !!props.defaultOpen
      };
      _this.openPortal = _this.openPortal.bind(_this);
      _this.closePortal = _this.closePortal.bind(_this);
      _this.wrapWithPortal = _this.wrapWithPortal.bind(_this);
      _this.handleOutsideMouseClick = _this.handleOutsideMouseClick.bind(_this);
      _this.handleKeydown = _this.handleKeydown.bind(_this);
      return _this;
    }

    _createClass(PortalWithState, [{
      key: 'componentDidMount',
      value: function componentDidMount() {
        if (this.props.closeOnEsc) {
          document.addEventListener('keydown', this.handleKeydown);
        }

        if (this.props.closeOnOutsideClick) {
          document.addEventListener('click', this.handleOutsideMouseClick);
        }
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        if (this.props.closeOnEsc) {
          document.removeEventListener('keydown', this.handleKeydown);
        }

        if (this.props.closeOnOutsideClick) {
          document.removeEventListener('click', this.handleOutsideMouseClick);
        }
      }
    }, {
      key: 'openPortal',
      value: function openPortal(e) {
        if (this.state.active) {
          return;
        }

        if (e && e.nativeEvent) {
          e.nativeEvent.stopImmediatePropagation();
        }

        this.setState({
          active: true
        }, this.props.onOpen);
      }
    }, {
      key: 'closePortal',
      value: function closePortal() {
        if (!this.state.active) {
          return;
        }

        this.setState({
          active: false
        }, this.props.onClose);
      }
    }, {
      key: 'wrapWithPortal',
      value: function wrapWithPortal(children) {
        var _this2 = this;

        if (!this.state.active) {
          return null;
        }

        return _react2["default"].createElement(_PortalCompat2["default"], {
          node: this.props.node,
          key: 'react-portal',
          ref: function ref(portalNode) {
            return _this2.portalNode = portalNode;
          }
        }, children);
      }
    }, {
      key: 'handleOutsideMouseClick',
      value: function handleOutsideMouseClick(e) {
        if (!this.state.active) {
          return;
        }

        var root = this.portalNode && (this.portalNode.props.node || this.portalNode.defaultNode);

        if (!root || root.contains(e.target) || e.button && e.button !== 0) {
          return;
        }

        this.closePortal();
      }
    }, {
      key: 'handleKeydown',
      value: function handleKeydown(e) {
        if (e.keyCode === KEYCODES.ESCAPE && this.state.active) {
          this.closePortal();
        }
      }
    }, {
      key: 'render',
      value: function render() {
        return this.props.children({
          openPortal: this.openPortal,
          closePortal: this.closePortal,
          portal: this.wrapWithPortal,
          isOpen: this.state.active
        });
      }
    }]);

    return PortalWithState;
  }(_react2["default"].Component);

  PortalWithState.propTypes = {
    children: _propTypes2["default"].func.isRequired,
    defaultOpen: _propTypes2["default"].bool,
    node: _propTypes2["default"].any,
    closeOnEsc: _propTypes2["default"].bool,
    closeOnOutsideClick: _propTypes2["default"].bool,
    onOpen: _propTypes2["default"].func,
    onClose: _propTypes2["default"].func
  };
  PortalWithState.defaultProps = {
    onOpen: function onOpen() {},
    onClose: function onClose() {}
  };
  exports["default"] = PortalWithState;
});

var lib = _commonjsHelpers.createCommonjsModule(function (module, exports) {

  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.PortalWithState = exports.Portal = undefined;

  var _PortalCompat2 = _interopRequireDefault(PortalCompat);

  var _PortalWithState2 = _interopRequireDefault(PortalWithState_1);

  function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
      "default": obj
    };
  }

  exports.Portal = _PortalCompat2["default"];
  exports.PortalWithState = _PortalWithState2["default"];
});

exports.lib = lib;
//# sourceMappingURL=index-7e39b598.js.map
