summaryrefslogtreecommitdiff
path: root/docs/src/components
diff options
context:
space:
mode:
authorGravatar Fred K. Schott <fkschott@gmail.com> 2021-07-29 13:31:41 -0700
committerGravatar GitHub <noreply@github.com> 2021-07-29 13:31:41 -0700
commit6eeafac715aaa077f07dd12d0609301a807a0705 (patch)
tree0e163a45864794fbe2e926a0a85a189bb500f837 /docs/src/components
parent908139642a7863ce38e567f791aee1972ab6ec1f (diff)
downloadastro-6eeafac715aaa077f07dd12d0609301a807a0705.tar.gz
astro-6eeafac715aaa077f07dd12d0609301a807a0705.tar.zst
astro-6eeafac715aaa077f07dd12d0609301a807a0705.zip
Add algolia-powered search bar to docs site (#917)
* add search bar * replace cmd+k with slash hotkey * update api key * add localhost support for search
Diffstat (limited to 'docs/src/components')
-rw-r--r--docs/src/components/Search.css93
-rw-r--r--docs/src/components/Search.tsx97
2 files changed, 190 insertions, 0 deletions
diff --git a/docs/src/components/Search.css b/docs/src/components/Search.css
new file mode 100644
index 000000000..53ca9bfd0
--- /dev/null
+++ b/docs/src/components/Search.css
@@ -0,0 +1,93 @@
+:root {
+ --docsearch-primary-color: var(--theme-accent);
+ --docsearch-logo-color: var(--theme-text);
+}
+
+.search {
+ display: none;
+ position: relative;
+ z-index: 10;
+ flex-grow: 1;
+ grid-area: search;
+ padding-left: 0.75rem;
+ padding-right: 0.7rem;
+ display: flex;
+ width: 400px;
+ max-width: 400px;
+}
+.search > * {
+ flex-grow: 1;
+}
+
+.search > :global(.algolia-autocomplete) {
+ width: 100%;
+}
+
+:global(body.is-nav-open) .search {
+ display: flex;
+}
+
+.search-input {
+ flex-grow: 1;
+ box-sizing: border-box;
+ width: 100%;
+ margin: 0;
+ padding: 0.33em 0.5em;
+ overflow: visible;
+ font-weight: 500;
+ font-size: 100%;
+ font-family: inherit;
+ line-height: inherit;
+ background-color: var(--theme-divider);
+ border-color: var(--theme-divider);
+ color: var(--theme-text-light);
+ border-style: solid;
+ border-width: 1px;
+ border-radius: 0.25rem;
+ outline: 0;
+ cursor: pointer;
+ transition-timing-function: ease-out;
+ transition-duration: 0.2s;
+ transition-property: border-color, background-color;
+ -webkit-font-smoothing: antialiased;
+}
+.no-underline {
+ text-decoration: none;
+}
+.search-input:hover {
+ color: var(--theme-text);
+}
+.search-input:hover::placeholder {
+ color: var(--theme-text-light);
+}
+.search-input::placeholder {
+ color: var(--theme-text-light);
+}
+
+.search-hint {
+ position: absolute;
+ top: 7px;
+ right: 19px;
+ padding: 3px 5px;
+ display: none;
+ display: none;
+ align-items: center;
+ justify-content: center;
+ letter-spacing: 0.125em;
+ color: var(--theme-text-light);
+ font-size: 13px;
+ font-family: var(--font-mono);
+ pointer-events: none;
+ border-color: var(--theme-text-lighter);
+ color: var(--theme-text-light);
+ border-style: solid;
+ border-width: 1px;
+ border-radius: 0.25rem;
+ line-height: 14px;
+}
+
+@media (min-width: 50em) {
+ .search-hint {
+ display: flex;
+ }
+} \ No newline at end of file
diff --git a/docs/src/components/Search.tsx b/docs/src/components/Search.tsx
new file mode 100644
index 000000000..066699fc1
--- /dev/null
+++ b/docs/src/components/Search.tsx
@@ -0,0 +1,97 @@
+/* jsxImportSource: react */
+import { useState, useCallback, useRef } from 'react'
+import { createPortal } from 'react-dom'
+import { DocSearchModal, useDocSearchKeyboardEvents } from '@docsearch/react'
+import '@docsearch/css//dist/style.css';
+import './Search.css';
+
+
+export function Search() {
+ const [isOpen, setIsOpen] = useState(false)
+ const searchButtonRef = useRef()
+ const [initialQuery, setInitialQuery] = useState(null)
+
+ const onOpen = useCallback(() => {
+ setIsOpen(true)
+ }, [setIsOpen])
+
+ const onClose = useCallback(() => {
+ setIsOpen(false)
+ }, [setIsOpen])
+
+ const onInput = useCallback(
+ (e) => {
+ setIsOpen(true)
+ setInitialQuery(e.key)
+ },
+ [setIsOpen, setInitialQuery]
+ )
+
+ useDocSearchKeyboardEvents({
+ isOpen,
+ onOpen,
+ onClose,
+ onInput,
+ searchButtonRef,
+ })
+
+ return (
+ <>
+ <button
+ type="button"
+ ref={searchButtonRef}
+ onClick={onOpen}
+ className="search-input"
+ >
+ <svg
+ width="24"
+ height="24"
+ fill="none"
+ >
+ <path
+ d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
+ stroke="currentColor"
+ strokeWidth="2"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ />
+ </svg>
+ <span>
+ Search
+ </span>
+ <span
+ className="search-hint"
+ >
+ <span className="sr-only">Press </span>
+ <kbd>/</kbd>
+ <span className="sr-only"> to search</span>
+ </span>
+ </button>
+ {isOpen &&
+ createPortal(
+ <DocSearchModal
+ initialQuery={initialQuery}
+ initialScrollY={window.scrollY}
+ onClose={onClose}
+ indexName="astro"
+ apiKey="0f387260ad74f9cbf4353facd29c919c"
+ 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
+ console.log(a.hash);
+ const hash = a.hash === '#overview' ? '' : a.hash
+ return {
+ ...item,
+ url: `${a.pathname}${hash}`,
+ }
+ })
+ }}
+ />,
+ document.body
+ )}
+ </>
+ )
+} \ No newline at end of file