Skip to content

Commit ec1aecc

Browse files
authored
fix: Correct a few docsearch bugs (#1364)
1 parent 65f4780 commit ec1aecc

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

src/components/header/search.jsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ function injectDocsearchCSS() {
2929
* interacted with the placeholder button prior to Docsearch fully loading.
3030
*
3131
* @param {HTMLElement} root
32-
* @returns {Promise<HTMLElement>}
32+
* @returns {Promise<HTMLButtonElement>}
3333
*/
3434
function waitForDocsearch(root) {
3535
return new Promise(resolve => {
36+
/** @returns {HTMLButtonElement | null} */
3637
const getDocSearchButton = () =>
3738
document.querySelector('.DocSearch.DocSearch-Button');
3839

@@ -69,10 +70,6 @@ export default function Search() {
6970
const root = useRef(null);
7071
const rendered = useRef(false);
7172
const interactedWith = useRef(false);
72-
const loadingBar =
73-
typeof window !== 'undefined'
74-
? document.querySelector('loading-bar')
75-
: null;
7673

7774
const loadDocSearch = () => {
7875
if (!rendered.current) {
@@ -91,7 +88,7 @@ export default function Search() {
9188

9289
waitForDocsearch(root.current).then(docsearchButton => {
9390
rendered.current = true;
94-
loadingBar.removeAttribute('showing');
91+
document.querySelector('loading-bar')?.removeAttribute('showing');
9592
if (interactedWith.current) {
9693
docsearchButton.click();
9794
}
@@ -107,26 +104,37 @@ export default function Search() {
107104
// The <loading-bar> is sat alongside the router & has it's state controlled by it,
108105
// so while we could create a new context to be able to set it here, direct DOM
109106
// manipulation is a heck of a lot simpler.
110-
loadingBar.setAttribute('showing', 'true');
107+
document.querySelector('loading-bar')?.setAttribute('showing', 'true');
111108
}
112109
};
113110

114111
return (
115112
<div class={style.search} ref={root}>
116-
{/* Copy/paste of the HTML DocSearch normally generates, used as a placeholder */}
117-
<button
118-
type="button"
119-
aria-label="Search"
120-
class="DocSearch DocSearch-Button"
121-
onMouseOver={loadDocSearch}
122-
onTouchStart={loadDocSearch}
123-
onFocus={loadDocSearch}
124-
onClick={onInteraction}
125-
>
126-
<span class="DocSearch-Button-Container">
127-
<span class="DocSearch-Button-Placeholder">Search</span>
128-
</span>
129-
</button>
113+
{rendered.current ? (
114+
<ErrorBoundary>
115+
<DocSearch
116+
apiKey={config.docsearch.apiKey}
117+
indexName={config.docsearch.indexName}
118+
appId={config.docsearch.appId}
119+
transformItems={transformItems}
120+
/>
121+
</ErrorBoundary>
122+
) : (
123+
/* Copy/paste of the HTML DocSearch normally generates, used as a placeholder */
124+
<button
125+
type="button"
126+
aria-label="Search"
127+
class="DocSearch DocSearch-Button"
128+
onMouseOver={loadDocSearch}
129+
onTouchStart={loadDocSearch}
130+
onFocus={loadDocSearch}
131+
onClick={onInteraction}
132+
>
133+
<span class="DocSearch-Button-Container">
134+
<span class="DocSearch-Button-Placeholder">Search</span>
135+
</span>
136+
</button>
137+
)}
130138
</div>
131139
);
132140
}

0 commit comments

Comments
 (0)