Skip to content

[combobox] Keep autoHighlight when async results refill through an empty list - #6336

Draft
jjenzz wants to merge 4 commits into
mui:masterfrom
jjenzz:codex/base-ui-issue-681f9cbd-baa7-4044-bfb5-c9411009aec3
Draft

jjenzz wants to merge 4 commits into
mui:masterfrom
jjenzz:codex/base-ui-issue-681f9cbd-baa7-4044-bfb5-c9411009aec3

Conversation

@jjenzz

@jjenzz jjenzz commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Fixes #6323

  • Cause: With autoHighlight, typing while the previous results are still shown highlights the first old result at once. If the app then empties the list while the new search runs, the highlight is cleared because it no longer points at an item, and nothing asks for it again. When the new results arrive nothing is highlighted, so Enter selects nothing.
  • Fix and rationale: When an empty list clears the highlight while the typed query is still active, Combobox now queues the same highlight request that typing creates. The next results resolve it through the existing checks, so it is still dropped when the query is cleared, the popup closes, autoHighlight is turned off or an inline input loses focus. Results that change without emptying keep their current highlight handling.
  • Considered: Highlight the first item whenever the item list changes and nothing is highlighted, as suggested in the issue — It passed the same browser checks but needs an extra ref to detect item changes, and its condition would also bring back a highlight the user cleared whenever the app passes a new array.
  • Considered: Keep the old highlight index while the list is empty so the refilled results land on it — It passed the same browser checks but also has to change the separate effect that clears empty lists, so two effects would depend on each other.
  • Verification: Chromium, jsdom, ESLint, TypeScript, formatting. Regression fails before and passes after the fix.
BEFORE AFTER
before.mp4
after.mp4

@jjenzz jjenzz added component: combobox Changes related to the combobox component. type: bug It doesn't behave as expected. labels Nov 23, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

commit: c52d540

@code-infra-dashboard

