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:
Rafid Muhymin Wafi 2022-05-13 02:20:20 +06:00 committed by GitHub
parent ea104dde91
commit 17da2b1611
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,14 +1,22 @@
/* 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 DocSearchModal =
docSearchReact.DocSearchModal || docSearchReact.default.DocSearchModal;
const useDocSearchKeyboardEvents =
docSearchReact.useDocSearchKeyboardEvents ||
docSearchReact.default.useDocSearchKeyboardEvents;
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const searchButtonRef = useRef(); const searchButtonRef = useRef();
const [initialQuery, setInitialQuery] = useState(null); const [initialQuery, setInitialQuery] = useState(null);
@ -39,7 +47,12 @@ export default function Search() {
return ( return (
<> <>
<button type="button" ref={searchButtonRef} onClick={onOpen} className="search-input"> <button
type="button"
ref={searchButtonRef}
onClick={onOpen}
className="search-input"
>
<svg width="24" height="24" fill="none"> <svg width="24" height="24" fill="none">
<path <path
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
@ -49,13 +62,18 @@ export default function Search() {
strokeLinejoin="round" strokeLinejoin="round"
/> />
</svg> </svg>
<span>Search</span> <span>Search</span>
<span className="search-hint"> <span className="search-hint">
<span className="sr-only">Press </span> <span className="sr-only">Press </span>
<kbd>/</kbd> <kbd>/</kbd>
<span className="sr-only"> to search</span> <span className="sr-only"> to search</span>
</span> </span>
</button> </button>
{isOpen && {isOpen &&
createPortal( createPortal(
<DocSearchModal <DocSearchModal
@ -69,9 +87,9 @@ export default function Search() {
return items.map((item) => { return items.map((item) => {
// We transform the absolute URL into a relative URL to // We transform the absolute URL into a relative URL to
// work better on localhost, preview URLS. // work better on localhost, preview URLS.
const a = document.createElement('a'); const a = document.createElement("a");
a.href = item.url; a.href = item.url;
const hash = a.hash === '#overview' ? '' : a.hash; const hash = a.hash === "#overview" ? "" : a.hash;
return { return {
...item, ...item,
url: `${a.pathname}${hash}`, url: `${a.pathname}${hash}`,