UNPKG

4.8 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2021 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.PanelStack2 = void 0;
19var tslib_1 = require("tslib");
20var classnames_1 = tslib_1.__importDefault(require("classnames"));
21var React = tslib_1.__importStar(require("react"));
22var react_transition_group_1 = require("react-transition-group");
23var common_1 = require("../../common");
24var panelView2_1 = require("./panelView2");
25/**
26 * Panel stack (v2) component.
27 *
28 * @see https://blueprintjs.com/docs/#core/components/panel-stack2
29 * @template T type union of all possible panels in this stack
30 */
31// eslint-disable-next-line @typescript-eslint/ban-types
32var PanelStack2 = function (props) {
33 var _a = props.renderActivePanelOnly, renderActivePanelOnly = _a === void 0 ? true : _a, _b = props.showPanelHeader, showPanelHeader = _b === void 0 ? true : _b, propsStack = props.stack;
34 var _c = React.useState("push"), direction = _c[0], setDirection = _c[1];
35 var _d = React.useState(props.initialPanel !== undefined ? [props.initialPanel] : []), localStack = _d[0], setLocalStack = _d[1];
36 var stack = React.useMemo(function () { return (propsStack != null ? propsStack.slice().reverse() : localStack); }, [localStack, propsStack]);
37 var stackLength = React.useRef(stack.length);
38 React.useEffect(function () {
39 if (stack.length !== stackLength.current) {
40 // Adjust the direction in case the stack size has changed, controlled or uncontrolled
41 setDirection(stack.length - stackLength.current < 0 ? "pop" : "push");
42 }
43 stackLength.current = stack.length;
44 }, [stack]);
45 var handlePanelOpen = React.useCallback(function (panel) {
46 var _a;
47 (_a = props.onOpen) === null || _a === void 0 ? void 0 : _a.call(props, panel);
48 if (props.stack == null) {
49 setLocalStack(function (prevStack) { return tslib_1.__spreadArray([panel], prevStack, true); });
50 }
51 }, [props.onOpen]);
52 var handlePanelClose = React.useCallback(function (panel) {
53 var _a;
54 // only remove this panel if it is at the top and not the only one.
55 if (stack[0] !== panel || stack.length <= 1) {
56 return;
57 }
58 (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props, panel);
59 if (props.stack == null) {
60 setLocalStack(function (prevStack) { return prevStack.slice(1); });
61 }
62 }, [stack, props.onClose]);
63 // early return, after all hooks are called
64 if (stack.length === 0) {
65 return null;
66 }
67 var panelsToRender = renderActivePanelOnly ? [stack[0]] : stack;
68 var panels = panelsToRender
69 .map(function (panel, index) {
70 // With renderActivePanelOnly={false} we would keep all the CSSTransitions rendered,
71 // therefore they would not trigger the "enter" transition event as they were entered.
72 // To force the enter event, we want to change the key, but stack.length is not enough
73 // and a single panel should not rerender as long as it's hidden.
74 // This key contains two parts: first one, stack.length - index is constant (and unique) for each panel,
75 // second one, active changes only when the panel becomes or stops being active.
76 var layer = stack.length - index;
77 var key = renderActivePanelOnly ? stack.length : layer;
78 return (React.createElement(react_transition_group_1.CSSTransition, { classNames: common_1.Classes.PANEL_STACK2, key: key, timeout: 400 },
79 React.createElement(panelView2_1.PanelView2, { onClose: handlePanelClose, onOpen: handlePanelOpen, panel: panel, previousPanel: stack[index + 1], showHeader: showPanelHeader })));
80 })
81 .reverse();
82 var classes = (0, classnames_1.default)(common_1.Classes.PANEL_STACK2, "".concat(common_1.Classes.PANEL_STACK2, "-").concat(direction), props.className);
83 return (React.createElement(react_transition_group_1.TransitionGroup, { className: classes, component: "div" }, panels));
84};
85exports.PanelStack2 = PanelStack2;
86exports.PanelStack2.displayName = "".concat(common_1.DISPLAYNAME_PREFIX, ".PanelStack2");
87//# sourceMappingURL=panelStack2.js.map
\No newline at end of file