code-infra-dashboard Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+86B(+0.02%) 🔺+27B(+0.02%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · precise to ±10.2% (±15.5% at worst) · ran 4m 04s · details


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

@netlify

netlify Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit c52d540
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6b055865f0c106f4a2eb42c2
😎 Deploy Preview https://deploy-preview-6336--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.

@jjenzz

jjenzz commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

PR review

The fix is small and has a clear purpose: when async results go through an empty list (for example, a loading state) after a typed query, it asks again for the first-item highlight. Nothing blocks merge. The one finding is narrow: the new re-arm checks only that the input is non-empty, not that the user typed a query. I read the diff and the enclosing effect but did not run any tests. As instructed, this review was read-only.

Bugs (1)

1. 🟡 Re-armed highlight is not limited to typed queries

Location: packages/react/src/combobox/root/AriaCombobox.tsx:1192-1201

// Results that empty while a typed query is active (such as a loading state) invalidate
// the highlight, not the query it was placed for. Request it again for the next results.
if (
  candidateItems.length === 0 &&
  autoHighlightMode &&
  (hasItems || hasFilteredItemsProp) &&
  String(inputValue).trim() !== ''
) {
  pendingQueryHighlightRef.current = { hasQuery: true };
}

The comment says this applies "while a typed query is active". The guard only checks that inputValue is non-empty.

  • The rest of the code makes { hasQuery: true } requests only from typed input (isTypedInput, around line 699).
  • A non-typed input value can therefore start the same first-item highlight. Two examples: a single-select input that shows the selected label, and a controlled inputValue set by code.
  • With autoHighlight={true} (the 'input-change' mode), the first item should only be highlighted after the input changes.
  • Before this change, that path ended with no highlight. 'always' mode already falls back to index 0 through the storeActiveIndex == null branch, so it is not affected.

Failure scenario:

  1. Set up a single-select Combobox with autoHighlight and items loaded async, with 'b' selected.
  2. Open it from the trigger. The input shows b and b is highlighted.
  3. The consumer refetches on open, so items becomes [] and then ['a', 'b', 'c'].
  4. The new branch re-arms the request, so a is highlighted even though the user never typed.
  5. Pressing Enter to close now selects a instead of keeping b.

It is narrow: the items have to empty while the popup is open and an item is highlighted.

Fix: Only re-arm when the query was actually typed. Gate on queryChangedAfterOpen, which is set only for typed non-empty queries and must be added to the deps, or use an equivalent ref. Optionally, add a sibling negative case: a selected value plus a refill with no typing should not move the highlight to index 0.

Test audit

No actionable test-audit findings.

  • Protected behavior: the new it.each case covers a highlight that was already resolved, then cleared by an empty list, then requested again when results return. This is a separate contract from the sibling test "highlights a candidate arriving after typing", which only covers a request still pending before any results arrive. It is not a duplicate.
  • Can it fail: tracing the base code, after setProps({ items: [] }) the activeIndex is cleared and no request stays pending. On refill, the storeActiveIndex == null branch highlights only in 'always' mode. AsyncCombobox uses autoHighlight ('input-change'), so the data-highlighted / aria-activedescendant assertions would fail on the base code. I traced this in the code; I did not run it.
  • Quality:
    • It asserts the starting state (31 is highlighted) and the in-between empty state (no aria-activedescendant) before acting.
    • It checks real outcomes: the highlighted item, aria-activedescendant and the value Enter commits. It does not check call shapes.
    • It reuses the shared AsyncCombobox fixture and is parametrized over items and filteredItems, both of which use the parameter.
    • It adds no test-only production seam.
  • Retained, not flagged: the test does not cover 'always' mode, which already highlighted index 0 through the existing null branch, or individually rendered items, which the guard deliberately excludes. Neither needs a new test. The only test worth considering is the optional negative case in finding 1.

Verification limits: I did not run the tests, so the fail-before / pass-after behavior is from reading the code only. CI was recorded as green at acfe260b0d17. The commit author is Claude <noreply@anthropic.com>, which matches the attribution rule. The labels (type: bug, component: combobox) still fit, so no label change.

Verdict

Approve after nits - the one finding is a narrow 🟡 edge case where a highlight can be re-armed for a query the user did not type.


🤖 Review generated with Claude Code

A selected label shown in the input is not a typed query, so an open-time
refill through an empty list must not move the highlight off the selection.

Amp-Thread-ID: https://ampcode.com/threads/T-01a20988-ea37-7157-885d-72ec477ce3c2
Co-authored-by: Jenna Smith <175330+jjenzz@users.noreply.github.com>
@jjenzz

jjenzz commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

PR review

Nothing here blocks the merge. The re-arm in AriaCombobox.tsx now runs only when the list empties while the popup is open, autoHighlight is on, the combobox gets data through items or filteredItems, queryChangedAfterOpen is set and the input holds a non-empty query. That addresses the earlier "re-arm only for typed queries" finding. I found one small naming issue in the new negative test. This was a read-only review: I didn't run any tests, so the pass/fail results mentioned come from the recorded history and I didn't reproduce them.

Residual risk I couldn't confirm: queryChangedAfterOpen is only cleared on close, unmount or an interrupted reopen. In an inline single-select combobox, it may still be true after the user picks an item. If async results then pass through an empty list, the highlight would go to index 0 instead of the selected item. I didn't trace the inline selection path far enough to prove this happens, so it is not listed as a finding.

Tests (1)

1. 🟡 Negative test title promises a kept highlight that the test doesn't assert

Location: packages/react/src/combobox/root/ComboboxRoot.test.tsx:9423

it('does not move the selection highlight when results refill through an empty list without typing', async () => {
  ...
  await setProps({ items: [] });
  await setProps({ items: ['31', '32', '33'] });
  expect(screen.getByRole('option', { name: '31' })).not.toHaveAttribute('data-highlighted');

  await user.keyboard('{Enter}');
  expect(onValueChange).not.toHaveBeenCalled();

The title reads as if the highlight stays on the selected item (32) after the refill. The body only checks that 31 is not highlighted and that Enter selects nothing. Without a re-arm, the root effect sets activeIndex to null when the list empties, so after the refill nothing is highlighted, including 32. The test is a valid guard for the queryChangedAfterOpen gate (per the history, it failed at acfe260b0d17). The problem is only that the title doesn't match what it checks.

Failure scenario: Someone reading the title assumes the selection highlight survives an empty-list refill. If a later change restores the highlight to 32, or moves it somewhere other than index 0, nothing here catches it, even though the title says this behavior is covered.

Fix: Rename it to match the assertions, for example 'does not highlight the first item when results refill through an empty list without typing'.

Test audit

Using the pinned test-audit skill, I read SKILL.md and references/audit.md. Its other references (CAMPAIGN.md, docs/help/testing/writing-tests.md, openclaw-testing/SKILL.md, BASE-UI.md) don't exist in the package, so I applied the rules from the two files I could read.

  • Authoring gate, positive it.each test: It covers a real user-facing contract: with autoHighlight, async results that refill after a typed query get their first item highlighted, both in the DOM and in aria-activedescendant, and Enter commits that item. It goes through the public Combobox.Root API, checks the starting highlight before emptying the list, and checks the empty state in between. The items/filteredItems rows go through different branches (hasItems and hasFilteredItemsProp), so the parameter does real work. The existing "highlights a candidate arriving after typing" test doesn't cover this, because it has no prior highlight that gets cleared by an empty list. Per the history, both rows fail with the master AriaCombobox.tsx.
  • Negative control: It fails at the guard it is meant to test (queryChangedAfterOpen). Per the history it failed at acfe260b0d17 and passes at 2a98494c51bb. The only issue is the title, covered in Tests Copy supporting files from the Core repository #1 above.
  • Junk patterns: None. There are no subject-generated expected values, mock-supplied behavior, implementation-coupled assertions, test-only production seams or duplicated contracts. No tests were deleted or weakened.

No other test-audit findings.

Verdict

Approve after nits - the only finding is a 🟡 rename of the new negative test; the earlier re-arm finding is fixed in the current code.


🤖 Review generated with Claude Code

A trigger press opens on the frame after mousedown, so the CI Chromium run could
query the options before the popup mounted. Also name the test after what it asserts.

Co-authored-by: Jenna Smith <175330+jjenzz@users.noreply.github.com>
@jjenzz

jjenzz commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

PR review

No findings, and nothing blocks merge. The change adds one gated re-arm of pendingQueryHighlightRef inside the existing highlight-reconciliation layout effect in AriaCombobox.tsx. It covers one case: the user has typed a query, the async results go empty, then new results arrive. In that case the first new candidate gets highlighted again. The two earlier findings are fixed in the current code: the re-arm is now gated on queryChangedAfterOpen plus a non-empty inputValue, and the negative test's title matches what it asserts. Per instructions, I ran no tests, builds or type checks, so the claims about tests failing without the change come from the PR history, not from my own runs.

What I checked in the current code:

  • Gate conditions: the re-arm fires only when:
    • an active index pointed past an empty list;
    • auto-highlight is on;
    • the list comes from items or filteredItems;
    • the user changed the query after opening;
    • the trimmed input is non-empty.
  • Clean-up paths:
    • A close discards the re-armed request: the [open] layout effect and the "any other open change" branch in handleOpenChange both clear it.
    • Clearing the input drops it on the next pass (String(inputValue).trim() === '').
    • For inline lists, the existing focus guard still drops it after focus leaves the input.
  • Effect ordering: the passive setIndices({ activeIndex: null }) for empty items runs after this layout effect, so the re-arm sees the old index before it is reset. With filteredItems alone that passive reset does not run, and the storeActiveIndex >= candidateItems.length branch handles the reset itself.
  • Effect placement: using an effect here is correct. The trigger is an items/filteredItems prop change, not a user event, and the code it extends already runs in this effect. Adding queryChangedAfterOpen to the dependency list is correct and harmless.
  • Residual risk from round 2 (inline single-select): I traced it. An inline list never goes through the close path or handleUnmount, so queryChangedAfterOpen can stay true after a pick that writes the label into the input. A later refill through [] would then highlight index 0. But while that flag is true, shouldBypassFiltering is false, so the label acts as a live typed filter. Highlighting its first match matches normal typed-query behavior, so this is not an established defect. It is not a finding, and there is no test for it.

Test audit

No findings.

  • Positive it.each test (items / filteredItems):
    • It checks a real user-facing contract: the highlight and aria-activedescendant both land on the refilled first candidate, and Enter commits '32'.
    • The two rows exercise different source branches (hasItems and hasFilteredItemsProp), so this is not duplicate proof.
    • It checks the in-between state (no aria-activedescendant while the list is empty) before the refill.
  • Negative test:
    • It checks the starting state first (the selected '32' is highlighted after opening from the trigger).
    • It then refills without typing and asserts both that '31' is not highlighted and that Enter does not commit.
    • History says it fails when the queryChangedAfterOpen gate is removed. The other gate conditions cannot let it pass by accident: autoHighlight is on, items is provided, and the input holds '32'.
    • The title matches what it asserts.
  • Other checks: neither test asserts on implementation details, mocks the asserted behavior, or adds a test-only hook. Both reuse the shared AsyncCombobox fixture.
  • Verification limit: of the files the test-audit skill references, CAMPAIGN.md (needed only for whole-subsystem campaigns), docs/help/testing/writing-tests.md and BASE-UI.md were not available through Kody. I applied SKILL.md and references/audit.md.

Verdict

Approve - the re-arm is narrowly gated, earlier findings are fixed, and the new tests pin the positive and negative contracts.


🤖 Review generated with Claude Code

…5-c9411009aec3

Co-authored-by: Amp <amp@ampcode.com>
@jjenzz

jjenzz commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

PR review

No findings. Nothing here blocks the merge. The new head merges in a docs-only master commit: git diff 22b887664a29 c52d5408ecd9 -- packages/react/src/combobox is empty, so the combobox change and its tests are the same as the head that was approved last round. I checked the current code independently again.

The source change is in AriaCombobox.tsx, in the highlight layout effect. When the results become empty, the effect requests the highlight again (pendingQueryHighlightRef.current = { hasQuery: true }), but only if all of these hold:

  • autoHighlight is on.
  • The list is data-driven (items or filteredItems).
  • The user typed a non-empty query after the popup opened (queryChangedAfterOpen and a non-blank inputValue).

This fits the existing request lifecycle:

  • The hasQuery branch at the top of the same effect resolves the request on the next non-empty results, or drops it when the query is cleared or autoHighlight is off.
  • A close drops it, both through the [open] layout effect and through handleOpenChange.
  • Adding queryChangedAfterOpen to the dependency list fits the gate.

The expected behavior holds up on its own terms. A typed query asked for the first candidate to be highlighted. An empty loading state in between doesn't change that query, so the first item should be highlighted when the results come back. Opening without typing anchors the highlight on the selection, so it should not be forced onto index 0.

Previous decisions:

  • The earlier findings (re-arm only for a typed query, the negative test title, the Chromium trigger race) are all still fixed in the current code.
  • The leftover concern about inline single-select keeping queryChangedAfterOpen after a pick is unchanged. The recorded rationale still holds: it matches how a typed filter already behaves.

Commit attribution: all four commits (acfe260b0, 2a98494c5, 22b887664, c52d5408e) have Claude <noreply@anthropic.com> as both author and committer. There are no human-authored commits to preserve and no violations.

Remaining risk: the re-arm also overrides a highlight the user moved with the arrow keys before the results emptied. It goes back to index 0, which is the same as what typing does, so I don't count it as a defect. Per the instructions, I didn't run tests. Test results come from the recorded history: the regressions fail with master's AriaCombobox.tsx and pass at head, and on the merge head ComboboxRoot passes 408 tests in jsdom and 467 in Chromium.

Test audit

No actionable test-audit findings.

  • highlights the first candidate when results refill through an empty list with %s (ComboboxRoot.test.tsx:9395). Keep.
    • It is the main regression test for the bug. It runs once each for items and filteredItems, and both rows use the parameter.
    • It checks the state before the action (index 0 highlighted after typing) and the cleared state while the list is empty (no aria-activedescendant).
    • After the refill it checks the user-visible results: data-highlighted, aria-activedescendant, and that Enter commits '32'.
    • Recorded history says it fails with master's code.
  • does not highlight the first item when results refill through an empty list without typing (:9423). Keep.
    • It is a real negative control for the queryChangedAfterOpen gate. Recorded history says it failed at acfe260b0d17, before the gate existed, so it fails at the intended guard rather than an unrelated one.
    • It checks the intermediate state (the selected item is highlighted after opening) before the action.
    • The title now matches what the body asserts.
    • It keeps its one negative assertion outside waitFor.
  • Overlap. These two tests cover a different lifecycle from the existing async tests next to them (waits for a controlled popup to open and its neighbors): a non-empty → empty → non-empty refill. There is no test-only export or seam, and the tests reuse the shared AsyncCombobox fixture.
  • False positives I considered and rejected. The waitFor around the highlight assertion is justified: the trigger opens on the frame after mousedown. Each waitFor holds a single assertion, as AGENTS.md requires.
  • Verification limits. The test-audit skill points to docs/help/testing/writing-tests.md and openclaw-testing/SKILL.md, but Kody couldn't find them in package:pr-inbox, so I used SKILL.md, references/audit.md and BASE-UI.md. I didn't run any mutation or baseline proof myself; the fail-before/pass-after claims come from the recorded history.

Verdict

Approve - the PR's combobox code and tests are unchanged since the approved head, the merge only touches docs, and no new defects or test-audit issues turned up.


🤖 Review generated with Claude Code

This branch has not been deployed

No deployments
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.

[combobox] autoHighlight drops the highlight when async results refill through an empty list

2 participants