Repository navigation
[dialog] Fix detached trigger HMR with recreated handles - #4472
Conversation
commit: |
Bundle size report
Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codex Review (GPT-5.4)This is a focused bug-fix PR. The branch now applies the detached-handle store-selection fix to both 1. Bugs / Issues (None)I reviewed the full branch diff against a freshly fetched 2. Root Cause & Patch Assessment
3. Pattern Consistency Assessment
4. Test Coverage AssessmentI ran the following validations against the branch:
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 ScoreOverall 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. |
flaviendelangle
left a comment
There was a problem hiding this comment.
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.useStorestatic method that matches existing implementations inPopoverStore,TooltipStore, etc. - Good fix to existing test helpers wrapping
Dialog.createHandle()inuseRefWithInit— without this, the newuseStorelogic would see a fresh handle on every rerender - Clean removal of now-unused
useRefWithInitimport from bothDialogRoot.tsxandAlertDialogRoot.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):
- Reparenting with removing wrappers
- Reparenting with adding wrappers
- Fast Refresh-like handle recreation
- 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
Fixes #4470
This completes the detached-trigger HMR fix for
Dialog. PR #3724 updated the popup-store-based components, butDialogstill kept the original detached trigger store selection inDialog.Root, so recreating a handle during Fast Refresh could leave the root and trigger talking to different stores.Changes
Dialogto the same external-store selection pattern used by the original detached-trigger fix soDialog.Rootswitches to the latest handle store on rerender.