From 870132b27ab98adb6d47344f4a1e3006995c8b0f Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Sat, 24 Feb 2024 13:18:47 -0500 Subject: [PATCH] Fix conditional layout edge case --- .../src/PanelGroup.test.tsx | 29 +++++++++++++++++++ .../react-resizable-panels/src/PanelGroup.ts | 8 ++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/react-resizable-panels/src/PanelGroup.test.tsx b/packages/react-resizable-panels/src/PanelGroup.test.tsx index c6f747af8..abb12ce40 100644 --- a/packages/react-resizable-panels/src/PanelGroup.test.tsx +++ b/packages/react-resizable-panels/src/PanelGroup.test.tsx @@ -130,6 +130,35 @@ describe("PanelGroup", () => { expect(rightPanelElement?.getAttribute("data-panel-size")).toBe("40.0"); }); + // github.com/bvaughn/react-resizable-panels/issues/303 + it("should recalculate layout after panels are changed", () => { + let mostRecentLayout: number[] | null = null; + + const onLayout = (layout: number[]) => { + mostRecentLayout = layout; + }; + + act(() => { + root.render( + + + + + + ); + }); + expect(mostRecentLayout).toEqual([30, 70]); + + act(() => { + root.render( + + + + ); + }); + expect(mostRecentLayout).toEqual([100]); + }); + describe("imperative handle API", () => { it("should report the most recently rendered group id", () => { const ref = createRef(); diff --git a/packages/react-resizable-panels/src/PanelGroup.ts b/packages/react-resizable-panels/src/PanelGroup.ts index 469d0e62a..ce167c7ec 100644 --- a/packages/react-resizable-panels/src/PanelGroup.ts +++ b/packages/react-resizable-panels/src/PanelGroup.ts @@ -768,10 +768,10 @@ function PanelGroupWithForwardedRef({ panelData, layout ); - assert( - prevPanelSize != null, - `Previous panel size not found for panel "${panelData.id}"` - ); + if (prevPanelSize == null) { + // It's possible that the panels in this group have changed since the last render + return; + } if ( prevCollapsible &&