Skip to content

Commit

Permalink
search: Add global hotkey to focus search (#168)
Browse files Browse the repository at this point in the history
* search: Add keyboard navigation to results

After searching for a string, the user can navigate results by pressing up/down arrows.

* search: Add global hotkey to focus search

* Commit from GitHub Actions (Applying autofixes)

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
zsol and github-actions[bot] committed May 27, 2024
1 parent 1c4cae1 commit a2af507
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions www/components/core/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from "@emotion/styled";
import fuzzysort from "fuzzysort";
import { ReactElement, useEffect, useState } from "react";
import { useHotkeys } from "react-hotkeys-hook";

import { Index, Result, Results, search } from "@/lib/searchDescriptor";

Expand Down Expand Up @@ -86,6 +87,10 @@ function Match({ result }: MatchProps): ReactElement {
export const Search = ({ descriptors }: SearchParams) => {
const [searchTerm, setSearchTerm] = useState("");
const [results, setResults] = useState<Results | null>(null);
useHotkeys("/", (event) => {
event.preventDefault();
document.getElementById("Search_Input")?.focus();
});

useEffect(() => {
// TODO: debounce this
Expand Down
10 changes: 10 additions & 0 deletions www/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions www/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"next": "^14.1.1",
"react": "^18.2.0",
"react-dom": "18.2.0",
"react-hotkeys-hook": "^4.5.0",
"react-router-dom": "^6.22.1",
"react-syntax-highlighter": "^15.5.0",
"swr": "^1.3.0"
Expand Down

0 comments on commit a2af507

Please sign in to comment.