-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSearch.js
More file actions
141 lines (130 loc) · 4.19 KB
/
Copy pathSearch.js
File metadata and controls
141 lines (130 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { useState, useCallback, useRef, useEffect } from 'react'
import { createPortal } from 'react-dom'
import Link from 'next/link'
import Head from 'next/head'
import { useRouter } from 'next/router'
import { DocSearchModal, useDocSearchKeyboardEvents } from '@docsearch/react'
const ACTION_KEY_DEFAULT = ['Ctrl ', 'Control']
const ACTION_KEY_APPLE = ['⌘', 'Command']
function Hit({ hit, children }) {
return (
<Link href={hit.url}>
<a>{children}</a>
</Link>
)
}
export function Search() {
const router = useRouter()
const [isOpen, setIsOpen] = useState(false)
const searchButtonRef = useRef()
const [initialQuery, setInitialQuery] = useState(null)
const [browserDetected, setBrowserDetected] = useState(false)
const [actionKey, setActionKey] = useState(ACTION_KEY_DEFAULT)
const onOpen = useCallback(() => {
setIsOpen(true)
}, [setIsOpen])
const onClose = useCallback(() => {
setIsOpen(false)
}, [setIsOpen])
const onInput = useCallback(
(e) => {
setIsOpen(true)
setInitialQuery(e.key)
},
[setIsOpen, setInitialQuery]
)
useDocSearchKeyboardEvents({
isOpen,
onOpen,
onClose,
onInput,
searchButtonRef,
})
useEffect(() => {
if (typeof navigator !== 'undefined') {
if (/(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)) {
setActionKey(ACTION_KEY_APPLE)
} else {
setActionKey(ACTION_KEY_DEFAULT)
}
setBrowserDetected(true)
}
}, [])
return (
<>
<Head>
<link rel="preconnect" href="https://BH4D9OD16A-dsn.algolia.net" crossOrigin="true" />
</Head>
<button
type="button"
ref={searchButtonRef}
onClick={onOpen}
className="group leading-6 font-medium flex items-center space-x-3 sm:space-x-4 text-gray-500 bg-gray-100 hover:text-gray-600 dark:bg-gray-800 dark:text-gray-400 transition ease-in-out duration-200 w-full py-1.5 px-1.5 sm:px-4 lg:px-4 rounded-lg border-2 border-gray-200 dark:border-gray-600"
>
<svg
width="24"
height="24"
fill="none"
className="text-blue-400 group-hover:text-blue-500 transition-colors duration-200"
>
<path
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
<span>
<span className="lg:hidden md:hidden">Search</span><span className="hidden sm:inline">Quick search for anything</span>
</span>
<span
style={{ opacity: browserDetected ? '1' : '0' }}
className="hidden sm:block text-gray-500 text-sm leading-5 py-0.5 px-1.5 border border-gray-400 rounded-md"
>
<span className="sr-only">Press </span>
<kbd className="font-sans">
<abbr title={actionKey[1]} className="no-underline">
{actionKey[0]}
</abbr>
</kbd>
<span className="sr-only"> and </span>
<kbd className="font-sans">K</kbd>
<span className="sr-only"> to search</span>
</span>
</button>
{isOpen &&
createPortal(
<DocSearchModal
initialQuery={initialQuery}
initialScrollY={window.scrollY}
onClose={onClose}
indexName="javaistic"
apiKey="2ec8ae33bd1995918d1c196b74be5128"
appId="BH4D9OD16A"
navigator={{
navigate({ suggestionUrl }) {
setIsOpen(false)
router.push(suggestionUrl)
},
}}
hitComponent={Hit}
transformItems={(items) => {
return items.map((item) => {
// We transform the absolute URL into a relative URL to
// leverage Next's preloading.
const a = document.createElement('a')
a.href = item.url
const hash = a.hash === '#content-wrapper' ? '' : a.hash
return {
...item,
url: `${a.pathname}${hash}`,
}
})
}}
/>,
document.body
)}
</>
)
}