mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
Fixed search bar of the docs example not working (#3247)
* fixed search bar not working * fixed search bar not working during build * fix search bar throwing error on production site
This commit is contained in:
parent
ea104dde91
commit
17da2b1611
1 changed files with 94 additions and 76 deletions
|
@ -1,86 +1,104 @@
|
||||||
/* jsxImportSource: react */
|
/* jsxImportSource: react */
|
||||||
import { useState, useCallback, useRef } from 'react';
|
import { useState, useCallback, useRef } from "react";
|
||||||
import { createPortal } from 'react-dom';
|
import * as CONFIG from "../../config";
|
||||||
import * as docSearchReact from '@docsearch/react';
|
import "@docsearch/css/dist/style.css";
|
||||||
import * as CONFIG from '../../config';
|
import "./Search.css";
|
||||||
import '@docsearch/css/dist/style.css';
|
|
||||||
import './Search.css';
|
|
||||||
|
|
||||||
const { DocSearchModal, useDocSearchKeyboardEvents } = docSearchReact.default;
|
// @ts-ignore
|
||||||
|
import * as docSearchReact from "@docsearch/react";
|
||||||
|
// @ts-ignore
|
||||||
|
import { createPortal } from "react-dom";
|
||||||
|
|
||||||
export default function Search() {
|
export default function Search() {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const DocSearchModal =
|
||||||
const searchButtonRef = useRef();
|
docSearchReact.DocSearchModal || docSearchReact.default.DocSearchModal;
|
||||||
const [initialQuery, setInitialQuery] = useState(null);
|
|
||||||
|
|
||||||
const onOpen = useCallback(() => {
|
const useDocSearchKeyboardEvents =
|
||||||
setIsOpen(true);
|
docSearchReact.useDocSearchKeyboardEvents ||
|
||||||
}, [setIsOpen]);
|
docSearchReact.default.useDocSearchKeyboardEvents;
|
||||||
|
|
||||||
const onClose = useCallback(() => {
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
setIsOpen(false);
|
const searchButtonRef = useRef();
|
||||||
}, [setIsOpen]);
|
const [initialQuery, setInitialQuery] = useState(null);
|
||||||
|
|
||||||
const onInput = useCallback(
|
const onOpen = useCallback(() => {
|
||||||
(e) => {
|
setIsOpen(true);
|
||||||
setIsOpen(true);
|
}, [setIsOpen]);
|
||||||
setInitialQuery(e.key);
|
|
||||||
},
|
|
||||||
[setIsOpen, setInitialQuery]
|
|
||||||
);
|
|
||||||
|
|
||||||
useDocSearchKeyboardEvents({
|
const onClose = useCallback(() => {
|
||||||
isOpen,
|
setIsOpen(false);
|
||||||
onOpen,
|
}, [setIsOpen]);
|
||||||
onClose,
|
|
||||||
onInput,
|
|
||||||
searchButtonRef,
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
const onInput = useCallback(
|
||||||
<>
|
(e) => {
|
||||||
<button type="button" ref={searchButtonRef} onClick={onOpen} className="search-input">
|
setIsOpen(true);
|
||||||
<svg width="24" height="24" fill="none">
|
setInitialQuery(e.key);
|
||||||
<path
|
},
|
||||||
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
[setIsOpen, setInitialQuery]
|
||||||
stroke="currentColor"
|
);
|
||||||
strokeWidth="2"
|
|
||||||
strokeLinecap="round"
|
useDocSearchKeyboardEvents({
|
||||||
strokeLinejoin="round"
|
isOpen,
|
||||||
/>
|
onOpen,
|
||||||
</svg>
|
onClose,
|
||||||
<span>Search</span>
|
onInput,
|
||||||
<span className="search-hint">
|
searchButtonRef,
|
||||||
<span className="sr-only">Press </span>
|
});
|
||||||
<kbd>/</kbd>
|
|
||||||
<span className="sr-only"> to search</span>
|
return (
|
||||||
</span>
|
<>
|
||||||
</button>
|
<button
|
||||||
{isOpen &&
|
type="button"
|
||||||
createPortal(
|
ref={searchButtonRef}
|
||||||
<DocSearchModal
|
onClick={onOpen}
|
||||||
initialQuery={initialQuery}
|
className="search-input"
|
||||||
initialScrollY={window.scrollY}
|
>
|
||||||
onClose={onClose}
|
<svg width="24" height="24" fill="none">
|
||||||
indexName={(CONFIG as any).ALGOLIA.indexName}
|
<path
|
||||||
appId={(CONFIG as any).ALGOLIA.appId}
|
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
||||||
apiKey={(CONFIG as any).ALGOLIA.apiKey}
|
stroke="currentColor"
|
||||||
transformItems={(items) => {
|
strokeWidth="2"
|
||||||
return items.map((item) => {
|
strokeLinecap="round"
|
||||||
// We transform the absolute URL into a relative URL to
|
strokeLinejoin="round"
|
||||||
// work better on localhost, preview URLS.
|
/>
|
||||||
const a = document.createElement('a');
|
</svg>
|
||||||
a.href = item.url;
|
|
||||||
const hash = a.hash === '#overview' ? '' : a.hash;
|
<span>Search</span>
|
||||||
return {
|
|
||||||
...item,
|
<span className="search-hint">
|
||||||
url: `${a.pathname}${hash}`,
|
<span className="sr-only">Press </span>
|
||||||
};
|
|
||||||
});
|
<kbd>/</kbd>
|
||||||
}}
|
|
||||||
/>,
|
<span className="sr-only"> to search</span>
|
||||||
document.body
|
</span>
|
||||||
)}
|
</button>
|
||||||
</>
|
|
||||||
);
|
{isOpen &&
|
||||||
|
createPortal(
|
||||||
|
<DocSearchModal
|
||||||
|
initialQuery={initialQuery}
|
||||||
|
initialScrollY={window.scrollY}
|
||||||
|
onClose={onClose}
|
||||||
|
indexName={(CONFIG as any).ALGOLIA.indexName}
|
||||||
|
appId={(CONFIG as any).ALGOLIA.appId}
|
||||||
|
apiKey={(CONFIG as any).ALGOLIA.apiKey}
|
||||||
|
transformItems={(items) => {
|
||||||
|
return items.map((item) => {
|
||||||
|
// We transform the absolute URL into a relative URL to
|
||||||
|
// work better on localhost, preview URLS.
|
||||||
|
const a = document.createElement("a");
|
||||||
|
a.href = item.url;
|
||||||
|
const hash = a.hash === "#overview" ? "" : a.hash;
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
url: `${a.pathname}${hash}`,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
/>,
|
||||||
|
document.body
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue