summaryrefslogtreecommitdiff
path: root/docs/src/components/Header/Search.tsx
blob: 5f05f744f50dd6d49d457f0545d2b04d3c237d75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* 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 default 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 className="search-placeholder">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;
                const hash = a.hash === '#overview' ? '' : a.hash;
                return {
                  ...item,
                  url: `${a.pathname}${hash}`,
                };
              });
            }}
          />,
          document.body
        )}
    </>
  );
}
p;id=36d991f8ac78f5bf399b4672b192e44b2ad02066&follow=1'>[ci] collect statsGravatar FredKSchott 1-0/+1 2021-09-12[ci] collect statsGravatar FredKSchott 1-0/+1 2021-09-11[ci] collect statsGravatar FredKSchott 1-0/+1 2021-09-10Prevent removing CSS preloads during bundling (#1326)Gravatar Bartek Igielski 8-18/+96 * Prevent removing nodes, becasue styles preloading was detected earlier * Add separate deduping for preloads and cover it with tests. * Create quiet-horses-turn.md * Test merging preload tags 2021-09-10Fix typos in Netlify sponsorship announcement blog post (#1346)Gravatar mundry 1-4/+4 Co-authored-by: mundry <mundry@users.noreply.github.com> 2021-09-10[ci] collect statsGravatar FredKSchott 2-1/+2 2021-09-09blog: announce netlify sponsorship (#1345)Gravatar Fred K. Schott 4-5/+64 2021-09-09Version Packages (#1344)Gravatar github-actions[bot] 29-53/+42 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2021-09-09Revert "Version Packages (#1303)"Gravatar Fred K. Schott 29-42/+53 This reverts commit 1a88cfde7d2b3854e279c1bf3e12adf5d2155801. 2021-09-09update lockfileastro@0.20.5@astrojs/markdown-support@0.3.1Gravatar Fred K. Schott 1-9/+9 2021-09-09Version Packages (#1303)Gravatar github-actions[bot] 29-53/+42 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2021-09-09[ci] collect statsGravatar FredKSchott 2-1/+2 2021-09-08Update netlify deploy instructions for `.nvmrc` syntax (#1337)Gravatar Caleb Jasik 1-1/+1 2021-09-08[ci] yarn formatGravatar jasikpark 1-1/+0