Skip to content

[combobox] Remove leaky value registry - #5198

Merged
atomiks merged 12 commits into
mui:masterfrom
atomiks:codex/remove-combobox-all-values-ref
Jul 13, 2026
Merged

atomiks merged 12 commits into
mui:masterfrom
atomiks:codex/remove-combobox-all-values-ref

Conversation

@atomiks

@atomiks atomiks commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  • Removed the retained all-values registry used when items is omitted.
  • Closed-state selected-index synchronization uses the items prop when provided, otherwise the live registry of mounted item values; mounted items re-assert their index while closed so keepMounted/force-mounted lists stay in sync.

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

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

commit: e654011

@code-infra-dashboard

code-infra-dashboard Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+42B(+0.01%) ▼-21B(-0.01%)

Details of bundle changes

Performance

Total duration: 1,298.71 ms -174.83 ms(-11.9%) | Renders: 78 (+0) | Paint: 2,024.85 ms -251.24 ms(-11.0%)

Test Duration Renders
Checkbox mount (500 instances) 83.93 ms 🔺+16.52 ms(+24.5%) 1 (+0)
Tabs mount (200 instances) 244.95 ms ▼-74.80 ms(-23.4%) 4 (+0)
Select mount (200 instances) 149.84 ms ▼-54.52 ms(-26.7%) 3 (+0)
Select open (500 options) 40.09 ms ▼-12.17 ms(-23.3%) 14 (+0)

10 tests within noise — details


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

@netlify

netlify Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit e654011
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a54a8406d07050008769a94
😎 Deploy Preview https://deploy-preview-5198--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 force-pushed the codex/remove-combobox-all-values-ref branch 2 times, most recently from 45f6a6f to 90c46ba Compare July 13, 2026 03:50
@atomiks
atomiks force-pushed the codex/remove-combobox-all-values-ref branch from 90c46ba to 9dc93f9 Compare July 13, 2026 04:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 allValuesRef from the combobox store/state and item registration flow.
  • Updated closed-state selectedIndex synchronization to use flatItems when items is provided, otherwise the live valuesRef registry.
  • Added regression tests covering external value changes while closed and typeahead behavior after open/close without an items prop.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

@atomiks
atomiks marked this pull request as ready for review July 13, 2026 06:37

@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.

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

@atomiks
atomiks merged commit d52e452 into mui:master Jul 13, 2026
7 checks passed
@atomiks
atomiks deleted the codex/remove-combobox-all-values-ref branch July 13, 2026 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants