Skip to content

Commit

Permalink
feature(web): Add keyboard shortcut to focus on search bar. Fixes #449 (
Browse files Browse the repository at this point in the history
#554)

added ctrl+k to focus the search bar
added escape to delete the input of the search bar
fixed behavior of ctrl+e on windows, which would otherwise focus the chrome searchbar
  • Loading branch information
kamtschatka authored Oct 19, 2024
1 parent 9a56e58 commit cbc268e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions apps/web/components/dashboard/bookmarks/EditorCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function useFocusOnKeyPress(inputRef: React.RefObject<HTMLTextAreaElement>) {
}
if ((e.metaKey || e.ctrlKey) && e.code === "KeyE") {
inputRef.current.focus();
e.preventDefault();
}
}

Expand Down
51 changes: 44 additions & 7 deletions apps/web/components/dashboard/search/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
"use client";

import React, { useEffect } from "react";
import React, { useEffect, useImperativeHandle, useRef } from "react";
import { Input } from "@/components/ui/input";
import { useDoBookmarkSearch } from "@/lib/hooks/bookmark-search";

function useFocusSearchOnKeyPress(
inputRef: React.RefObject<HTMLInputElement>,
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void,
) {
useEffect(() => {
function handleKeyPress(e: KeyboardEvent) {
if (!inputRef.current) {
return;
}
if ((e.metaKey || e.ctrlKey) && e.code === "KeyK") {
e.preventDefault();
inputRef.current.focus();
// Move the cursor to the end of the input field, so you can continue typing
const length = inputRef.current.value.length;
inputRef.current.setSelectionRange(length, length);
}
if (e.code === "Escape") {
e.preventDefault();
inputRef.current.blur();
inputRef.current.value = "";
onChange({
target: inputRef.current,
} as React.ChangeEvent<HTMLInputElement>);
}
}

document.addEventListener("keydown", handleKeyPress);
return () => {
document.removeEventListener("keydown", handleKeyPress);
};
}, [inputRef, onChange]);
}

const SearchInput = React.forwardRef<
HTMLInputElement,
React.HTMLAttributes<HTMLInputElement> & { loading?: boolean }
Expand All @@ -12,20 +45,24 @@ const SearchInput = React.forwardRef<

const [value, setValue] = React.useState(searchQuery);

const inputRef = useRef<HTMLInputElement>(null);
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
debounceSearch(e.target.value);
};

useFocusSearchOnKeyPress(inputRef, onChange);
useImperativeHandle(ref, () => inputRef.current!);

useEffect(() => {
if (!isInSearchPage) {
setValue("");
}
}, [isInSearchPage]);

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setValue(e.target.value);
debounceSearch(e.target.value);
};

return (
<Input
ref={ref}
ref={inputRef}
value={value}
onChange={onChange}
placeholder="Search"
Expand Down

0 comments on commit cbc268e

Please sign in to comment.