Skip to content

[dialog] Fix detached trigger HMR with recreated handles - #4472

Merged
atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/dialog-detached-trigger-hmr
Mar 30, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
atomiks:codex/dialog-detached-trigger-hmr

Conversation

@atomiks

@atomiks atomiks commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #4470

This completes the detached-trigger HMR fix for Dialog. PR #3724 updated the popup-store-based components, but Dialog still kept the original detached trigger store selection in Dialog.Root, so recreating a handle during Fast Refresh could leave the root and trigger talking to different stores.

Changes

  • Move Dialog to the same external-store selection pattern used by the original detached-trigger fix so Dialog.Root switches to the latest handle store on rerender.
  • Add a Chromium regression test that recreates the detached trigger handle without changing the tree shape, matching the Fast Refresh case from the issue.

@atomiks atomiks added type: bug It doesn't behave as expected. component: dialog Changes related to the dialog component. labels Mar 29, 2026 — with ChatGPT Codex Connector
@pkg-pr-new

pkg-pr-new Bot commented Mar 29, 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@4472
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4472
    

commit: e979389

@mui-bot

mui-bot commented Mar 29, 2026 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+44B(+0.01%) 🔺+26B(+0.02%)

Details of bundle changes


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

@netlify

netlify Bot commented Mar 29, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit e979389
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69ca214f6df6b30008709cea
😎 Deploy Preview https://deploy-preview-4472--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@atomiks
atomiks marked this pull request as ready for review March 30, 2026 00:15

atomiks commented Mar 30, 2026

Copy link
Copy Markdown
Contributor Author

Codex Review (GPT-5.4)

This is a focused bug-fix PR. The branch now applies the detached-handle store-selection fix to both Dialog and AlertDialog, and the regression coverage directly exercises Fast Refresh-like handle recreation plus the nearby reparenting cases that were previously untested on the dialog-family side.

1. Bugs / Issues (None)

I reviewed the full branch diff against a freshly fetched upstream/master and did not find any remaining correctness issues in the patched paths.

2. Root Cause & Patch Assessment

Area Assessment Notes
Dialog.Root store selection Handled Switching from one-time store capture to DialogStore.useStore(handle?.store, ...) addresses the stale detached-handle root cause directly.
AlertDialog.Root parity Handled The same store-selection fix is now applied to the sibling surface that shares DialogHandle / DialogStore.
Regression scope Good The branch covers both pure handle recreation and handle recreation combined with trigger reparenting.

3. Pattern Consistency Assessment

Area Status Notes
Dialog-family behavior Consistent Dialog and AlertDialog now follow the same external-store selection pattern.
Broad detached-trigger fixtures Improved Stabilizing the general DialogRoot.test.tsx detached-trigger fixtures with useRefWithInit removes incidental handle churn from unrelated lifecycle tests while leaving Fast Refresh coverage in the dedicated regression tests.

4. Test Coverage Assessment

I ran the following validations against the branch:

Command Result
pnpm test:jsdom DialogRoot --no-watch Passed
pnpm test:chromium DialogRoot --no-watch Passed
pnpm eslint packages/react/src/dialog/root/DialogRoot.tsx packages/react/src/dialog/store/DialogStore.ts packages/react/src/dialog/root/DialogRoot.test.tsx packages/react/src/dialog/root/DialogRoot.detached-triggers.test.tsx Passed
pnpm test:jsdom AlertDialogRoot --no-watch Passed
pnpm test:chromium AlertDialogRoot --no-watch Passed
pnpm eslint packages/react/src/alert-dialog/root/AlertDialogRoot.tsx packages/react/src/alert-dialog/root/AlertDialogRoot.test.tsx Passed

The new coverage is appropriately targeted for a bug-fix PR: it pins the reported Fast Refresh failure mode and adds adjacent reparenting variants for both public detached-trigger surfaces that share this handle/store mechanism.

Merge Confidence Score

Overall merge confidence is 4/5. I’m reasonably confident the branch now fixes the detached-trigger HMR/store-mismatch issue cleanly across the dialog-family surfaces it touches, with focused regression coverage and passing targeted validation to back it up.

@atomiks atomiks added the component: alert dialog Changes related to the alert dialog component. label Mar 30, 2026

@flaviendelangle flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code Review: [dialog] Fix detached trigger HMR with recreated handles (#4472)

Confidence: 4.5/5

High confidence in the architectural assessment. The pattern is already battle-tested in Popover, Menu, Tooltip, and PreviewCard. Docked half a point only because I haven't run the full Chromium test suite locally.

Overview

Fixes a bug where Fast Refresh (HMR) could break detached dialog triggers. The root cause: DialogRoot and AlertDialogRoot used useRefWithInit to capture the store on first render only. When HMR recreated the handle (producing a new store), the root kept using the stale ref'd store while the trigger talked to the new one.

The fix aligns Dialog/AlertDialog with the pattern already established for Popover, Menu, Tooltip, and PreviewCard — a static useStore method on the store class that returns externalStore ?? internalStore, re-evaluating on every render.

Positives

  • Clean, minimal DialogStore.useStore static method that matches existing implementations in PopoverStore, TooltipStore, etc.
  • Good fix to existing test helpers wrapping Dialog.createHandle() in useRefWithInit — without this, the new useStore logic would see a fresh handle on every rerender
  • Clean removal of now-unused useRefWithInit import from both DialogRoot.tsx and AlertDialogRoot.tsx
  • Excellent test coverage with 8 new tests (4 for Dialog, 4 for AlertDialog)

Issues

1. No disposeEffect needed (verified, non-issue)

Unlike PopoverStore.useStore which cleans up stickIfOpenTimeout, DialogStore has no side-effect resources to dispose. The omission of useOnMount(internalStore.disposeEffect) is correct.

2. ESLint suppress for hooks rule (acceptable)

// eslint-disable-next-line react-hooks/rules-of-hooks
const internalStore = useRefWithInit(() => {

The suppress in DialogStore.useStore is necessary because the hook is inside a static method. This matches the pattern used in TooltipStore. Safe in practice since useStore is only ever called from component render.

3. Test helper duplication (minor, stylistic)

TriggerWithNesting, DetachedTriggerReparentingTest, and openAndCloseDialog are duplicated nearly identically between the Dialog and AlertDialog test files (~130 lines). A shared test utility could reduce this, but keeping test files self-contained is also a valid convention.

4. openAndCloseDialog types user as any (minor)

async function openAndCloseDialog(user: any) {

Matches existing test conventions in this repo, so consistent but not ideal.

Test Coverage

Four new test scenarios for both Dialog and AlertDialog (8 tests total):

  1. Reparenting with removing wrappers
  2. Reparenting with adding wrappers
  3. Fast Refresh-like handle recreation
  4. Combined reparenting + handle recreation

The TriggerWithNesting helper is a clever way to simulate DOM tree changes without changing the component identity.

Verdict

Well-scoped, well-tested bug fix that correctly aligns Dialog with the established store pattern. No blocking issues. Ready to merge.

Rating: 4.5/5

@atomiks
atomiks merged commit 4e5b1c2 into mui:master Mar 30, 2026
7 checks passed
@atomiks
atomiks deleted the codex/dialog-detached-trigger-hmr branch March 30, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[dialog] Detached triggers don't work after HMR

3 participants