preact-arco-design
Version:
Arco Design React UI Library.
84 lines (65 loc) • 2.09 kB
JavaScript
var __extends = this && this.__extends || function () {
var _extendStatics = function extendStatics(d, b) {
_extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) {
if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
}
};
return _extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
_extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
}();
import { Component } from "preact/compat";
import ReactDOM from "preact/compat";
var Portal =
/** @class */
function (_super) {
__extends(Portal, _super);
function Portal() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.container = null;
return _this;
} // constructor(props) {
// super(props);
// const { getContainer } = this.props;
// this.container = getContainer && getContainer();
// }
Portal.prototype.componentDidMount = function () {
var _this = this;
this.createContainer();
this.timer = setTimeout(function () {
// getContainer 返回ref时,子组件首先执行 componentDidMount,此时ref还未赋值
if (!_this.container) {
_this.createContainer();
}
});
};
Portal.prototype.componentWillUnmount = function () {
clearTimeout(this.timer);
};
Portal.prototype.createContainer = function () {
var getContainer = this.props.getContainer;
this.container = getContainer && getContainer();
this.forceUpdate();
};
Portal.prototype.render = function () {
var children = this.props.children;
if (this.container) {
return ReactDOM.createPortal(children, this.container);
}
return null;
};
return Portal;
}(Component);
export default Portal;