Skip to content

v4 regression: useDefaultLayout overwrites persisted sizes for conditionally rendered panels #565

@taochu

Description

@taochu

When using conditional panels with layout persistence, the persisted layout for hidden panels gets overwritten on initial load. This worked correctly in v3 but regressed in v4.

v3 Code & Behavior (worked)

<PanelGroup
  autoSaveId="my-layout"
  direction="horizontal"
>
  <Panel order={1} id="main" defaultSize={80}>Main</Panel>
  <PanelResizeHandle />
  {showSidebar && (
    <>
      <Panel order={2} defaultSize={20} id="sidebar">Sidebar</Panel>
    </>
  )}
</PanelGroup>

v3 stored separate layouts per panel configuration:

{
  "sidebar,main": {
    "expandToSizes": {},
    "layout": [63.31, 36.69]
  },
  "main": {
    "expandToSizes": {},
    "layout": [100]
  }
}

When panels changed, v3 looked up the layout for that specific combination of panel IDs. Hidden panels didn't affect persisted layouts for other configurations.

v4 Code & Behavior (broken)

const { defaultLayout, onLayoutChange } = useDefaultLayout({
  id: 'my-layout',
  storage: localStorage,
});

<Group defaultLayout={defaultLayout} onLayoutChange={onLayoutChange}>
  <Panel id="main" defaultSize="80%">Main</Panel>
  <Separator />
  {showSidebar && (
    <>
      <Panel id="sidebar" defaultSize="20%">Sidebar</Panel>
    </>
  )}
</Group>
  • Layout stored as object: { main: 80, sidebar: 20 }
  • On initial render with only main visible, onLayoutChange fires with { main: 100 }
  • storage.setItem overwrites stored layout → sidebar's persisted size lost
  • When sidebar mounts, it falls back to defaultSize instead of the persisted value

Reproduction Steps

  1. Load page with showSidebar=true
  2. Resize sidebar to 30%
  3. Reload page with showSidebar=false (or sidebar conditionally hidden on initial render)
  4. Toggle showSidebar=true

Expected: Sidebar restores to 30%
Actual: Sidebar resets to defaultSize (20%)

Reproduction

https://codesandbox.io/p/sandbox/8hznkp

Environment

  • react-resizable-panels: 4.0.16
  • React: 19.2.3

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions