text: 'use strict';
  
  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 _react = require('react');
  
  var _react2 = _interopRequireDefault(_react);
  
  var _propTypes = require('prop-types');
  
  var _propTypes2 = _interopRequireDefault(_propTypes);
  
  var _reactDom = require('react-dom');
  
  var _reactDom2 = _interopRequireDefault(_reactDom);
  
  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 && (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 " + 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 noop = function noop() {
    return null;
  };
  
  var Travel = function (_Component) {
    _inherits(Travel, _Component);
  
    function Travel() {
      var _ref;
  
      var _temp, _this, _ret;
  
      _classCallCheck(this, Travel);
  
      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
  
      return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Travel.__proto__ || Object.getPrototypeOf(Travel)).call.apply(_ref, [this].concat(args))), _this), _this._portalNode = null, _temp), _possibleConstructorReturn(_this, _ret);
    }
  
    _createClass(Travel, [{
      key: 'componentDidMount',
      value: function componentDidMount() {
        this._renderPortal();
      }
    }, {
      key: 'componentDidUpdate',
      value: function componentDidUpdate() {
        this._renderPortal();
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        this._destroyPortal();
      }
    }, {
      key: '_getRenderToNode',
      value: function _getRenderToNode() {
        var renderTo = this.props.renderTo;
  
        if (typeof renderTo === 'string') {
          return document.querySelector(renderTo);
        } else {
          return renderTo || document.body;
        }
      }
    }, {
      key: '_getComponent',
      value: function _getComponent() {
        if (this.props.useArray) {
          return _react.Children.toArray(this.props.children)[1];
        } else {
          return _react.Children.only(this.props.children);
        }
      }
    }, {
      key: '_setupPortal',
      value: function _setupPortal() {
        var _props = this.props,
            renderTag = _props.renderTag,
            onMount = _props.onMount;
  
        var renderToNode = this._getRenderToNode();
  
        // create a node that we can stick our component in
        this._portalNode = document.createElement(renderTag);
  
        // append node to the render node
        renderToNode.appendChild(this._portalNode);
  
        // store the instance passed back to allow work to be done on it
        this._portalInstance = typeof onMount === 'function' ? onMount(this._portalNode) : this._portalNode;
      }
    }, {
      key: '_renderPortal',
      value: function _renderPortal() {
        var _this2 = this;
  
        var component = this._getComponent();
  
        // if no component, bail out
        if (!component) {
          this._destroyPortal();
          return;
        }
  
        // if no portalNode found, create it
        if (!this._portalNode) {
          this._setupPortal();
        }
  
        // render component into the DOM
        _reactDom2.default.unstable_renderSubtreeIntoContainer(this, component, this._portalNode, function () {
          // don't update until the subtree has finished rendering
          _this2._updatePortal();
        });
      }
    }, {
      key: '_updatePortal',
      value: function _updatePortal() {
        var _this3 = this;
  
        var _props2 = this.props,
            id = _props2.id,
            className = _props2.className,
            style = _props2.style,
            onUpdate = _props2.onUpdate;
  
  
        if (id) {
          this._portalNode.id = id;
        }
  
        if (className) {
          this._portalNode.className = className;
        }
  
        if (style) {
          Object.keys(style).forEach(function (key) {
            _this3._portalNode.style[key] = style[key];
          });
        }
  
        if (typeof onUpdate === 'function') {
          this._portalInstance = onUpdate(this._portalInstance);
        }
      }
    }, {
      key: '_destroyPortal',
      value: function _destroyPortal() {
        if (this._portalNode) {
          _reactDom2.default.unmountComponentAtNode(this._portalNode);
          this._portalNode.parentNode.removeChild(this._portalNode);
        }
        this._portalNode = null;
      }
    }, {
      key: 'render',
      value: function render() {
        if (this.props.useArray) {
          return _react.Children.toArray(this.props.children)[0];
        } else {
          return null;
        }
      }
    }]);
  
    return Travel;
  }(_react.Component);
  
  Travel.propTypes = {
    renderTag: _propTypes2.default.string,
    renderTo: _propTypes2.default.any,
    id: _propTypes2.default.string,
    className: _propTypes2.default.any,
    style: _propTypes2.default.object,
    useArray: _propTypes2.default.bool,
    onMount: _propTypes2.default.func,
    onUpdate: _propTypes2.default.func,
    onUnmount: _propTypes2.default.func
  };
  Travel.defaultProps = {
    renderTag: 'div',
    renderTo: null,
    onMount: noop,
    onUpdate: noop,
    onUnmount: noop
  };
  exports.default = Travel;
  module.exports = exports['default'];