Repository navigation
[combobox][menu][select] Add highlightItem imperative action - #5586
Conversation
commit: |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
Bundle size
PerformanceTotal duration: 1,149.10 ms -133.35 ms(-10.4%) | Renders: 76 (+0) | Paint: 1,913.31 ms -182.12 ms(-8.7%)
11 tests within noise — details Metric alarms
Check out the code infra dashboard for more information about this PR. |
5b388d4 to
761c869
Compare
761c869 to
0c06fc2
Compare
0881005 to
4c28121
Compare
|
i haven't looked to closely so i'll leave it to you to decipher if these make sense haha DX reviewThe navigation primitive is useful, but it currently inherits legacy popup-lifecycle behavior that applications cannot opt out of independently. The public contract needs a v2-safe direction before release. Findings (3)1. 🔴
|
|
Yes, the |
4c28121 to
ca947b7
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ca947b7a18
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| export interface Actions { | ||
| unmount: () => void; | ||
| highlightItem: (target: HighlightItemTarget) => void; |
There was a problem hiding this comment.
Preserve assignability of existing actions refs
Making highlightItem required breaks existing consumers that initialize an actions ref with the previously valid shape, such as useRef<Combobox.Root.Actions>({ unmount() {} }); the changes to test/public-types/autocomplete.tsx and combobox.tsx from such initializers to null demonstrate the downstream compilation regression rather than preserving compatibility. The same issue affects the newly required members on Autocomplete, Menu, Context Menu, and Select, so the exported ref type should remain assignable from the prior action shape on a non-major release.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
This follows the precedent set by #5734, which added a required close member to the same Actions interfaces on master; ref initializers with the previous object shape were updated there too. Making highlightItem optional would force every caller into actionsRef.current?.highlightItem?.() for a corner case, so we are keeping it required.
| if (floatingFocusEl && contains(previouslyHighlightedItem, activeEl)) { | ||
| cancelQueuedFocusRef.current?.(); | ||
| floatingFocusEl.focus({ preventScroll: true }); |
There was a problem hiding this comment.
Cancel pending item focus when clearing the highlight
When highlightItem('none') is called after another imperative highlight has committed but before enqueueFocus's animation frame runs, focus is still on the popup, so this condition is false and the queued item focus is not canceled. The pending frame then focuses the now-unhighlighted item, recreating the exact focus/highlight divergence described above and allowing Enter to activate an item that no longer appears highlighted; cancel the queued focus whenever the previous highlighted item exists, before conditionally moving current focus back to the popup.
Useful? React with 👍 / 👎.
Adds `highlightItem(target)` to the `actionsRef` of `Combobox.Root`, `Autocomplete.Root`, `Menu.Root` and `Select.Root`, so apps can bind their own keyboard shortcuts (Ctrl+N/Ctrl+P, the cmdk aliases Ctrl+J/Ctrl+K) on top of the built-in arrow keys. `target` is `'next' | 'previous' | 'first' | 'last' | 'none'`. It mirrors the main-orientation arrow key while the popup is open, but never lands outside the list: `'previous'` on the first item wraps to the last one rather than escaping to the input. Wrapping respects each component's `loopFocus`. It is a no-op while the popup is closed, and `'next'`/`'previous'` are no-ops in Combobox grid mode. Programmatic highlights report `reason: 'imperative-action'`. Closes mui#5146 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…lightItem mui#5734 decoupled automatic unmounting from `actionsRef`, so the notes, the demo's manual `unmount()` call and the regression guard that pinned the old coupling no longer apply. Keep `close` alongside `highlightItem` on the Combobox and Select actions. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
ca947b7 to
01b6637
Compare
Adds the new "Custom keyboard shortcuts" sections to the index so `pnpm docs:validate` passes. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Clearing the highlight now cancels a focus move that is still queued for the next frame and reclaims focus from whichever item holds it, not only the one the highlight points at. Otherwise a queued focus landing after the clear re-highlighted the item, and a clear that followed a move in the same tick left focus on an item that no longer looked highlighted. Tests: assert the popup receives focus on clear, prove closed-popup calls are dropped rather than queued, await the open sequence before driving the highlight, cover the inline cursor after a clear, virtualized targets beyond the rendered window, entry from the selected Select item, the `'none'` payload, arrows after an imperative move, and the hook's own `source` argument and no-op guards. Docs: explain that a same-event open-and-highlight is dropped, document where the key handler goes for Menu and Select, state the wrap rule and the focus handoff on the actionsRef props, describe the `'none'` reason by its sources, and give the HighlightItemTarget aliases JSDoc. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
|
PR review all good, DX review incoming... PR reviewNo findings. Targeted tests passed: 42 in jsdom and 45 in Chromium, including focus cancellation and virtualized navigation. The full suite and typecheck were not run. VerdictApprove — no actionable correctness, test, simplification, or documentation issues found. 🤖 Review generated with Codex |
DX reviewThe navigation action is a useful, lasting addition. No blocking API or composition issues found; one documentation correction remains. Findings (1)1. 🟡 Combobox documents an unsupported
|
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ctionsRef docs Combobox's autoHighlight prop is boolean only; the 'always' mode belongs to Autocomplete, whose docs keep the note. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Combobox drives grids with a horizontal main orientation, so ArrowRight and ArrowLeft already step through the cells in DOM order and cross row boundaries. `'next'` and `'previous'` now do the same instead of being a no-op, which drops the special case and its caveats. Bundle size: every entry that includes the hook shrinks by 33 B parsed and 10-17 B gzipped; nothing grows. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
An inline grid Combobox with buttons and shortcuts for every highlightItem target, plus a log of onItemHighlighted events, to exercise DOM-order stepping across row boundaries alongside the arrow keys. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The grid navigator returns the unchanged index for keys it does not handle, and the key handler treated any non-null result as a navigation, so every keydown in a grid re-emitted onItemHighlighted for the current item, and ArrowLeft/ArrowRight reported it twice. Also type the experiment's log entry as unknown, which is what onItemHighlighted hands an untyped Combobox. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ntract to the reference Per review: the Autocomplete and Combobox pages now introduce the action in a couple of sentences around the demo, the virtualization sections carry the scroll-on-'imperative-action' note, and the full contract (sequential traversal including grids, wrapping, clearing, the closed-popup rule and the reported reason) lives in the actionsRef JSDoc on every root. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Resolves the SelectRoot import conflict with mui#5589, which removed the root props context: keep the HighlightItemTarget import alongside master's reduced context import. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolves conflicts with mui#5586 (highlightItem) and mui#5734 (unmount decoupling): - AriaCombobox: keep scrollItemIntoView alongside the new onNavigate source/inline handling; drop the now-unused useOpenChangeComplete import. - Combobox virtualized demos keep the built-in Virtualizer; the `imperative-action` scroll handling moves to the TanStack demos, and the docs note to the third-party section. - Add `imperative-action` to ListHighlightReason so imperative highlights scroll the built-in virtualizer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The grid guard in useListNavigation is a standalone fix that mui#5586 shipped, not part of highlightItem. Without it, keys the grid does not handle re-report the current highlight, which corrupts inline Autocomplete input in grid mode. Also keep three onItemHighlighted tests from mui#5527 that cover live syncHighlightedItem behavior, now set up through keyboard and hover.
Closes #5146
Supersedes #4815
AutocompleteandComboboxnavigate the list with the arrow keys, but there is no way for an app to drive the highlight itself — so bindings like Ctrl+N/Ctrl+P (cmdk's defaults, and also standard macOS text navigation) can't be added in application code. Rather than adding vim-specific keys to the library, this adds the missing primitive.API
highlightItem(target)on theactionsRefofCombobox.Root,Autocomplete.Root,Menu.RootandSelect.Root:type HighlightItemTarget = 'next' | 'previous' | 'first' | 'last' | 'none', exported per namespace as<Component>.Root.HighlightItemTarget. No numeric index — indices are unstable under filtering, grouping and virtualization.Semantics
'next'enters at the first item and'previous'at the last.'previous'on the first item wraps to the last item, never back to the input — the method ishighlightItem, so landing on the input would be wrong.loopFocus. Note Select doesn't loop, so it never wraps.'next'/'previous'are no-ops in Comboboxgridmode.reason: 'imperative-action'toonItemHighlighted.'none', which hands focus back to the popup rather than stranding it on a now-unhighlighted item.Implemented once in
useListNavigation, so the virtual-focus (Combobox/Autocomplete) and DOM-focus (Menu/Select) components share a single traversal implementation.Known limitations, deliberately
setOpen(true); highlightItem('first')in one tick is a no-op, becauseopenis still false at call time. There is no queued/pending highlight.'imperative-action'reason, and the docs say to do the same.Not included
Home/End/PageUp/PageDownlist navigation — a separate feature with its own APG questions.stepListIndex()sohighlightItemandcommonOnKeyDowndon't each implement the traversal maths. Worth doing, but it rewrites arrow-key navigation for every list component in the library and belongs in its own PR.Demo
https://deploy-preview-5586--base-ui.netlify.app/react/components/autocomplete#custom-keyboard-shortcuts
Note on the demo's bindings
The demo binds Ctrl+N/Ctrl+P only. cmdk also maps Ctrl+J/Ctrl+K, but Ctrl+K opens the search on this docs site, so a demo advertising it would not work on the page it ships on.
Testing
22 new tests, including the issue's actual scenario (Ctrl+N/P driving an Autocomplete end to end, plus Ctrl+J/K to show the action doesn't care which key drives it), the wrap-to-last rule,
loopFocuson and off, grid no-ops, closed-popup no-ops, the'imperative-action'reason, and DOM-focus handoff on'none'for Menu and Select.🤖 Generated with Claude Code