UNPKG

@blueprintjs/core

Version:

Core styles & components

72 lines 4.13 kB
/* * Copyright 2021 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { __spreadArray } from "tslib"; import classNames from "classnames"; import * as React from "react"; import { CSSTransition, TransitionGroup } from "react-transition-group"; import { Classes, DISPLAYNAME_PREFIX } from "../../common"; import { usePrevious } from "../../hooks"; import { PanelView2 } from "./panelView2"; /** * Panel stack (v2) component. * * @see https://blueprintjs.com/docs/#core/components/panel-stack2 * @template T type union of all possible panels in this stack */ export var PanelStack2 = function (props) { var _a; var initialPanel = props.initialPanel, onClose = props.onClose, onOpen = props.onOpen, _b = props.renderActivePanelOnly, renderActivePanelOnly = _b === void 0 ? true : _b, _c = props.showPanelHeader, showPanelHeader = _c === void 0 ? true : _c, propsStack = props.stack; var isControlled = propsStack != null; var _d = React.useState(initialPanel !== undefined ? [initialPanel] : []), localStack = _d[0], setLocalStack = _d[1]; var stack = React.useMemo(function () { return (isControlled ? propsStack.slice().reverse() : localStack); }, [localStack, isControlled, propsStack]); var prevStackLength = (_a = usePrevious(stack.length)) !== null && _a !== void 0 ? _a : stack.length; var direction = stack.length - prevStackLength < 0 ? "pop" : "push"; var handlePanelOpen = React.useCallback(function (panel) { onOpen === null || onOpen === void 0 ? void 0 : onOpen(panel); if (!isControlled) { setLocalStack(function (prevStack) { return __spreadArray([panel], prevStack, true); }); } }, [onOpen, isControlled]); var handlePanelClose = React.useCallback(function (panel) { onClose === null || onClose === void 0 ? void 0 : onClose(panel); if (!isControlled) { setLocalStack(function (prevStack) { return prevStack.slice(1); }); } }, [onClose, isControlled]); // early return, after all hooks are called if (stack.length === 0) { return null; } var panelsToRender = renderActivePanelOnly ? [stack[0]] : stack; var panels = panelsToRender .map(function (panel, index) { // With renderActivePanelOnly={false} we would keep all the CSSTransitions rendered, // therefore they would not trigger the "enter" transition event as they were entered. // To force the enter event, we want to change the key, but stack.length is not enough // and a single panel should not rerender as long as it's hidden. // This key contains two parts: first one, stack.length - index is constant (and unique) for each panel, // second one, active changes only when the panel becomes or stops being active. var layer = stack.length - index; var key = renderActivePanelOnly ? stack.length : layer; return (React.createElement(CSSTransition, { classNames: Classes.PANEL_STACK2, key: key, timeout: 400 }, React.createElement(PanelView2, { onClose: handlePanelClose, onOpen: handlePanelOpen, panel: panel, previousPanel: stack[index + 1], showHeader: showPanelHeader }))); }) .reverse(); var classes = classNames(Classes.PANEL_STACK2, "".concat(Classes.PANEL_STACK2, "-").concat(direction), props.className); return (React.createElement(TransitionGroup, { className: classes, component: "div" }, panels)); }; PanelStack2.displayName = "".concat(DISPLAYNAME_PREFIX, ".PanelStack2"); //# sourceMappingURL=panelStack2.js.map