@skbkontur/ui-kit
Version:
81 lines • 3.06 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var react_dom_1 = tslib_1.__importDefault(require("react-dom"));
var lastID = 0;
function nextID() {
return ++lastID;
}
function setID(id) {
lastID = id;
}
var REACT_16 = !!react_dom_1.default.createPortal;
var RenderContainer = /** @class */ (function (_super) {
tslib_1.__extends(RenderContainer, _super);
function RenderContainer(props) {
var _this = _super.call(this, props) || this;
_this.domContainer = document.createElement('div');
_this.hydrateId();
_this.testID = nextID();
_this.domContainer.setAttribute('data-rendered-container-id', _this.testID.toString());
_this.domContainer.className = 'react-ui';
var body = document.body;
if (!body) {
throw Error('There is no "body" in "document"');
}
body.appendChild(_this.domContainer);
return _this;
}
RenderContainer.prototype.componentDidMount = function () {
if (!REACT_16) {
this.renderChild();
}
};
RenderContainer.prototype.componentDidUpdate = function () {
if (!REACT_16) {
this.renderChild();
}
};
RenderContainer.prototype.componentWillUnmount = function () {
if (!REACT_16) {
react_dom_1.default.unmountComponentAtNode(this.domContainer);
}
if (this.domContainer.parentNode) {
this.domContainer.parentNode.removeChild(this.domContainer);
}
};
RenderContainer.prototype.render = function () {
if (REACT_16) {
return [
react_dom_1.default.createPortal(this.props.children, this.domContainer),
React.createElement(Portal, { key: "portal-ref", rt_rootID: this.testID })
];
}
return React.createElement(Portal, { rt_rootID: this.testID });
};
RenderContainer.prototype.hydrateId = function () {
var nodes = document.querySelectorAll('[data-rendered-container-id]');
if (nodes.length === 0) {
return;
}
var lastNode = nodes[nodes.length - 1];
var containerId = lastNode.getAttribute('data-rendered-container-id');
if (containerId) {
setID(parseInt(containerId, 10));
}
};
RenderContainer.prototype.renderChild = function () {
react_dom_1.default.unstable_renderSubtreeIntoContainer(this, React.createElement(RootContainer, { rt_portalID: this.testID }, this.props.children), this.domContainer);
};
return RenderContainer;
}(React.Component));
exports.default = RenderContainer;
var Portal = function (_a) {
var children = _a.children, rt_rootID = _a.rt_rootID;
return (React.createElement("noscript", { "data-render-container-id": rt_rootID }));
};
function RootContainer(props) {
return React.Children.only(props.children);
}
//# sourceMappingURL=RenderContainer.js.map
;