File size: 4,328 Bytes
1e92f2d | 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import { useMemo, useCallback, useRef, useState, useEffect } from 'react';
import matchSorter from 'match-sorter';
import cn from 'classnames';
import { useRouter } from 'next/router';
import Link from 'next/link';
const Item = ({ title, active, href, onMouseOver, search }) => {
const highlight = title.toLowerCase().indexOf(search.toLowerCase());
return (
<Link href={href}>
<a className="block no-underline" onMouseOver={onMouseOver}>
<li
className={cn('py-2 px-3 text-gray-800', {
'bg-gray-100': active,
})}
>
{title.substring(0, highlight)}
<span className="bg-teal-300">
{title.substring(highlight, highlight + search.length)}
</span>
{title.substring(highlight + search.length)}
</li>
</a>
</Link>
);
};
const Search = ({ directories }) => {
const router = useRouter();
const [show, setShow] = useState(false);
const [search, setSearch] = useState('');
const [active, setActive] = useState(0);
const input = useRef(null);
const results = useMemo(() => {
if (!search) return [];
// Will need to scrape all the headers from each page and search through them here
// (similar to what we already do to render the hash links in sidebar)
// We could also try to search the entire string text from each page
return matchSorter(directories, search, { keys: ['title'] });
}, [search]);
const handleKeyDown = useCallback(
(e) => {
switch (e.key) {
case 'ArrowDown': {
e.preventDefault();
if (active + 1 < results.length) {
setActive(active + 1);
}
break;
}
case 'ArrowUp': {
e.preventDefault();
if (active - 1 >= 0) {
setActive(active - 1);
}
break;
}
case 'Enter': {
router.push(results[active].route);
break;
}
}
},
[active, results, router]
);
useEffect(() => {
setActive(0);
}, [search]);
useEffect(() => {
const inputs = ['input', 'select', 'button', 'textarea'];
const down = (e) => {
if (
document.activeElement &&
inputs.indexOf(document.activeElement.tagName.toLowerCase() !== -1)
) {
if (e.key === '/') {
e.preventDefault();
input.current.focus();
} else if (e.key === 'Escape') {
setShow(false);
}
}
};
window.addEventListener('keydown', down);
return () => window.removeEventListener('keydown', down);
}, []);
const renderList = show && results.length > 0;
return (
<div className="relative w-full md:w-64 mr-2">
{renderList && (
<div className="search-overlay z-1" onClick={() => setShow(false)} />
)}
<div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
<svg
fill="none"
strokeLinecap="round"
strokeLinejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
stroke="currentColor"
className="h-4 w-4 text-gray-400"
>
<path d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
</svg>
</div>
<input
onChange={(e) => {
setSearch(e.target.value);
setShow(true);
}}
className="appearance-none pl-8 border rounded-md py-2 pr-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline w-full"
type="search"
placeholder='Search ("/" to focus)'
onKeyDown={handleKeyDown}
onFocus={() => setShow(true)}
ref={input}
aria-label="Search documentation"
/>
{renderList && (
<ul className="shadow-md list-none p-0 m-0 absolute left-0 md:right-0 bg-white rounded-md mt-1 border top-100 divide-y divide-gray-300 z-2">
{results.map((res, i) => {
return (
<Item
key={`search-item-${i}`}
title={res.title}
href={res.route}
active={i === active}
search={search}
onMouseOver={() => setActive(i)}
/>
);
})}
</ul>
)}
</div>
);
};
export default Search;
|