[ci] format
This commit is contained in:
parent
17da2b1611
commit
29e7260a4e
1 changed files with 78 additions and 85 deletions
|
@ -1,104 +1,97 @@
|
||||||
/* jsxImportSource: react */
|
/* jsxImportSource: react */
|
||||||
import { useState, useCallback, useRef } from "react";
|
import { useState, useCallback, useRef } from 'react';
|
||||||
import * as CONFIG from "../../config";
|
import * as CONFIG from '../../config';
|
||||||
import "@docsearch/css/dist/style.css";
|
import '@docsearch/css/dist/style.css';
|
||||||
import "./Search.css";
|
import './Search.css';
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import * as docSearchReact from "@docsearch/react";
|
import * as docSearchReact from '@docsearch/react';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { createPortal } from "react-dom";
|
import { createPortal } from 'react-dom';
|
||||||
|
|
||||||
export default function Search() {
|
export default function Search() {
|
||||||
const DocSearchModal =
|
const DocSearchModal = docSearchReact.DocSearchModal || docSearchReact.default.DocSearchModal;
|
||||||
docSearchReact.DocSearchModal || docSearchReact.default.DocSearchModal;
|
|
||||||
|
|
||||||
const useDocSearchKeyboardEvents =
|
const useDocSearchKeyboardEvents =
|
||||||
docSearchReact.useDocSearchKeyboardEvents ||
|
docSearchReact.useDocSearchKeyboardEvents || docSearchReact.default.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);
|
||||||
|
|
||||||
const onOpen = useCallback(() => {
|
const onOpen = useCallback(() => {
|
||||||
setIsOpen(true);
|
setIsOpen(true);
|
||||||
}, [setIsOpen]);
|
}, [setIsOpen]);
|
||||||
|
|
||||||
const onClose = useCallback(() => {
|
const onClose = useCallback(() => {
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
}, [setIsOpen]);
|
}, [setIsOpen]);
|
||||||
|
|
||||||
const onInput = useCallback(
|
const onInput = useCallback(
|
||||||
(e) => {
|
(e) => {
|
||||||
setIsOpen(true);
|
setIsOpen(true);
|
||||||
setInitialQuery(e.key);
|
setInitialQuery(e.key);
|
||||||
},
|
},
|
||||||
[setIsOpen, setInitialQuery]
|
[setIsOpen, setInitialQuery]
|
||||||
);
|
);
|
||||||
|
|
||||||
useDocSearchKeyboardEvents({
|
useDocSearchKeyboardEvents({
|
||||||
isOpen,
|
isOpen,
|
||||||
onOpen,
|
onOpen,
|
||||||
onClose,
|
onClose,
|
||||||
onInput,
|
onInput,
|
||||||
searchButtonRef,
|
searchButtonRef,
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<button
|
<button type="button" ref={searchButtonRef} onClick={onOpen} className="search-input">
|
||||||
type="button"
|
<svg width="24" height="24" fill="none">
|
||||||
ref={searchButtonRef}
|
<path
|
||||||
onClick={onOpen}
|
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
||||||
className="search-input"
|
stroke="currentColor"
|
||||||
>
|
strokeWidth="2"
|
||||||
<svg width="24" height="24" fill="none">
|
strokeLinecap="round"
|
||||||
<path
|
strokeLinejoin="round"
|
||||||
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
|
/>
|
||||||
stroke="currentColor"
|
</svg>
|
||||||
strokeWidth="2"
|
|
||||||
strokeLinecap="round"
|
|
||||||
strokeLinejoin="round"
|
|
||||||
/>
|
|
||||||
</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
|
||||||
initialQuery={initialQuery}
|
initialQuery={initialQuery}
|
||||||
initialScrollY={window.scrollY}
|
initialScrollY={window.scrollY}
|
||||||
onClose={onClose}
|
onClose={onClose}
|
||||||
indexName={(CONFIG as any).ALGOLIA.indexName}
|
indexName={(CONFIG as any).ALGOLIA.indexName}
|
||||||
appId={(CONFIG as any).ALGOLIA.appId}
|
appId={(CONFIG as any).ALGOLIA.appId}
|
||||||
apiKey={(CONFIG as any).ALGOLIA.apiKey}
|
apiKey={(CONFIG as any).ALGOLIA.apiKey}
|
||||||
transformItems={(items) => {
|
transformItems={(items) => {
|
||||||
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}`,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}}
|
}}
|
||||||
/>,
|
/>,
|
||||||
document.body
|
document.body
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue