Skip to content

Commit 786305c

Browse files
[Accessibility Audit] Copilot Search scroll on focus (#55412)
Co-authored-by: Copilot <[email protected]>
1 parent 5862cad commit 786305c

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/search/components/input/AskAIResults.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type AIQueryResultsProps = {
3030
askAIEventGroupId: React.MutableRefObject<string>
3131
aiCouldNotAnswer: boolean
3232
setAICouldNotAnswer: (aiCouldNotAnswer: boolean) => void
33+
listElementsRef: React.RefObject<Array<HTMLLIElement | null>>
3334
}
3435

3536
type AISearchResultEventParams = {
@@ -56,6 +57,7 @@ export function AskAIResults({
5657
askAIEventGroupId,
5758
aiCouldNotAnswer,
5859
setAICouldNotAnswer,
60+
listElementsRef,
5961
}: AIQueryResultsProps) {
6062
const router = useRouter()
6163
const { t } = useTranslation('search')
@@ -396,6 +398,7 @@ export function AskAIResults({
396398
if (index >= MAX_REFERENCES_TO_SHOW) {
397399
return null
398400
}
401+
const refIndex = index + referencesIndexOffset
399402
return (
400403
<ActionList.Item
401404
sx={{
@@ -408,7 +411,12 @@ export function AskAIResults({
408411
onSelect={() => {
409412
referenceOnSelect(source.url)
410413
}}
411-
active={index + referencesIndexOffset === selectedIndex}
414+
active={refIndex === selectedIndex}
415+
ref={(element) => {
416+
if (listElementsRef.current) {
417+
listElementsRef.current[refIndex] = element
418+
}
419+
}}
412420
>
413421
<ActionList.LeadingVisual aria-hidden="true">
414422
<FileIcon />

src/search/components/input/SearchOverlay.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,16 @@ export function SearchOverlay({
417417

418418
// Handle keyboard navigation of suggestions
419419
const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
420+
let optionsLength = listElementsRef.current?.length ?? 0
420421
if (event.key === 'ArrowDown') {
421422
event.preventDefault()
422-
if (combinedOptions.length > 0) {
423+
if (optionsLength > 0) {
423424
let newIndex = 0
424425
// If no item is selected, select the first item
425426
if (selectedIndex === -1) {
426427
newIndex = 0
427428
} else {
428-
newIndex = (selectedIndex + 1) % combinedOptions.length
429+
newIndex = (selectedIndex + 1) % optionsLength
429430
// If we go "out of bounds" (i.e. the index is less than the selected index), unselect the item
430431
if (newIndex < selectedIndex) {
431432
newIndex = -1
@@ -439,17 +440,23 @@ export function SearchOverlay({
439440
newIndex += 1
440441
}
441442
setSelectedIndex(newIndex)
443+
if (newIndex !== -1 && listElementsRef.current[newIndex]) {
444+
listElementsRef.current[newIndex]?.scrollIntoView({
445+
behavior: 'smooth',
446+
block: 'center',
447+
})
448+
}
442449
}
443450
} else if (event.key === 'ArrowUp') {
444451
event.preventDefault()
445-
if (combinedOptions.length > 0) {
452+
if (optionsLength > 0) {
446453
let newIndex = 0
447454
// If no item is selected, select the last item
448455
if (selectedIndex === -1) {
449-
newIndex = combinedOptions.length - 1
456+
newIndex = optionsLength - 1
450457
} else {
451458
// Otherwise, select the previous item
452-
newIndex = (selectedIndex - 1 + combinedOptions.length) % combinedOptions.length
459+
newIndex = (selectedIndex - 1 + optionsLength) % optionsLength
453460
// If we go "out of bounds" (i.e. the index is greater than the selected index), unselect the item
454461
if (newIndex > selectedIndex) {
455462
newIndex = -1
@@ -464,6 +471,12 @@ export function SearchOverlay({
464471
newIndex -= 1
465472
}
466473
setSelectedIndex(newIndex)
474+
if (newIndex !== -1 && listElementsRef.current[newIndex]) {
475+
listElementsRef.current[newIndex]?.scrollIntoView({
476+
behavior: 'smooth',
477+
block: 'center',
478+
})
479+
}
467480
}
468481
} else if (event.key === 'Enter') {
469482
event.preventDefault()
@@ -877,6 +890,7 @@ function renderSearchGroups(
877890
askAIEventGroupId={askAIState.askAIEventGroupId}
878891
aiCouldNotAnswer={askAIState.aiCouldNotAnswer}
879892
setAICouldNotAnswer={askAIState.setAICouldNotAnswer}
893+
listElementsRef={listElementsRef}
880894
/>
881895
</ActionList.Group>,
882896
)

0 commit comments

Comments
 (0)