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.Portal2 = void 0;
|
19 | var tslib_1 = require("tslib");
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var React = tslib_1.__importStar(require("react"));
|
34 | var ReactDOM = tslib_1.__importStar(require("react-dom"));
|
35 | var Classes = tslib_1.__importStar(require("../../common/classes"));
|
36 | var Errors = tslib_1.__importStar(require("../../common/errors"));
|
37 | var props_1 = require("../../common/props");
|
38 | var portalProvider_1 = require("../../context/portal/portalProvider");
|
39 | var usePrevious_1 = require("../../hooks/usePrevious");
|
40 | var REACT_CONTEXT_TYPES = {
|
41 | blueprintPortalClassName: function (obj, key) {
|
42 | if (obj[key] != null && typeof obj[key] !== "string") {
|
43 | return new Error(Errors.PORTAL_CONTEXT_CLASS_NAME_STRING);
|
44 | }
|
45 | return undefined;
|
46 | },
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function Portal2(props, legacyContext) {
|
56 | if (legacyContext === void 0) { legacyContext = {}; }
|
57 | var context = React.useContext(portalProvider_1.PortalContext);
|
58 | var _a = React.useState(false), hasMounted = _a[0], setHasMounted = _a[1];
|
59 | var _b = React.useState(), portalElement = _b[0], setPortalElement = _b[1];
|
60 | var createContainerElement = React.useCallback(function () {
|
61 | var container = document.createElement("div");
|
62 | container.classList.add(Classes.PORTAL);
|
63 | maybeAddClass(container.classList, props.className);
|
64 | maybeAddClass(container.classList, context.portalClassName);
|
65 | var blueprintPortalClassName = legacyContext.blueprintPortalClassName;
|
66 | if (blueprintPortalClassName != null && blueprintPortalClassName !== "") {
|
67 | console.error(Errors.PORTAL_LEGACY_CONTEXT_API);
|
68 | maybeAddClass(container.classList, blueprintPortalClassName);
|
69 | }
|
70 | return container;
|
71 | }, [props.className, context.portalClassName]);
|
72 |
|
73 | React.useEffect(function () {
|
74 | if (props.container == null) {
|
75 | return;
|
76 | }
|
77 | var newPortalElement = createContainerElement();
|
78 | props.container.appendChild(newPortalElement);
|
79 | setPortalElement(newPortalElement);
|
80 | setHasMounted(true);
|
81 | return function () {
|
82 | newPortalElement.remove();
|
83 | setHasMounted(false);
|
84 | setPortalElement(undefined);
|
85 | };
|
86 | }, [props.container, createContainerElement]);
|
87 |
|
88 | React.useEffect(function () {
|
89 | var _a;
|
90 | if (hasMounted) {
|
91 | (_a = props.onChildrenMount) === null || _a === void 0 ? void 0 : _a.call(props);
|
92 | }
|
93 | }, [hasMounted, props.onChildrenMount]);
|
94 |
|
95 | var prevClassName = (0, usePrevious_1.usePrevious)(props.className);
|
96 | React.useEffect(function () {
|
97 | if (portalElement != null) {
|
98 | maybeRemoveClass(portalElement.classList, prevClassName);
|
99 | maybeAddClass(portalElement.classList, props.className);
|
100 | }
|
101 | }, [props.className]);
|
102 |
|
103 |
|
104 |
|
105 | if (typeof document === "undefined" || !hasMounted || portalElement == null) {
|
106 | return null;
|
107 | }
|
108 | else {
|
109 | return ReactDOM.createPortal(props.children, portalElement);
|
110 | }
|
111 | }
|
112 | exports.Portal2 = Portal2;
|
113 | Portal2.defaultProps = {
|
114 | container: typeof document !== "undefined" ? document.body : undefined,
|
115 | };
|
116 | Portal2.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".Portal2");
|
117 | Portal2.contextTypes = REACT_CONTEXT_TYPES;
|
118 | function maybeRemoveClass(classList, className) {
|
119 | if (className != null && className !== "") {
|
120 | classList.remove.apply(classList, className.split(" "));
|
121 | }
|
122 | }
|
123 | function maybeAddClass(classList, className) {
|
124 | if (className != null && className !== "") {
|
125 | classList.add.apply(classList, className.split(" "));
|
126 | }
|
127 | }
|
128 |
|
\ | No newline at end of file |