UNPKG

4.42 kBJavaScriptView Raw
1/*
2 * Copyright 2021 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __spreadArrays } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { CSSTransition, TransitionGroup } from "react-transition-group";
20import { Classes, DISPLAYNAME_PREFIX } from "../../common";
21import { PanelView2 } from "./panelView2";
22/**
23 * @template T type union of all possible panels in this stack
24 */
25// eslint-disable-next-line @typescript-eslint/ban-types
26export var PanelStack2 = function (props) {
27 var _a = props.renderActivePanelOnly, renderActivePanelOnly = _a === void 0 ? true : _a, _b = props.showPanelHeader, showPanelHeader = _b === void 0 ? true : _b, propsStack = props.stack;
28 var _c = React.useState("push"), direction = _c[0], setDirection = _c[1];
29 var _d = React.useState(props.initialPanel !== undefined ? [props.initialPanel] : []), localStack = _d[0], setLocalStack = _d[1];
30 var stack = React.useMemo(function () { return (propsStack != null ? propsStack.slice().reverse() : localStack); }, [
31 localStack,
32 propsStack,
33 ]);
34 var stackLength = React.useRef(stack.length);
35 React.useEffect(function () {
36 if (stack.length !== stackLength.current) {
37 // Adjust the direction in case the stack size has changed, controlled or uncontrolled
38 setDirection(stack.length - stackLength.current < 0 ? "pop" : "push");
39 }
40 stackLength.current = stack.length;
41 }, [stack]);
42 var handlePanelOpen = React.useCallback(function (panel) {
43 var _a;
44 (_a = props.onOpen) === null || _a === void 0 ? void 0 : _a.call(props, panel);
45 if (props.stack == null) {
46 setLocalStack(function (prevStack) { return __spreadArrays([panel], prevStack); });
47 }
48 }, [props.onOpen]);
49 var handlePanelClose = React.useCallback(function (panel) {
50 var _a;
51 // only remove this panel if it is at the top and not the only one.
52 if (stack[0] !== panel || stack.length <= 1) {
53 return;
54 }
55 (_a = props.onClose) === null || _a === void 0 ? void 0 : _a.call(props, panel);
56 if (props.stack == null) {
57 setLocalStack(function (prevStack) { return prevStack.slice(1); });
58 }
59 }, [stack, props.onClose]);
60 // early return, after all hooks are called
61 if (stack.length === 0) {
62 return null;
63 }
64 var panelsToRender = renderActivePanelOnly ? [stack[0]] : stack;
65 var panels = panelsToRender
66 .map(function (panel, index) {
67 // With renderActivePanelOnly={false} we would keep all the CSSTransitions rendered,
68 // therefore they would not trigger the "enter" transition event as they were entered.
69 // To force the enter event, we want to change the key, but stack.length is not enough
70 // and a single panel should not rerender as long as it's hidden.
71 // This key contains two parts: first one, stack.length - index is constant (and unique) for each panel,
72 // second one, active changes only when the panel becomes or stops being active.
73 var layer = stack.length - index;
74 var key = renderActivePanelOnly ? stack.length : layer;
75 return (React.createElement(CSSTransition, { classNames: Classes.PANEL_STACK2, key: key, timeout: 400 },
76 React.createElement(PanelView2, { onClose: handlePanelClose, onOpen: handlePanelOpen, panel: panel, previousPanel: stack[index + 1], showHeader: showPanelHeader })));
77 })
78 .reverse();
79 var classes = classNames(Classes.PANEL_STACK2, Classes.PANEL_STACK2 + "-" + direction, props.className);
80 return (React.createElement(TransitionGroup, { className: classes, component: "div" }, panels));
81};
82PanelStack2.displayName = DISPLAYNAME_PREFIX + ".PanelStack2";
83//# sourceMappingURL=panelStack2.js.map
\No newline at end of file