@@ -84,6 +84,7 @@ export function SearchOverlay({
8484 const [ aiSearchError , setAISearchError ] = useState < boolean > ( false )
8585 const [ aiReferences , setAIReferences ] = useState < AIReference [ ] > ( [ ] as AIReference [ ] )
8686 const [ aiCouldNotAnswer , setAICouldNotAnswer ] = useState < boolean > ( false )
87+ const [ showSpinner , setShowSpinner ] = useState ( false )
8788
8889 // Group all events between open / close of the overlay together
8990 const searchEventGroupId = useRef < string > ( '' )
@@ -110,6 +111,26 @@ export function SearchOverlay({
110111 const { aiAutocompleteOptions, generalSearchResults, totalGeneralSearchResults } =
111112 autoCompleteOptions
112113
114+ // Whenever "searchLoading" changes, decide whether to show the spinner after 1s.
115+ useEffect ( ( ) => {
116+ let timer : ReturnType < typeof setTimeout >
117+
118+ // If it's the initial fetch, show the spinner immediately
119+ if ( ! aiAutocompleteOptions . length && ! generalSearchResults . length ) {
120+ return setShowSpinner ( true )
121+ }
122+
123+ if ( searchLoading ) {
124+ timer = setTimeout ( ( ) => setShowSpinner ( true ) , 1000 )
125+ } else {
126+ setShowSpinner ( false )
127+ }
128+
129+ return ( ) => {
130+ clearTimeout ( timer )
131+ }
132+ } , [ searchLoading , aiAutocompleteOptions . length , generalSearchResults . length ] )
133+
113134 // Filter out any options that match the local query and replace them with a custom user query option that include isUserQuery: true
114135 const filteredAIOptions = aiAutocompleteOptions . filter (
115136 ( option ) => option . term !== urlSearchInputQuery ,
@@ -225,10 +246,10 @@ export function SearchOverlay({
225246
226247 // When loading, capture the last height of the suggestions list so we can use it for the loading div
227248 const previousSuggestionsListHeight = useMemo ( ( ) => {
228- if ( suggestionsListHeightRef . current ?. clientHeight ) {
229- return suggestionsListHeightRef . current . clientHeight
249+ if ( generalSearchResults . length || aiAutocompleteOptions . length ) {
250+ return 7 * ( generalSearchResults . length + aiAutocompleteOptions . length ) + ''
230251 } else {
231- return '250 ' // Default height that looks very close to 5 suggestions (in px)
252+ return '150 ' // Default height for just 2 suggestions
232253 }
233254 } , [ searchLoading ] )
234255
@@ -461,6 +482,9 @@ export function SearchOverlay({
461482 showDividers
462483 className = { styles . suggestionsList }
463484 ref = { suggestionsListHeightRef }
485+ sx = { {
486+ minHeight : `${ previousSuggestionsListHeight } px` ,
487+ } }
464488 >
465489 { /* Always show the AI Search UI error message when it is needed */ }
466490 { aiSearchError && (
@@ -520,7 +544,7 @@ export function SearchOverlay({
520544 selectedIndex ,
521545 listElementsRef ,
522546 askAIState ,
523- searchLoading ,
547+ showSpinner ,
524548 previousSuggestionsListHeight ,
525549 ) }
526550 </ ActionList >
@@ -533,6 +557,9 @@ export function SearchOverlay({
533557 showDividers
534558 className = { styles . suggestionsList }
535559 ref = { suggestionsListHeightRef }
560+ sx = { {
561+ minHeight : `${ previousSuggestionsListHeight } px` ,
562+ } }
536563 >
537564 { renderSearchGroups (
538565 t ,
@@ -544,7 +571,7 @@ export function SearchOverlay({
544571 selectedIndex ,
545572 listElementsRef ,
546573 askAIState ,
547- searchLoading ,
574+ showSpinner ,
548575 previousSuggestionsListHeight ,
549576 ) }
550577 </ ActionList >
@@ -687,7 +714,7 @@ function renderSearchGroups(
687714 aiCouldNotAnswer : boolean
688715 setAICouldNotAnswer : ( value : boolean ) => void
689716 } ,
690- searchLoading : boolean ,
717+ showSpinner : boolean ,
691718 previousSuggestionsListHeight : number | string ,
692719) {
693720 const groups = [ ]
@@ -733,7 +760,7 @@ function renderSearchGroups(
733760 groups . push ( < ActionList . Divider key = "no-answer-divider" /> )
734761 }
735762
736- if ( searchLoading ) {
763+ if ( showSpinner ) {
737764 groups . push (
738765 < Box
739766 key = "loading"
0 commit comments