Skip to content

Commit 605d318

Browse files
authored
fix: prevent PanelStack2 from opening in the wrong direction (#6847)
1 parent f35e0da commit 605d318

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

packages/core/src/components/panel-stack2/panelStack2.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as React from "react";
1919
import { CSSTransition, TransitionGroup } from "react-transition-group";
2020

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

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

102102
const [localStack, setLocalStack] = React.useState<T[]>(initialPanel !== undefined ? [initialPanel] : []);
103103
const stack = React.useMemo(
104104
() => (isControlled ? propsStack.slice().reverse() : localStack),
105105
[localStack, isControlled, propsStack],
106106
);
107-
const stackLength = React.useRef<number>(stack.length);
108-
React.useEffect(() => {
109-
if (stack.length !== stackLength.current) {
110-
// Adjust the direction in case the stack size has changed, controlled or uncontrolled
111-
setDirection(stack.length - stackLength.current < 0 ? "pop" : "push");
112-
}
113-
stackLength.current = stack.length;
114-
}, [stack]);
107+
const prevStackLength = usePrevious(stack.length) ?? stack.length;
108+
const direction = stack.length - prevStackLength < 0 ? "pop" : "push";
115109

116110
const handlePanelOpen = React.useCallback(
117111
(panel: T) => {

0 commit comments

Comments
 (0)