"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Portal = void 0;
var _react = require("react");
var _reactDom = require("react-dom");
var _useSsr = _interopRequireDefault(require("use-ssr"));
var _utils = require("../../common/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Portal = exports.Portal = function Portal(_ref) {
  var children = _ref.children,
    attachTo = _ref.attachTo;
  var _useSSR = (0, _useSsr.default)(),
    isBrowser = _useSSR.isBrowser,
    isServer = _useSSR.isServer;
  var portal = (0, _react.useRef)(isBrowser && document.createElement('div'));
  (0, _react.useEffect)(function () {
    var elToMountTo = (0, _utils.isUndefined)(attachTo) ? document.body : attachTo;
    var portalNode = portal.current;
    if (isServer || !(0, _utils.isHTMLElement)(elToMountTo) || !(0, _utils.isHTMLElement)(portalNode)) {
      return;
    }
    elToMountTo.appendChild(portalNode);
    return function cleanup() {
      elToMountTo.removeChild(portalNode);
    };
  }, [attachTo, isServer]);
  return (0, _utils.isHTMLElement)(portal.current) ? (0, _reactDom.createPortal)(children, portal.current) : null;
};