Repository navigation
fix(core): select by typing on a focused Selector (#3764) - #3797
Conversation
Typing a printable character on a focused, closed Selector did nothing —
the keydown only opened the popup and moved a highlight, so a state picker
could not be driven the way a native <select> can ("C" -> "CA"). Typing on
the closed trigger now commits the matching option without opening the menu.
Repeated presses cycle through options sharing a first letter (wrapping past
the current selection), spaces count as match characters so "new y" reaches
"New York", and the buffer resets after 500ms. Menu-open behavior is
unchanged: typing moves the highlight and Enter commits. With hasSearch,
typing on the closed trigger opens the popup and appends to the search query,
so characters racing the focus handoff are not dropped.
The commit path changes the value without opening the popup or moving focus,
so a polite live region announces the new selection.
useCombobox gains hasTypeahead (default true). CommandPalette passes false:
its own input already filters the items and forwards every keystroke to the
hook, so prefix matching on top of that would drag the highlight — and what
Enter commits — onto a command the user never picked.
The typeahead anchor and duplicate-select guard read the optimistic value
rather than the raw prop, so a pending changeAction cannot strand cycling on
the first match or re-fire the action for a value already committed.
Claude-Session: https://claude.ai/code/session_01LvZ7hEU9jDyeZm4YTStXzt
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
cixzhang
left a comment
There was a problem hiding this comment.
Thank you for working on this! I left some comments mainly to reuse existing a11y hooks so we're not applying behaviors inconsistently.
| // keystroke here. Prefix typeahead on top of that would chase the same | ||
| // text and drag the highlight — and what Enter commits — off the user's | ||
| // pick, so the input stays the only thing typing drives. | ||
| hasTypeahead: false, |
There was a problem hiding this comment.
This option probably doesn't make sense for the command palette since it's already a search input form of typeahead so it naturally performs this behavior. We'd only really need the behavior on selectors.
There was a problem hiding this comment.
hasTypeahead is gone. Typeahead lives in the shared useTypeahead hook now, so CommandPalette gets it as a search input with no opt-out to carry.
| // Announces a value committed by typing on the closed trigger. That path | ||
| // changes the selection without opening the popup or moving focus, so no | ||
| // other event prompts assistive tech to re-read the trigger. | ||
| const [liveMessage, setLiveMessage] = useState(''); |
There was a problem hiding this comment.
Hmm it can be finicky to manage this manually but using state seems odd. Are you able to use useAnnounce instead?
There was a problem hiding this comment.
Switched to useAnnounce. The liveMessage state and the inline VisuallyHidden region are removed.
| onSelect: useCallback( | ||
| (newValue: string) => { | ||
| onChange?.(newValue); | ||
| if (!popover.isOpen) { |
There was a problem hiding this comment.
Hmm it should be possible to have the typeahead behavior when it's open (it should move focus to the item that matches) so we shouldn't need this gate.
There was a problem hiding this comment.
Gate removed. Open, it walks the highlight via aria-activedescendant; closed, it commits like a native select. It moves the virtual cursor rather than DOM focus, since the trigger keeps focus and the popup is the role="listbox", so that is the combobox pattern here.
| // clearing all end the "word" being typed. Without this, a stale prefix | ||
| // survives for up to TYPEAHEAD_RESET_MS and poisons the next keystroke: | ||
| // selecting Dog then typing "c" would search for "dc" and match nothing. | ||
| const resetTypeahead = useCallback(() => { |
There was a problem hiding this comment.
Are we able to reuse useTypeahead instead of rolling something separately?
There was a problem hiding this comment.
Done. The separate matcher is deleted and it composes the shared useTypeahead ahead of the combobox key handler; reset fires on close and on clear so no stale prefix survives. Adopting it surfaced a few off-by-one bugs in the shared hook (single-char offset and the -1 start), fixed in this PR, so DropdownMenu/ContextMenu/NavHeadingMenu benefit too. More detail in the PR description.
|
Dug into both open review questions against the current diff — concrete answers on both:
|
Answers the four review threads on meta#3797. useCombobox no longer implements matching at all: findTypeaheadMatch and handleTypeahead are deleted and Selector composes the shared useTypeahead ahead of the combobox key handler, so type-to-select behaves like the other collections. With nothing left to opt out of, CommandPalette's hasTypeahead flag goes away too — its own input stays the only thing typing drives. The gate that limited matching to the closed trigger is gone. Typing with the menu open now walks the highlight; because the trigger keeps focus and owns aria-activedescendant here, that is the same model arrow-key navigation already uses. The hand-rolled liveMessage state and its inline VisuallyHidden live region are replaced by useAnnounce, called only on the closed-trigger commit — the one path where nothing else prompts a re-read. Adopting the shared hook surfaced two matching bugs in it, fixed here, so DropdownMenu, ContextMenu and NavHeadingMenu benefit as well: - A single-character search now starts after the current item, matching a native <select> and APG getIndexByLetter. It previously advanced only on a repeated press, so pressing a letter the current item already begins with was a dead keystroke — one per reset window. - getCurrentIndex() returning -1 now really searches from the top. The modulo was wrapping it onto the last item instead, which every menu caller hits whenever nothing is focused; the hook's own JSDoc already documented the behavior now implemented. Selector's clamp workaround for this is removed. Characters composed with Option/Alt (Option+a -> "a-ring") count as typeahead again; real chords still carry ctrl or meta, and AltGr sets ctrlKey. Test debt from the rework is paid off: the pending-changeAction test was vacuous on a two-option fixture, and two more tests rendered an uncontrolled Selector that pinned the match anchor at 0, so none of them could exercise the anchor. The buffer reset on clear, and the deliberate silence when matching with the menu open, had no coverage at all. Every fix here is mutation-verified — reverting each one makes exactly the intended test fail.
…a#3764) An edge-case pass over the typeahead rework. One real bug: a Space chorded with ctrl or meta was consumed mid-buffer and appended a raw space, poisoning the buffer for the rest of the reset window — the file's own comment already claimed real chords carry ctrl or meta, and Space was the one character escaping it. isSpaceMidType now excludes both modifiers. Space is the only character a chord could not already suppress, so the fix is one line. New tests pin the edges the rework leans on: useTypeahead — chord-Space rejection (ctrl and meta); a Space extending a live buffer (the positive path that had no coverage, which is why the chord hole survived); null/empty/whitespace label slots keeping their index; a negative sentinel and a stale out-of-range current index never searching backwards or reporting out of range; a single-item list. Selector — matching keys off the label while committing the value; matching across sections while ignoring dividers and group titles; aria-active- descendant landing on the match after wrapping into a section; a value matching no option staying anchored at the top. DropdownMenu — pressing the letter the focused item already begins with advances to the next match, pinning the shared-hook offset change at the consumer level (red on the parent commit). Mutation-verified: reverting the offset rule fails the DropdownMenu test, matching on value fails the label test, weakening the null guard fails the label-slots test.
|
Thanks @HelloOjasMutreja. This is the direction the PR landed on, reusing the shared |
…ypeahead # Conflicts: # packages/core/src/Selector/Selector.test.tsx # packages/core/src/Selector/Selector.tsx
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR Modified ComponentsCommandPalette (@astryxdesign/core) · View in Storybook
DropdownMenu (@astryxdesign/core) · View in Storybook
Selector (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
cixzhang
left a comment
There was a problem hiding this comment.
Thanks this is looking good now
…eahead Resolved two test-file conflicts by keeping both sides: - Selector.test.tsx: kept main's generateThemeTestCSS helper; dropped main's duplicate politeRegion() (the PR moved the same helper further down the file). - CommandPalette.test.tsx: kept the PR's 'does not move the highlight while typing' test and main's 'screen reader announcements' describe block.
|
@cixzhang is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @AKnassa — this had drifted ~306 commits behind Both conflicts were in test files only; production code merged clean:
Verified on the merged tree: no test cases lost (Selector 108, CommandPalette 20 — the exact union of both sides), Nothing needed from you — just flagging that the branch moved. |
|
Thank you so much! |
Fixes #3764
What this does
You can now pick an option in a Selector just by typing. Focus the dropdown with Tab, press "C", and it selects "CA" — no need to open the menu and hunt for the option first. This is how the browser's own dropdowns have always worked, and Selector now matches that, so it can genuinely replace them.
Why
The person who reported this was building a state picker. They could tab to it, but then had to stop and interact with the menu to choose anything — which makes a form slow to fill in by keyboard and made Selector a poor swap for a native dropdown.
What changed
useTypeaheadhook, not in the shared combobox. CommandPalette and MultiSelector get nothing extra, and the palette is already a search input, so it needs no opt-out.Menu-open behavior is unchanged for the user: typing walks the highlight and Enter confirms. The same shared hook now drives it, moving the highlight via
aria-activedescendant.How to see it
In Storybook, open Core → Selector → Default, click once outside the dropdown, then press Tab to focus it and type a letter. The value changes with the menu closed.
Notes for reviewers
useTypeaheadhook, run ahead of the combobox key handler. Nothing was added touseCombobox, so CommandPalette and MultiSelector are untouched.useAnnouncerather than local live-region state.<select>/ APGgetIndexByLetter); a-1start no longer wraps onto the last item; and a ctrl/meta-chorded Space no longer appends a raw space that poisons the buffer. DropdownMenu, ContextMenu, and NavHeadingMenu share the hook and benefit.valueprop, so a pendingchangeActioncan't strand cycling on the first match or re-fire the action for a value already committed.useAnnouncemounts its live region ondocument.body, so a Selector inside a modal Dialog won't announce — a limitation shared by every currentuseAnnounceconsumer, not specific to this change.