Skip to content

[drawer] Avoid retaining unmounted drawers in DrawerProvider - #5921

Merged
atomiks merged 3 commits into
mui:masterfrom
atomiks:claude/base-ui-issue-5915-3eb66e
Oct 8, 2026
Merged

atomiks merged 3 commits into
mui:masterfrom
atomiks:claude/base-ui-issue-5915-3eb66e

Conversation

@atomiks

@atomiks atomiks commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5915

Thanks to @bryantgillespie for the detailed report and patch.

This PR stops Drawer.Provider from retaining closed drawers after they unmount. Each Drawer.Root reports setDrawerOpen(store, false) on mount and removeDrawer(store) on unmount. For a drawer that never opened, the functional updater returned prev. React skips the render in that case, but still keeps the update and its closure over the drawer store on the provider's hook queue until the provider next renders. This happens in React 17, 18 and 19. The provider only renders when the open set changes, so every closed drawer that mounted and unmounted stayed reachable, along with its elements and callbacks, until some drawer opened or closed.

Changes

  • The open drawers are now tracked in a mutable Set held in a ref. A call that doesn't change membership returns early without calling setState.
  • The provider state is now just the active boolean, which is all the context ever exposed. setActive(boolean) passes no closure, so even a skipped update can't hold a drawer. It's also only called when active actually flips.
  • Replaced the Profiler-based retention test, which also passed on the leaking code, with a jsdom test that forces GC (through process.getBuiltinModule('node:v8'), no CLI flag) and asserts removed drawers are collected.

Performance

In a jsdom probe, a routed child under a provider that never re-renders mounts and unmounts 21 closed drawers, then checks with WeakRef + forced GC whether their stores were collected:

Scenario Baseline Patch
With Drawer.Provider 42/42 stores retained 0/42
Without provider 0/42 0/42

@atomiks atomiks added type: bug It doesn't behave as expected. component: drawer Changes related to the drawer component. labels Oct 8, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5921
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5921
    

commit: 75c279f

@code-infra-dashboard

code-infra-dashboard Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+19B(0.00%) ▼-5B(0.00%)

Details of bundle changes

Performance

Total duration: 478.37 ms -104.84 ms(-18.0%) | Renders: 76 (+0) | Paint: 871.03 ms -177.93 ms(-17.0%)

Test Duration Renders
Tabs mount (200 instances) 68.82 ms ▼-29.94 ms(-30.3%) 3 (+0)
Slider mount (300 instances) 48.31 ms ▼-17.12 ms(-26.2%) 2 (+0)
Select open (500 options) 28.79 ms ▼-12.88 ms(-30.9%) 14 (+0)
Checkbox mount (500 instances) 32.05 ms ▼-9.77 ms(-23.4%) 1 (+0)
Popover mount (300 instances) 14.47 ms ▼-5.35 ms(-27.0%) 1 (+0)

…and 3 more (+7 within noise) — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 75c279f
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ac7522bf4f64400083acd71
😎 Deploy Preview https://deploy-preview-5921--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks marked this pull request as ready for review October 8, 2026 02:12
@atomiks
atomiks merged commit 3296de3 into mui:master Oct 8, 2026
22 checks passed
@atomiks
atomiks deleted the claude/base-ui-issue-5915-3eb66e branch October 8, 2026 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: drawer Changes related to the drawer component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[drawer] DrawerProvider retains every unmounted Drawer until a drawer opens or closes

3 participants