Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent PanelStack2 from opening in the wrong direction #6847

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions packages/core/src/components/panel-stack2/panelStack2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as React from "react";
import { CSSTransition, TransitionGroup } from "react-transition-group";

import { Classes, DISPLAYNAME_PREFIX, type Props } from "../../common";
import { usePrevious } from "../../hooks";

import type { Panel } from "./panelTypes";
import { PanelView2 } from "./panelView2";
Expand Down Expand Up @@ -97,21 +98,14 @@ export const PanelStack2: PanelStack2Component = <T extends Panel<object>>(props
stack: propsStack,
} = props;
const isControlled = propsStack != null;
const [direction, setDirection] = React.useState("push");

const [localStack, setLocalStack] = React.useState<T[]>(initialPanel !== undefined ? [initialPanel] : []);
const stack = React.useMemo(
() => (isControlled ? propsStack.slice().reverse() : localStack),
[localStack, isControlled, propsStack],
);
const stackLength = React.useRef<number>(stack.length);
React.useEffect(() => {
if (stack.length !== stackLength.current) {
// Adjust the direction in case the stack size has changed, controlled or uncontrolled
setDirection(stack.length - stackLength.current < 0 ? "pop" : "push");
}
stackLength.current = stack.length;
}, [stack]);
const prevStackLength = usePrevious(stack.length) ?? stack.length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving comment for posterity - technically stack.length is not the previous stack length, but it lets us not worry about handling the undefined case, and there is no animation on the initial render anyways

const direction = stack.length - prevStackLength < 0 ? "pop" : "push";

const handlePanelOpen = React.useCallback(
(panel: T) => {
Expand Down