File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -30,10 +30,15 @@ export interface DocSearchProps
3030export function DocSearch ( props : DocSearchProps ) {
3131 const [ isOpen , setIsOpen ] = React . useState ( false ) ;
3232 const searchButtonRef = React . useRef < HTMLButtonElement > ( null ) ;
33+ const [ initialQuery , setInitialQuery ] = React . useState ( '' ) ;
3334
34- const onOpen = React . useCallback ( ( ) => {
35- setIsOpen ( true ) ;
36- } , [ setIsOpen ] ) ;
35+ const onOpen = React . useCallback (
36+ ( { query = '' } = { } ) => {
37+ setIsOpen ( true ) ;
38+ setInitialQuery ( query ) ;
39+ } ,
40+ [ setIsOpen ]
41+ ) ;
3742
3843 const onClose = React . useCallback ( ( ) => {
3944 setIsOpen ( false ) ;
@@ -49,6 +54,7 @@ export function DocSearch(props: DocSearchProps) {
4954 createPortal (
5055 < DocSearchModal
5156 { ...props }
57+ initialQuery = { initialQuery }
5258 initialScrollY = { window . scrollY }
5359 onClose = { onClose }
5460 /> ,
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import { ScreenState } from './ScreenState';
2323import { Footer } from './Footer' ;
2424
2525interface DocSearchModalProps extends DocSearchProps {
26+ initialQuery ?: string ;
2627 initialScrollY : number ;
2728 onClose ?( ) : void ;
2829}
@@ -42,6 +43,7 @@ export function DocSearchModal({
4243 hitComponent = Hit ,
4344 resultsFooterComponent = ( ) => null ,
4445 navigator,
46+ initialQuery : queryFromProp = '' ,
4547 initialScrollY = 0 ,
4648} : DocSearchModalProps ) {
4749 const [ state , setState ] = React . useState <
@@ -57,7 +59,7 @@ export function DocSearchModal({
5759 const inputRef = React . useRef < HTMLInputElement | null > ( null ) ;
5860 const snipetLength = React . useRef < number > ( 10 ) ;
5961 const initialQuery = React . useRef (
60- typeof window !== 'undefined'
62+ queryFromProp || typeof window !== 'undefined'
6163 ? window . getSelection ( ) ! . toString ( ) . slice ( 0 , MAX_QUERY_SIZE )
6264 : ''
6365 ) . current ;
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ export function useDocSearchKeyboardEvents({
3030 searchButtonRef . current === document . activeElement
3131 ) {
3232 if ( / [ a - z A - Z 0 - 9 ] / . test ( String . fromCharCode ( event . keyCode ) ) ) {
33- onOpen ( ) ;
33+ onOpen ( { query : event . keyCode } ) ;
3434 }
3535 }
3636 }
You can’t perform that action at this time.
0 commit comments