Repository navigation
[popups] Fix detached trigger remounting - #3724
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. |
Greptile OverviewGreptile SummaryOverviewThis PR fixes a critical bug where detached popover triggers would lose correct positioning after remounting, particularly in scenarios involving React key-based remounts and conditional trigger rendering. Root CauseWhen triggers remount (especially with React key changes), the cleanup function from the old component instance could run after the new instance had already registered, causing the new element to be incorrectly removed from the trigger map. Additionally, stale element references weren't being updated in the floating root store. SolutionThe fix implements three coordinated changes: 1. Registration Race Condition Guard (
|
| Filename | Score | Overview |
|---|---|---|
| packages/react/src/floating-ui-react/hooks/useSyncedFloatingRootContext.ts | 5/5 | Added positionReference sync when it equals referenceElement, ensuring Floating UI stays anchored to current trigger during remounts |
| packages/react/src/utils/popups/popupStoreUtils.ts | 5/5 | Hardened trigger registration against race conditions during keyed remounts and added logic to refresh activeTriggerElement when active trigger re-registers |
| packages/react/src/popover/root/PopoverRoot.detached-triggers.test.tsx | 5/5 | Added comprehensive test case validating correct positioning when conditional triggers unmount and the tree remounts with a new key |
Sequence Diagram
sequenceDiagram
participant React
participant TriggerOld as Trigger 0 (old)
participant TriggerMap
participant PopupStore
participant FloatingStore
participant TriggerNew as Trigger 0 (new)
Note over React,FloatingStore: Initial state with old element
React->>TriggerNew: Mount new element with same ID
TriggerNew->>TriggerMap: register(newElement)
TriggerMap->>TriggerMap: add id, newElement
Note over TriggerNew,PopupStore: Fix 2: Refresh active element
TriggerNew->>PopupStore: update activeTriggerElement
Note over PopupStore,FloatingStore: Fix 3: Sync position reference
PopupStore->>FloatingStore: sync referenceElement
PopupStore->>FloatingStore: sync positionReference
React->>TriggerOld: Cleanup old element
Note over TriggerOld,TriggerMap: Fix 1: Guard deletion
TriggerOld->>TriggerMap: check if currentElement == oldElement
TriggerMap-->>TriggerOld: No match, skip deletion
Note over FloatingStore: Positioning stays correct
Fixes #3681
https://stackblitz.com/edit/tzqusig4-qsqweuem?file=src%2FApp.tsx
FloatingRootStore.positionReferencesynced with the latest referenceElement (when it hasn't been overridden), so Floating UI doesn't remain anchored to a stale triggeractiveTriggerElementwhen the active trigger re-registersPopover.createHandle()is recreated: previouslyuseRefWithInit(() => externalStore ?? ...)would keep the old store forever, so the Root kept syncing listeners/floatingRootContext into the old store while the Trigger was using the new one. Additionally, the store needs to be reactive to thestorehandle itself, not just its valuesReview: @greptileai