Skip to content

Commit 2068779

Browse files
committed
fix(search): fix docsearch support
1 parent 34d99e4 commit 2068779

File tree

4 files changed

+21
-66
lines changed

4 files changed

+21
-66
lines changed

smooth-doc/src/components/DocSearch.js

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as React from 'react'
2-
import { Link, navigate } from 'gatsby'
32
import { createPortal } from 'react-dom'
43
import styled, { x, createGlobalStyle } from '@xstyled/styled-components'
5-
import { useDocSearchKeyboardEvents } from '@docsearch/react'
4+
import { DocSearchModal, useDocSearchKeyboardEvents } from '@docsearch/react'
65
import { RiSearchLine } from 'react-icons/ri'
76
import { 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-
4338
const 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-
6154
export 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
)}

smooth-doc/src/components/Input.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export const Input = styled.input`
2828
`
2929

3030
export const InputGroup = styled.div`
31+
appearance: none;
32+
background-color: transparent;
33+
padding: 0;
3134
display: inline-flex;
3235
color: control-placeholder;
3336
transition: control;

smooth-doc/src/components/MDX.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { CarbonAd } from './CarbonAd'
55
import { Table, TableContainer } from './Table'
66

77
function transformCode({ children, className, ...props }) {
8-
console.log(className)
98
const lang = className && className.split('-')[1]
109
return (
1110
<Code lang={lang} {...props}>

website/gatsby-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module.exports = {
1717
carbonAdsURL:
1818
'//cdn.carbonads.com/carbon.js?serve=CE7IL2JN&placement=xstyleddev',
1919
docSearch: {
20-
apiKey: 'a0c23a738fe1cfffa923daba2eb185be',
20+
appId: 'J2LYQ9877O',
21+
apiKey: '9295224c1474afa9f75f7d4772a1f713',
2122
indexName: 'smooth-doc',
2223
},
2324
},

0 commit comments

Comments
 (0)