File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,6 +43,9 @@ export function DocSearch({
4343 const dropdownRef = React . useRef < HTMLDivElement | null > ( null ) ;
4444 const inputRef = React . useRef < HTMLInputElement | null > ( null ) ;
4545 const snipetLength = React . useRef < number > ( 10 ) ;
46+ const initialQuery = React . useRef (
47+ typeof window !== 'undefined' ? window . getSelection ( ) ! . toString ( ) : ''
48+ ) . current ;
4649
4750 const searchClient = React . useMemo ( ( ) => createSearchClient ( appId , apiKey ) , [
4851 appId ,
@@ -90,10 +93,7 @@ export function DocSearch({
9093 placeholder : 'Search docs...' ,
9194 openOnFocus : true ,
9295 initialState : {
93- query :
94- typeof window !== 'undefined'
95- ? window . getSelection ( ) ! . toString ( )
96- : '' ,
96+ query : initialQuery ,
9797 } ,
9898 onStateChange ( { state } ) {
9999 setState ( state as any ) ;
@@ -230,6 +230,7 @@ export function DocSearch({
230230 recentSearches ,
231231 favoriteSearches ,
232232 saveRecentSearch ,
233+ initialQuery ,
233234 ]
234235 ) ;
235236
@@ -291,6 +292,7 @@ export function DocSearch({
291292 < SearchBox
292293 { ...autocomplete }
293294 state = { state }
295+ initialQuery = { initialQuery }
294296 onClose = { onClose }
295297 inputRef = { inputRef }
296298 />
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ interface SearchBoxProps
1717 React . KeyboardEvent
1818 > {
1919 state : AutocompleteState < InternalDocSearchHit > ;
20+ initialQuery : string ;
2021 inputRef : MutableRefObject < HTMLInputElement | null > ;
2122 onClose ( ) : void ;
2223}
@@ -25,6 +26,17 @@ export function SearchBox(props: SearchBoxProps) {
2526 const { onSubmit, onReset } = props . getFormProps ( {
2627 inputElement : props . inputRef . current ,
2728 } ) ;
29+ const { initialQuery, refresh } = props ;
30+
31+ // We don't focus the input when there's an initial query because users
32+ // rather want to see the results directly. We therefore need to refresh
33+ // the autocomplete instance to load all the results, which is usually
34+ // triggered on focus.
35+ React . useEffect ( ( ) => {
36+ if ( initialQuery . length > 0 ) {
37+ refresh ( ) ;
38+ }
39+ } , [ initialQuery , refresh ] ) ;
2840
2941 return (
3042 < >
@@ -48,6 +60,7 @@ export function SearchBox(props: SearchBoxProps) {
4860 className = "DocSearch-Input"
4961 ref = { props . inputRef }
5062 { ...props . getInputProps ( {
63+ autoFocus : props . initialQuery . length === 0 ,
5164 inputElement : props . inputRef . current ! ,
5265 type : 'search' ,
5366 maxLength : '512' ,
You can’t perform that action at this time.
0 commit comments