Repository navigation
[combobox] Remove leaky value registry - #5198
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,298.71 ms -174.83 ms(-11.9%) | Renders: 78 (+0) | Paint: 2,024.85 ms -251.24 ms(-11.0%)
10 tests within noise — details 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. |
45f6a6f to
90c46ba
Compare
90c46ba to
9dc93f9
Compare
There was a problem hiding this comment.
Pull request overview
This PR removes the retained “all values ever mounted” registry from the Combobox implementation (when items is omitted), and adjusts closed-state selectedIndex reconciliation to use only the current mounted/derived registries. This reduces memory retention and avoids stale index resolution when items are unmounted or change externally.
Changes:
- Removed
allValuesReffrom the combobox store/state and item registration flow. - Updated closed-state
selectedIndexsynchronization to useflatItemswhenitemsis provided, otherwise the livevaluesRefregistry. - Added regression tests covering external value changes while closed and typeahead behavior after open/close without an
itemsprop.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/react/src/combobox/trigger/ComboboxTrigger.test.tsx | Adds a typeahead regression test for open/close cycling without items. |
| packages/react/src/combobox/store.ts | Removes allValuesRef from the shared combobox store state type. |
| packages/react/src/combobox/root/ComboboxRoot.test.tsx | Adds coverage for closed-state index clearing/re-highlighting with external value changes (no items). |
| packages/react/src/combobox/root/AriaCombobox.tsx | Drops the retained registry and reconciles selectedIndex using flatItems or live valuesRef. |
| packages/react/src/combobox/item/ComboboxItem.tsx | Stops pushing into the removed retained registry; keeps only live valuesRef registration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
flaviendelangle
left a comment
There was a problem hiding this comment.
Restored to the original branch with a clean tree. Here's the review.
PR review
Nothing merge-blocking. The core change is sound: selectedIndex is now resolved against the same registry (valuesRef) that typeahead and list navigation index into, which is exactly the misalignment the removed allValuesRef created, and the unbounded push-without-cleanup registry is gone. I ran the full Combobox suite in jsdom on the PR branch (710 passed) and chased the one removed-behavior candidate I could name — the open dependency dropped from the item effect, which used to re-assert selectedIndex on every open — but I could not construct a case where that safety net actually mattered; master behaves identically on the path I probed. The findings below are a missing test for the one genuinely new piece of logic and a note on effect shape.
Verification limit: the Chromium project could not run locally (Playwright's chrome-headless-shell isn't installed), so the reworked pointerdown/mouseup gesture path was only exercised in jsdom.
Bugs (0)
No findings.
Tests (2)
1. 🟡 The new isPrimary guard is untested
Location: packages/react/src/combobox/item/ComboboxItem.tsx:161
onPointerDownCapture(event) {
// The compat `mouseup` only fires for the primary pointer, so a non-primary
// touch must not overwrite the shared ref — a mismatch would make the primary
// pointer's release read as a drag-select and commit a second time after `click`.
if (event.isPrimary) {
store.state.pointerDownItemRef.current = event.currentTarget;
}
event.preventDefault();
}Moving from a per-item boolean (didPointerDownRef) to a single shared element ref on the store is the one place in this PR that introduces genuinely new logic rather than relocating existing logic, and the isPrimary guard is the only thing standing between it and a double commit. isPrimary appears nowhere else in packages/react/src, so there is no precedent test to lean on and no coverage of the case the comment describes.
Failure scenario: a second (non-primary) touch lands on item B while the primary touch is held on item A. Without the guard the shared ref becomes B, so the primary pointer's mouseup on A sees pointerDownItemRef.current !== event.currentTarget, treats the release as a drag-select, and commits A — then the subsequent click on A commits it a second time. Nothing in the suite would catch a regression that drops or inverts this guard.
Fix: add a Chromium-only test that dispatches pointerdown on item A with isPrimary: true, a second pointerdown on item B with isPrimary: false, then mouseup + click on A, and asserts onValueChange fired once.
2. 🟡 keepMounted closed lists now register selectedIndex, with no test pinning it
Location: packages/react/src/combobox/item/ComboboxItem.tsx:117
Dropping the if (!open) return; guard changes observable state on a path none of the new tests assert. With <Combobox.Portal keepMounted> and no items prop, selectedIndex on master is null while the popup has never been opened (the item effect bailed out, and the closed sync read the still-empty allValuesRef because FloatingPortal mounts its children in a second commit). On this branch the item effect runs on that second commit and selectedIndex is correctly the selected item's index.
Failure scenario: this is an improvement, but it's unpinned — the it.each([false, true]) typeahead test in ComboboxTrigger.test.tsx passes on master for both keepMounted values, so it does not cover it. Of the seven new tests, only cycles from the selected item after reordering items while closed fails on master; the five new ComboboxRoot.test.tsx tests all pass there, so they guard against regression rather than pin the fix.
Fix: assert selectedIndex (via the existing SelectedIndexProbe) is the selected item's index on initial render with a keepMounted portal and no items prop, before any open.
Simplifications (1)
1. ℹ️ The pointer-gesture reset is coupled to the selection-sync effect's dependency list
Location: packages/react/src/combobox/root/AriaCombobox.tsx:817
// State-driven (not tied to the internal event path) so controlled closes
// also clear a pointerdown that never received a matching item mouseup.
pointerDownItemRef.current = null;Clearing the ref is correct, but the only thing it cares about is open, while the effect it lives in re-runs on selectedValue, flatItems, multiple, isItemEqualToValue, and setIndices. Those extra runs are harmless no-ops today, but the coupling means a future change to the selection-sync dependencies silently changes when the pointer state is reset.
Failure scenario: the concrete cost is readability — a reader of syncClosedState has to work out that a pointer-gesture reset is riding along in a selection-index effect.
Fix: a dedicated one-liner states the intent and cannot drift:
useIsoLayoutEffect(() => {
if (!open) {
pointerDownItemRef.current = null;
}
}, [open]);Docs (0)
No findings.
Verdict
Approve after nits — no correctness problems found, but the new isPrimary guard and the new closed-state selectedIndex registration both ship without a test.
🤖 Review generated with Claude Code
Changes
itemsis omitted.itemsprop when provided, otherwise the live registry of mounted item values; mounted items re-assert their index while closed sokeepMounted/force-mounted lists stay in sync.