'use strict';

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

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var ReactDOM = require('react-dom');
var useIsomorphicLayoutEffect = require('../../hooks/useIsomorphicLayoutEffect.js');

// Полиморфная версия портала для рендера как на сервере, так и на клиенте.
// Позволяет избежать ошибок, связанных с гидрацией.
var ClientOnlyPortal = function ClientOnlyPortal(_ref) {
  var children = _ref.children,
    providerFrame = _ref.providerFrame;
  var ref = React.useRef(null);
  var _useState = React.useState(false),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    mounted = _useState2[0],
    setMounted = _useState2[1];
  useIsomorphicLayoutEffect.useIsomorphicLayoutEffect(function () {
    setMounted(true);
    if (typeof providerFrame !== 'string' && providerFrame && providerFrame.current) {
      ref.current = providerFrame.current;
      return;
    }
    if (typeof providerFrame === 'string' && providerFrame !== 'document') {
      var containerElement = document.getElementById(providerFrame);
      if (!containerElement) {
        ref.current = document.body;
      }
      ref.current = containerElement;
      return;
    }
    ref.current = document.body;
  }, []);
  return mounted && ref.current ? /*#__PURE__*/ReactDOM.createPortal(children, ref.current) : null;
};

exports.ClientOnlyPortal = ClientOnlyPortal;
