UNPKG

4.66 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.Portal = void 0;
19var tslib_1 = require("tslib");
20var React = tslib_1.__importStar(require("react"));
21var ReactDOM = tslib_1.__importStar(require("react-dom"));
22var Classes = tslib_1.__importStar(require("../../common/classes"));
23var Errors = tslib_1.__importStar(require("../../common/errors"));
24var props_1 = require("../../common/props");
25var 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 * Portal component.
35 *
36 * This component detaches its contents and re-attaches them to document.body.
37 * Use it when you need to circumvent DOM z-stacking (for dialogs, popovers, etc.).
38 * Any class names passed to this element will be propagated to the new container element on document.body.
39 *
40 * @see https://blueprintjs.com/docs/#core/components/portal
41 */
42var Portal = /** @class */ (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 // Only render `children` once this component has mounted in a browser environment, so they are
53 // immediately attached to the DOM tree and can do DOM things like measuring or `autoFocus`.
54 // See long comment on componentDidMount in https://reactjs.org/docs/portals.html#event-bubbling-through-portals
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 /* eslint-disable-next-line react/no-did-mount-set-state */
69 this.setState({ hasMounted: true }, this.props.onChildrenMount);
70 };
71 Portal.prototype.componentDidUpdate = function (prevProps) {
72 // update className prop on portal DOM element
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));
98exports.Portal = Portal;
99function maybeRemoveClass(classList, className) {
100 if (className != null && className !== "") {
101 classList.remove.apply(classList, className.split(" "));
102 }
103}
104function maybeAddClass(classList, className) {
105 if (className != null && className !== "") {
106 classList.add.apply(classList, className.split(" "));
107 }
108}
109//# sourceMappingURL=portal.js.map
\No newline at end of file