Skip to content

Commit

Permalink
feat: cmd palette used via keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Deep-Codes committed Sep 3, 2021
1 parent 6e95b1e commit a8a4b43
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
62 changes: 57 additions & 5 deletions components/SearchModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import SearchIcon from '@/assets/icons/SearchIcon';
import useSearch from '@/lib/useSearch';
import EnterIcon from '@/assets/icons/EnterIcon';
import useClickOutside from '@/lib/useClickOutside';
import Link from 'next/link';
import useKeyPress from '@/lib/useKeyPress';

interface Props {
docs: { url: string; title: string; description: string; nav: number; content: string }[];
Expand All @@ -11,6 +13,7 @@ interface Props {
}

const SearchModal: React.FC<Props> = ({ docs, currentDocSlug, setModal }) => {
const paletteTrack = React.useRef(-1);
const [search, setSearch] = React.useState('');
const ref = React.useRef();
const inputRef = React.useRef();
Expand All @@ -25,6 +28,55 @@ const SearchModal: React.FC<Props> = ({ docs, currentDocSlug, setModal }) => {
folder: currentDocSlug,
docs
});
// reset if result is 0
if (res.length === 0) {
paletteTrack.current = -1;
}
const downKeyPressed = useKeyPress('ArrowDown');
const upKeyPressed = useKeyPress('ArrowUp');
if (downKeyPressed) {
// reset to top
if (paletteTrack.current >= res.length - 1) {
paletteTrack.current = 0;
const last = document.getElementById(`res-box-${res.length - 1}`);
if (last) {
last.style.backgroundColor = 'var(--gray3)';
}
} else {
paletteTrack.current += 1;
}
const ele = document.getElementById(`res-box-${paletteTrack.current}`);
if (ele) {
ele.style.backgroundColor = 'var(--gray8)';
ele.focus();
}
const prev = document.getElementById(`res-box-${paletteTrack.current - 1}`);
if (prev) {
prev.style.backgroundColor = 'var(--gray3)';
}
}
if (upKeyPressed) {
// on top
if (paletteTrack.current === 0) {
paletteTrack.current = res.length - 1;
const top = document.getElementById(`res-box-0`);
if (top) {
top.style.backgroundColor = 'var(--gray3)';
}
} else {
paletteTrack.current -= 1;
}

const ele = document.getElementById(`res-box-${paletteTrack.current}`);
if (ele) {
ele.style.backgroundColor = 'var(--gray8)';
ele.focus();
}
const prev = document.getElementById(`res-box-${paletteTrack.current + 1}`);
if (prev) {
prev.style.backgroundColor = 'var(--gray3)';
}
}
return (
// @ts-ignore
<div className="search-modal" ref={ref}>
Expand All @@ -42,17 +94,17 @@ const SearchModal: React.FC<Props> = ({ docs, currentDocSlug, setModal }) => {
</div>
{res.length > 0 ? (
<div className="res-ctx">
{res.map((e) => (
<a href={e.url} key={e.url}>
<div className="res-box">
{res.map((e, i) => (
<Link href={e.url} key={e.url}>
<a id={`res-box-${i}`} className="res-box">
<div>
<span>{e.title}</span>
<span className="slug">{e.url}</span>
</div>

<EnterIcon />
</div>
</a>
</a>
</Link>
))}
</div>
) : null}
Expand Down
3 changes: 2 additions & 1 deletion next-sitemap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
siteUrl: process.env.SITE_URL || 'https://docs.100ms.live/'
siteUrl: process.env.SITE_URL || 'https://docs.100ms.live/',
exclude: ['/api-reference/*']
};
30 changes: 0 additions & 30 deletions pages/api-references.tsx

This file was deleted.

0 comments on commit a8a4b43

Please sign in to comment.