1- import * as React from 'react' ;
2- import { createPortal } from 'react-dom' ;
3- import Router from 'next/router' ;
4- import Link from 'next/link' ;
5- import Head from 'next/head' ;
6- import { useDocSearchKeyboardEvents } from '@docsearch/react' ;
7- import { siteConfig } from 'siteConfig' ;
1+ import * as React from 'react'
2+ import { useSearch } from './useSearch'
83
9- function Hit ( {
10- hit,
11- children
12- } ) {
13- return < Link href = { hit . url . replace ( ) } >
14- < a > { children } </ a >
15- </ Link > ;
16- }
17-
18- const options = {
19- appId : siteConfig . algolia . appId ,
20- apiKey : siteConfig . algolia . apiKey ,
21- indexName : siteConfig . algolia . indexName ,
22- rednerModal : true
23- } ;
24- let DocSearchModal = null ;
25- export const Search = ( {
26- appId,
27- searchParameters = {
28- hitsPerPage : 5
29- } ,
30- renderModal = true
31- } ) => {
32- const [ isLoaded , setIsLoaded ] = React . useState ( true ) ;
33- const [ isShowing , setIsShowing ] = React . useState ( false ) ;
34- const scrollY = React . useRef ( 0 ) ;
35- const importDocSearchModalIfNeeded = React . useCallback ( function importDocSearchModalIfNeeded ( ) {
36- if ( DocSearchModal ) {
37- return Promise . resolve ( ) ;
38- }
39-
40- return Promise . all ( [ import ( '@docsearch/react/modal' ) ] ) . then ( ( [ {
41- DocSearchModal : Modal
42- } ] ) => {
43- DocSearchModal = Modal ;
44- } ) ;
45- } , [ ] ) ;
46- const onOpen = React . useCallback ( function onOpen ( ) {
47- importDocSearchModalIfNeeded ( ) . then ( ( ) => {
48- setIsShowing ( true ) ;
49- } ) ;
50- } , [ importDocSearchModalIfNeeded , setIsShowing ] ) ;
51- const onClose = React . useCallback ( function onClose ( ) {
52- setIsShowing ( false ) ;
53- } , [ setIsShowing ] ) ;
54- useDocSearchKeyboardEvents ( {
55- isOpen : isShowing ,
56- onOpen,
57- onClose
58- } ) ;
59- return < >
60- < Head >
61- < link rel = "preconnect" href = { `https://${ appId } -dsn.algolia.net` } crossOrigin = "true" />
62- </ Head >
4+ export const Search = ( ) => {
5+ const { onOpen } = useSearch ( )
636
64- < div >
65- < button type = "button" className = "group form-input hover:text-gray-600 hover:border-gray-300 transition duration-150 ease-in-out pointer flex items-center bg-gray-50 text-left w-full text-gray-500 rounded-lg text-sm align-middle" onClick = { onOpen } >
66- < svg width = "1em" height = "1em" className = "mr-3 align-middle text-gray-600 flex-shrink-0 group-hover:text-gray-700" style = { {
67- marginBottom : 2
68- } } viewBox = "0 0 20 20" >
69- < path d = "M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke = "currentColor" fill = "none" strokeWidth = "2" fillRule = "evenodd" strokeLinecap = "round" strokeLinejoin = "round" > </ path >
70- </ svg >
71- Search docs
72- < span className = "ml-auto invisible lg:visible" >
73- < kbd className = "border border-gray-300 mr-1 bg-gray-100 align-middle p-0 inline-flex justify-center items-center text-xs text-center mr-0 rounded group-hover:border-gray-300 transition duration-150 ease-in-out " style = { {
74- minWidth : '1.8em'
75- } } >
76- ⌘
77- </ kbd >
78- < kbd className = "border border-gray-300 bg-gray-100 align-middle p-0 inline-flex justify-center items-center text-xs text-center ml-auto mr-0 rounded group-hover:border-gray-300 transition duration-150 ease-in-out " style = { {
79- minWidth : '1.8em'
80- } } >
81- K
82- </ kbd >
83- </ span >
84- </ button >
85- </ div >
86-
87- { isLoaded && isShowing && createPortal ( < DocSearchModal { ...options } searchParameters = { searchParameters } onClose = { onClose } navigator = { {
88- navigate ( {
89- suggestionUrl
90- } ) {
91- Router . push ( suggestionUrl ) ;
92- }
93-
94- } } transformItems = { items => {
95- return items . map ( item => {
96- const url = new URL ( item . url ) ;
97- return { ...item ,
98- url : item . url . replace ( url . origin , '' ) . replace ( '#__next' , '' ) . replace ( '/docs/#' , '/docs/overview#' )
99- } ;
100- } ) ;
101- } } hitComponent = { Hit } /> , document . body ) }
102- </ > ;
103- } ;
104- Search . displayName = 'Search' ;
7+ return (
8+ < div >
9+ < button
10+ type = "button"
11+ className = "group form-input hover:text-gray-600 hover:border-gray-300 transition duration-150 ease-in-out pointer flex items-center bg-gray-50 text-left w-full text-gray-500 rounded-lg text-sm align-middle"
12+ onClick = { onOpen }
13+ >
14+ < svg
15+ width = "1em"
16+ height = "1em"
17+ className = "mr-3 align-middle text-gray-600 flex-shrink-0 group-hover:text-gray-700"
18+ style = { {
19+ marginBottom : 2 ,
20+ } }
21+ viewBox = "0 0 20 20"
22+ >
23+ < path
24+ d = "M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z"
25+ stroke = "currentColor"
26+ fill = "none"
27+ strokeWidth = "2"
28+ fillRule = "evenodd"
29+ strokeLinecap = "round"
30+ strokeLinejoin = "round"
31+ > </ path >
32+ </ svg >
33+ Search docs
34+ < span className = "ml-auto invisible lg:visible" >
35+ < kbd
36+ className = "border border-gray-300 mr-1 bg-gray-100 align-middle p-0 inline-flex justify-center items-center text-xs text-center mr-0 rounded group-hover:border-gray-300 transition duration-150 ease-in-out "
37+ style = { {
38+ minWidth : '1.8em' ,
39+ } }
40+ >
41+ ⌘
42+ </ kbd >
43+ < kbd
44+ className = "border border-gray-300 bg-gray-100 align-middle p-0 inline-flex justify-center items-center text-xs text-center ml-auto mr-0 rounded group-hover:border-gray-300 transition duration-150 ease-in-out "
45+ style = { {
46+ minWidth : '1.8em' ,
47+ } }
48+ >
49+ K
50+ </ kbd >
51+ </ span >
52+ </ button >
53+ </ div >
54+ )
55+ }
56+ Search . displayName = 'Search'
0 commit comments