100 lines
4.0 KiB
JavaScript
100 lines
4.0 KiB
JavaScript
'use strict';
|
|
|
|
exports.__esModule = true;
|
|
|
|
var _propTypes = require('prop-types');
|
|
|
|
var _propTypes2 = _interopRequireDefault(_propTypes);
|
|
|
|
var _componentOrElement = require('prop-types-extra/lib/componentOrElement');
|
|
|
|
var _componentOrElement2 = _interopRequireDefault(_componentOrElement);
|
|
|
|
var _react = require('react');
|
|
|
|
var _react2 = _interopRequireDefault(_react);
|
|
|
|
var _reactDom = require('react-dom');
|
|
|
|
var _reactDom2 = _interopRequireDefault(_reactDom);
|
|
|
|
var _getContainer = require('./utils/getContainer');
|
|
|
|
var _getContainer2 = _interopRequireDefault(_getContainer);
|
|
|
|
var _ownerDocument = require('./utils/ownerDocument');
|
|
|
|
var _ownerDocument2 = _interopRequireDefault(_ownerDocument);
|
|
|
|
var _LegacyPortal = require('./LegacyPortal');
|
|
|
|
var _LegacyPortal2 = _interopRequireDefault(_LegacyPortal);
|
|
|
|
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; }
|
|
|
|
/**
|
|
* The `<Portal/>` component renders its children into a new "subtree" outside of current component hierarchy.
|
|
* You can think of it as a declarative `appendChild()`, or jQuery's `$.fn.appendTo()`.
|
|
* The children of `<Portal/>` component will be appended to the `container` specified.
|
|
*/
|
|
var Portal = function (_React$Component) {
|
|
_inherits(Portal, _React$Component);
|
|
|
|
function Portal() {
|
|
var _temp, _this, _ret;
|
|
|
|
_classCallCheck(this, Portal);
|
|
|
|
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.setContainer = function () {
|
|
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props;
|
|
|
|
_this._portalContainerNode = (0, _getContainer2.default)(props.container, (0, _ownerDocument2.default)(_this).body);
|
|
}, _this.getMountNode = function () {
|
|
return _this._portalContainerNode;
|
|
}, _temp), _possibleConstructorReturn(_this, _ret);
|
|
}
|
|
|
|
Portal.prototype.componentDidMount = function componentDidMount() {
|
|
this.setContainer();
|
|
this.forceUpdate(this.props.onRendered);
|
|
};
|
|
|
|
Portal.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
|
if (nextProps.container !== this.props.container) {
|
|
this.setContainer(nextProps);
|
|
}
|
|
};
|
|
|
|
Portal.prototype.componentWillUnmount = function componentWillUnmount() {
|
|
this._portalContainerNode = null;
|
|
};
|
|
|
|
Portal.prototype.render = function render() {
|
|
return this.props.children && this._portalContainerNode ? _reactDom2.default.createPortal(this.props.children, this._portalContainerNode) : null;
|
|
};
|
|
|
|
return Portal;
|
|
}(_react2.default.Component);
|
|
|
|
Portal.displayName = 'Portal';
|
|
Portal.propTypes = {
|
|
/**
|
|
* A Node, Component instance, or function that returns either. The `container` will have the Portal children
|
|
* appended to it.
|
|
*/
|
|
container: _propTypes2.default.oneOfType([_componentOrElement2.default, _propTypes2.default.func]),
|
|
|
|
onRendered: _propTypes2.default.func
|
|
};
|
|
exports.default = _reactDom2.default.createPortal ? Portal : _LegacyPortal2.default;
|
|
module.exports = exports['default']; |