Skip to content

[combobox] Fix stale chip focus after returning to the input - #5879

Merged
atomiks merged 6 commits into
mui:masterfrom
mdm317:fix/combobox-chip-tab-navigation
Oct 6, 2026
Merged

atomiks merged 6 commits into
mui:masterfrom
mdm317:fix/combobox-chip-tab-navigation

Conversation

@mdm317

@mdm317 mdm317 commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

reproduction

  1. go https://base-ui.com/react/components/combobox#multiple-select
  2. Select multiple values, then focus the empty input.
  3. Press ArrowLeft to focus the last chip.
  4. Press Tab to return to the input.
  5. Press Backspace or ArrowLeft:
    • Backspace does not remove the last chip.
    • ArrowLeft focuses the second-to-last chip instead of the last chip.

Expected behavior

Backspace removes the last chip, and ArrowLeft focuses the last chip.

@pkg-pr-new

pkg-pr-new Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

commit: 2357088

@code-infra-dashboard

code-infra-dashboard Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react ▼-518B(-0.10%) ▼-150B(-0.09%)

Details of bundle changes

Performance

Total duration: 399.21 ms -29.24 ms(-6.8%) | Renders: 76 (+0) | Paint: 728.31 ms -69.05 ms(-8.7%)

Test Duration Renders
Select open (500 options) 23.64 ms ▼-6.56 ms(-21.7%) 14 (+0)
Combobox type — 500 items, all stay mounted (type "Row ") 11.87 ms ▼-3.07 ms(-20.5%) 11 (+0)

13 tests within noise — details


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

@netlify

netlify Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

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

@mdm317
mdm317 marked this pull request as ready for review October 4, 2026 19:21
@atomiks atomiks added type: bug It doesn't behave as expected. component: combobox Changes related to the combobox component. labels Oct 5, 2026
@atomiks

atomiks commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

PR review

The PR fixes the reported keyboard path, but the stale chip highlight it targets still happens when focus leaves a chip any way other than Tab. Clicking back into the input or clicking away and tabbing back still breaks Backspace and ArrowLeft. Because of that, the fix is incomplete and blocks merge. I checked both missed paths with jsdom probes against e3a49da62b; I didn't run Chromium.

Bugs (1)

1. 🔴 Stale chip highlight survives every focus exit except Tab

Location: packages/react/src/combobox/chip/ComboboxChip.tsx:111

if (event.key === 'Tab') {
  setHighlightedChipIndex(undefined);
  return;
}

highlightedChipIndex is only cleared from a chip's own keydown or when the popup opens (ComboboxChips.tsx:32). The PR adds Tab and Shift+Tab to the keydown path. Any other way of leaving the chip still leaves the index set. The input's keydown then takes the highlighted-chip branch (ComboboxInput.tsx:161): ArrowLeft goes to index − 1, and Backspace skips the "remove last chip" path because that path requires highlightedChipIndex === undefined.

Failure scenario: Both of these fail on the PR head (no onValueChange call):

  • With openOnInputClick={false}: focus the last chip with ArrowLeft, click the input, press Backspace. No chip is removed.
  • With default props: focus the last chip with ArrowLeft, click outside the combobox, Tab back into the input, press Backspace. No chip is removed.

Fix: Clear the highlight when the input gains focus, and drop the Tab special case in the chip:

onFocus() {
  setFocused(true);
  comboboxChipsContext?.setHighlightedChipIndex(undefined);
  // ...

This passes both probes and the PR's two new tests. The rest of the Combobox jsdom suite passes except the test covered in Tests #1.

Tests (1)

1. 🟠 An existing test pins the stale-highlight behavior, and the new tests only cover Tab

Location: packages/react/src/combobox/input/ComboboxInput.test.tsx:609

input.focus();
await user.keyboard('{ArrowLeft}');
expect(banana).toHaveFocus();

"navigates an existing chip highlight when focus returns to the input" calls input.focus() and expects the old chip highlight to keep driving arrow navigation. That is the same behavior this PR treats as a bug. Any root-cause fix (input onFocus or chip blur) fails it, so it needs updating rather than keeping. The two new tests only reach the input through user.tab().

Failure scenario: A later change to the chip keydown path could bring back the mouse and blur variants of this bug without any test failing. The fix in Bugs #1 is also blocked by the outdated test at line 609.

Fix: Change the line-609 test to expect that focusing the input clears the highlight (ArrowLeft goes to the last chip). Add cases for clicking the input with openOnInputClick={false} and for clicking outside then tabbing back.

Simplifications (1)

1. ℹ️ The input-side highlighted-chip branch is nearly dead once focus clears the highlight

Location: packages/react/src/combobox/input/ComboboxInput.tsx:161

if (highlightedChipIndex !== undefined) {
  if (event.key === previousChipKey) {

Chips receive real focus, so with the input clearing the highlight in onFocus, this branch only runs if chipsRef.current[nextIndex]?.focus() fails to move focus.

Failure scenario: About 25 lines of keyboard handling stay in the bundle to serve a state that can no longer be reached in practice.

Fix: Optional follow-up: remove the branch and the highlightedChipIndex check in the Backspace guard, then measure the bundle-size change. This can be left out of this PR.

Verdict

Request changes - the fix covers only the Tab path, so the same Backspace/ArrowLeft bug is still reachable by mouse or by clicking away and tabbing back.


🤖 Review generated with Claude Code

@mdm317

mdm317 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

I’ve addressed the review

Claude’s review recommended updating the existing test to expect the chip highlight to be cleared when the input receives focus.
Could you confirm whether it’s appropriate to remove the logic that preserves the highlight in this case?

@atomiks

atomiks commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Could you confirm whether it’s appropriate to remove the logic that preserves the highlight in this case?

Yep, that is fine

More simplifications could be done here, applied them

@atomiks atomiks changed the title [combobox] Clear chip highlight when pressing Tab [combobox] Fix stale chip focus after returning to the input Oct 6, 2026
@atomiks
atomiks merged commit 3955ac7 into mui:master Oct 6, 2026
22 checks passed
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.

2 participants