@@ -417,15 +417,16 @@ export function SearchOverlay({
417
417
418
418
// Handle keyboard navigation of suggestions
419
419
const handleKeyDown = ( event : React . KeyboardEvent < HTMLElement > ) => {
420
+ let optionsLength = listElementsRef . current ?. length ?? 0
420
421
if ( event . key === 'ArrowDown' ) {
421
422
event . preventDefault ( )
422
- if ( combinedOptions . length > 0 ) {
423
+ if ( optionsLength > 0 ) {
423
424
let newIndex = 0
424
425
// If no item is selected, select the first item
425
426
if ( selectedIndex === - 1 ) {
426
427
newIndex = 0
427
428
} else {
428
- newIndex = ( selectedIndex + 1 ) % combinedOptions . length
429
+ newIndex = ( selectedIndex + 1 ) % optionsLength
429
430
// If we go "out of bounds" (i.e. the index is less than the selected index), unselect the item
430
431
if ( newIndex < selectedIndex ) {
431
432
newIndex = - 1
@@ -439,17 +440,23 @@ export function SearchOverlay({
439
440
newIndex += 1
440
441
}
441
442
setSelectedIndex ( newIndex )
443
+ if ( newIndex !== - 1 && listElementsRef . current [ newIndex ] ) {
444
+ listElementsRef . current [ newIndex ] ?. scrollIntoView ( {
445
+ behavior : 'smooth' ,
446
+ block : 'center' ,
447
+ } )
448
+ }
442
449
}
443
450
} else if ( event . key === 'ArrowUp' ) {
444
451
event . preventDefault ( )
445
- if ( combinedOptions . length > 0 ) {
452
+ if ( optionsLength > 0 ) {
446
453
let newIndex = 0
447
454
// If no item is selected, select the last item
448
455
if ( selectedIndex === - 1 ) {
449
- newIndex = combinedOptions . length - 1
456
+ newIndex = optionsLength - 1
450
457
} else {
451
458
// Otherwise, select the previous item
452
- newIndex = ( selectedIndex - 1 + combinedOptions . length ) % combinedOptions . length
459
+ newIndex = ( selectedIndex - 1 + optionsLength ) % optionsLength
453
460
// If we go "out of bounds" (i.e. the index is greater than the selected index), unselect the item
454
461
if ( newIndex > selectedIndex ) {
455
462
newIndex = - 1
@@ -464,6 +471,12 @@ export function SearchOverlay({
464
471
newIndex -= 1
465
472
}
466
473
setSelectedIndex ( newIndex )
474
+ if ( newIndex !== - 1 && listElementsRef . current [ newIndex ] ) {
475
+ listElementsRef . current [ newIndex ] ?. scrollIntoView ( {
476
+ behavior : 'smooth' ,
477
+ block : 'center' ,
478
+ } )
479
+ }
467
480
}
468
481
} else if ( event . key === 'Enter' ) {
469
482
event . preventDefault ( )
@@ -877,6 +890,7 @@ function renderSearchGroups(
877
890
askAIEventGroupId = { askAIState . askAIEventGroupId }
878
891
aiCouldNotAnswer = { askAIState . aiCouldNotAnswer }
879
892
setAICouldNotAnswer = { askAIState . setAICouldNotAnswer }
893
+ listElementsRef = { listElementsRef }
880
894
/>
881
895
</ ActionList . Group > ,
882
896
)
0 commit comments