fix(Typeahead): omit aria-activedescendant when search results are empty (#4059)#4312
Open
HelloOjasMutreja wants to merge 1 commit into
Open
Conversation
…pty (facebook#4059) When keyboard navigation (ArrowDown, Home) was used in a Typeahead with no matching results, highlightedIndex was set to 0 and aria-activedescendant emitted an ID pointing to option-0. Because 0 option nodes exist in an empty results listbox, this left assistive technology with a dangling reference. Guards keyboard navigation to only highlight items when results exist, and validates highlightedIndex < results.length in aria-activedescendant and the scrollIntoView effect. Includes unit test coverage and a patch changeset. Closes facebook#4059
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
HelloOjasMutreja
marked this pull request as ready for review
July 25, 2026 05:54
HelloOjasMutreja
requested review from
cixzhang,
ejhammond and
imdreamrunner
as code owners
July 25, 2026 05:54
Contributor
PR Analysis ReportModified ComponentsTypeahead
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | View full report |
Contributor
Author
Technical Verification & Implementation Detail
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4059.
When keyboard navigation (
ArrowDown,Home) was used in aTypeaheadwith no matching search results,highlightedIndexwas set to0andaria-activedescendantemitted an ID pointing to<listbox-id>-option-0. Because 0 option nodes exist in an empty results listbox, this left assistive technology with a dangling reference to a non-existent DOM element.Root cause
BaseTypeahead.tsxhandleKeyDown,ArrowDownevaluated:setHighlightedIndex(prev => prev < results.length - 1 ? prev + 1 : 0)When
results.length === 0,results.length - 1is-1.-1 < -1evaluated tofalse, causinghighlightedIndexto evaluate to0.Homeunconditionally setsetHighlightedIndex(0).aria-activedescendantcheckedpopover.isOpen && highlightedIndex >= 0, which evaluated totruewhenhighlightedIndex === 0even thoughresults.length === 0.Fix
BaseTypeahead.tsx:ArrowDown,ArrowUp,Home, andEndinhandleKeyDownto only update highlight whenresults.length > 0.aria-activedescendant:popover.isOpen && highlightedIndex >= 0 && highlightedIndex < results.length. Omitsaria-activedescendantwhen results are empty.scrollIntoVieweffect (highlightedIndex < results.length).performSearch&performBootstrapto initialize highlight withshown.length > 0 ? 0 : -1(the actual sliced results array).Testing
Typeahead.test.tsxverifying thataria-activedescendantis absent when search results are empty, and remains absent after pressingArrowDownandHome.Typeahead.test.tsxpass.@astryxdesign/corepackage built cleanly (Babel + tsc + CSS + UMD).