11import * as React from 'react'
2- import { Link , navigate } from 'gatsby'
32import { createPortal } from 'react-dom'
43import styled , { x , createGlobalStyle } from '@xstyled/styled-components'
5- import { useDocSearchKeyboardEvents } from '@docsearch/react'
4+ import { DocSearchModal , useDocSearchKeyboardEvents } from '@docsearch/react'
65import { RiSearchLine } from 'react-icons/ri'
76import { Input , InputGroup , InputGroupIcon } from './Input'
87
@@ -36,10 +35,6 @@ const GlobalStyle = createGlobalStyle`
3635 }
3736`
3837
39- function Hit ( { hit, children } ) {
40- return < Link to = { hit . url } > { children } </ Link >
41- }
42-
4338const Kbd = styled . kbd `
4439 border: 1;
4540 border-color: control-border;
@@ -56,53 +51,29 @@ const Kbd = styled.kbd`
5651 min-width: 1.5em;
5752`
5853
59- let DocSearchModal = null
60-
6154export function DocSearch ( { apiKey, indexName, appId } ) {
6255 const searchButtonRef = React . useRef ( null )
63- const [ isShowing , setIsShowing ] = React . useState ( false )
56+ const [ isOpen , setIsOpen ] = React . useState ( false )
6457 const [ initialQuery , setInitialQuery ] = React . useState ( null )
6558
66- const importDocSearchModalIfNeeded = React . useCallback ( ( ) => {
67- if ( DocSearchModal ) {
68- return Promise . resolve ( )
69- }
70-
71- return Promise . resolve ( import ( '@docsearch/react/modal' ) ) . then (
72- ( { DocSearchModal : Modal } ) => {
73- DocSearchModal = Modal
74- } ,
75- )
76- } , [ ] )
77-
7859 const onOpen = React . useCallback ( ( ) => {
79- importDocSearchModalIfNeeded ( ) . then ( ( ) => {
80- // We check that no other DocSearch modal is showing before opening this
81- // one (we use one instance for desktop and one instance for mobile).
82- if ( document . body . classList . contains ( 'DocSearch--active' ) ) {
83- return
84- }
85-
86- setIsShowing ( true )
87- } )
88- } , [ importDocSearchModalIfNeeded , setIsShowing ] )
60+ setIsOpen ( true )
61+ } , [ setIsOpen ] )
8962
9063 const onClose = React . useCallback ( ( ) => {
91- setIsShowing ( false )
92- } , [ setIsShowing ] )
64+ setIsOpen ( false )
65+ } , [ setIsOpen ] )
9366
9467 const onInput = React . useCallback (
9568 ( event ) => {
96- importDocSearchModalIfNeeded ( ) . then ( ( ) => {
97- setIsShowing ( true )
98- setInitialQuery ( event . key )
99- } )
69+ setIsOpen ( true )
70+ setInitialQuery ( event . key )
10071 } ,
101- [ importDocSearchModalIfNeeded , setIsShowing , setInitialQuery ] ,
72+ [ setIsOpen , setInitialQuery ] ,
10273 )
10374
10475 useDocSearchKeyboardEvents ( {
105- isOpen : isShowing ,
76+ isOpen,
10677 onOpen,
10778 onClose,
10879 onInput,
@@ -113,16 +84,11 @@ export function DocSearch({ apiKey, indexName, appId }) {
11384 < >
11485 < GlobalStyle />
11586 < div >
116- < InputGroup >
87+ < InputGroup ref = { searchButtonRef } as = "button" onClick = { ( ) => onOpen ( ) } >
11788 < InputGroupIcon >
11889 < RiSearchLine />
11990 </ InputGroupIcon >
120- < Input
121- ref = { searchButtonRef }
122- onClick = { onOpen }
123- type = "search"
124- placeholder = "Search..."
125- />
91+ < Input disabled type = "search" placeholder = "Search..." />
12692 < x . div
12793 position = "absolute"
12894 top = "50%"
@@ -138,29 +104,15 @@ export function DocSearch({ apiKey, indexName, appId }) {
138104 </ InputGroup >
139105 </ div >
140106
141- { isShowing &&
107+ { isOpen &&
142108 createPortal (
143109 < DocSearchModal
144110 apiKey = { apiKey }
145111 indexName = { indexName }
146- appId = { appId ?? 'BH4D9OD16A' }
147- initialQuery = { initialQuery }
112+ appId = { appId }
148113 onClose = { onClose }
149- navigator = { {
150- navigate ( { suggestionUrl } ) {
151- navigate ( suggestionUrl )
152- } ,
153- } }
154- transformItems = { ( items ) => {
155- return items . map ( ( item ) => {
156- const url = new URL ( item . url )
157- return {
158- ...item ,
159- url : item . url . replace ( url . origin , '' ) ,
160- }
161- } )
162- } }
163- hitComponent = { Hit }
114+ initialScrollY = { window . scrollY }
115+ initialQuery = { initialQuery }
164116 /> ,
165117 document . body ,
166118 ) }
0 commit comments