'use strict';

exports.__esModule = true;

var _react = require('react');

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _helper = require('./Shared/helper.js');

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

//component - CoreUI / CPortal

var CPortal = function CPortal(props) {

  var fields = (0, _react.useRef)({ defaultNode: null }).current;

  // effect

  (0, _react.useEffect)(function () {
    return function cleanup() {
      if (fields.defaultNode) {
        document.body.removeChild(fields.defaultNode);
      }
      fields.defaultNode = null;
    };
  }, [fields.defaultNode]);

  // render

  if (!_helper.canUseDOM) {
    return null;
  }

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

  return _reactDom2.default.createPortal(props.children, props.node || fields.defaultNode);
};

CPortal.propTypes = {
  children: _propTypes2.default.node.isRequired,
  //
  node: _propTypes2.default.any
};

exports.default = CPortal;
module.exports = exports['default'];