1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.Portal = void 0;
|
19 | var tslib_1 = require("tslib");
|
20 | var React = tslib_1.__importStar(require("react"));
|
21 | var ReactDOM = tslib_1.__importStar(require("react-dom"));
|
22 | var Classes = tslib_1.__importStar(require("../../common/classes"));
|
23 | var Errors = tslib_1.__importStar(require("../../common/errors"));
|
24 | var props_1 = require("../../common/props");
|
25 | var REACT_CONTEXT_TYPES = {
|
26 | blueprintPortalClassName: function (obj, key) {
|
27 | if (obj[key] != null && typeof obj[key] !== "string") {
|
28 | return new Error(Errors.PORTAL_CONTEXT_CLASS_NAME_STRING);
|
29 | }
|
30 | return undefined;
|
31 | },
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var Portal = (function (_super) {
|
43 | tslib_1.__extends(Portal, _super);
|
44 | function Portal() {
|
45 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
46 | _this.context = {};
|
47 | _this.state = { hasMounted: false };
|
48 | _this.portalElement = null;
|
49 | return _this;
|
50 | }
|
51 | Portal.prototype.render = function () {
|
52 |
|
53 |
|
54 |
|
55 | if (typeof document === "undefined" || !this.state.hasMounted || this.portalElement === null) {
|
56 | return null;
|
57 | }
|
58 | else {
|
59 | return ReactDOM.createPortal(this.props.children, this.portalElement);
|
60 | }
|
61 | };
|
62 | Portal.prototype.componentDidMount = function () {
|
63 | if (this.props.container == null) {
|
64 | return;
|
65 | }
|
66 | this.portalElement = this.createContainerElement();
|
67 | this.props.container.appendChild(this.portalElement);
|
68 |
|
69 | this.setState({ hasMounted: true }, this.props.onChildrenMount);
|
70 | };
|
71 | Portal.prototype.componentDidUpdate = function (prevProps) {
|
72 |
|
73 | if (this.portalElement != null && prevProps.className !== this.props.className) {
|
74 | maybeRemoveClass(this.portalElement.classList, prevProps.className);
|
75 | maybeAddClass(this.portalElement.classList, this.props.className);
|
76 | }
|
77 | };
|
78 | Portal.prototype.componentWillUnmount = function () {
|
79 | var _a;
|
80 | (_a = this.portalElement) === null || _a === void 0 ? void 0 : _a.remove();
|
81 | };
|
82 | Portal.prototype.createContainerElement = function () {
|
83 | var container = document.createElement("div");
|
84 | container.classList.add(Classes.PORTAL);
|
85 | maybeAddClass(container.classList, this.props.className);
|
86 | if (this.context != null) {
|
87 | maybeAddClass(container.classList, this.context.blueprintPortalClassName);
|
88 | }
|
89 | return container;
|
90 | };
|
91 | Portal.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".Portal");
|
92 | Portal.contextTypes = REACT_CONTEXT_TYPES;
|
93 | Portal.defaultProps = {
|
94 | container: typeof document !== "undefined" ? document.body : undefined,
|
95 | };
|
96 | return Portal;
|
97 | }(React.Component));
|
98 | exports.Portal = Portal;
|
99 | function maybeRemoveClass(classList, className) {
|
100 | if (className != null && className !== "") {
|
101 | classList.remove.apply(classList, className.split(" "));
|
102 | }
|
103 | }
|
104 | function maybeAddClass(classList, className) {
|
105 | if (className != null && className !== "") {
|
106 | classList.add.apply(classList, className.split(" "));
|
107 | }
|
108 | }
|
109 |
|
\ | No newline at end of file |