Add hotkey to focus search bar

This commit is contained in:
bkellam 2024-09-02 19:19:22 -07:00
parent 45c176f7ae
commit dd80630f74
4 changed files with 22 additions and 1 deletions

View file

@ -33,6 +33,7 @@
"react": "^18", "react": "^18",
"react-dom": "^18", "react-dom": "^18",
"react-hook-form": "^7.53.0", "react-hook-form": "^7.53.0",
"react-hotkeys-hook": "^4.5.1",
"react-resizable-panels": "^2.1.1", "react-resizable-panels": "^2.1.1",
"sharp": "^0.33.5", "sharp": "^0.33.5",
"tailwind-merge": "^2.5.2", "tailwind-merge": "^2.5.2",

View file

@ -43,7 +43,9 @@ export default function Home() {
/> />
</div> </div>
<div className="w-full flex flex-row mt-4"> <div className="w-full flex flex-row mt-4">
<SearchBar /> <SearchBar
autoFocus={true}
/>
</div> </div>
</div> </div>
</div> </div>

View file

@ -14,11 +14,14 @@ import { cva } from "class-variance-authority";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { z } from "zod"; import { z } from "zod";
import { useHotkeys } from 'react-hotkeys-hook'
import { useRef } from "react";
interface SearchBarProps { interface SearchBarProps {
className?: string; className?: string;
size?: "default" | "sm"; size?: "default" | "sm";
defaultQuery?: string; defaultQuery?: string;
autoFocus?: boolean;
} }
const formSchema = z.object({ const formSchema = z.object({
@ -44,7 +47,15 @@ export const SearchBar = ({
className, className,
size, size,
defaultQuery, defaultQuery,
autoFocus,
}: SearchBarProps) => { }: SearchBarProps) => {
const inputRef = useRef<HTMLInputElement>(null);
useHotkeys('/', (event) => {
event.preventDefault();
inputRef.current?.focus();
});
const router = useRouter(); const router = useRouter();
const form = useForm<z.infer<typeof formSchema>>({ const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema), resolver: zodResolver(formSchema),
@ -73,6 +84,8 @@ export const SearchBar = ({
placeholder="Search..." placeholder="Search..."
className={cn(searchBarVariants({ size, className }))} className={cn(searchBarVariants({ size, className }))}
{...field} {...field}
ref={inputRef}
autoFocus={autoFocus ?? false}
/> />
</FormControl> </FormControl>
<FormMessage /> <FormMessage />

View file

@ -2889,6 +2889,11 @@ react-hook-form@^7.53.0:
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.53.0.tgz#3cf70951bf41fa95207b34486203ebefbd3a05ab" resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.53.0.tgz#3cf70951bf41fa95207b34486203ebefbd3a05ab"
integrity sha512-M1n3HhqCww6S2hxLxciEXy2oISPnAzxY7gvwVPrtlczTM/1dDadXgUxDpHMrMTblDOcm/AXtXxHwZ3jpg1mqKQ== integrity sha512-M1n3HhqCww6S2hxLxciEXy2oISPnAzxY7gvwVPrtlczTM/1dDadXgUxDpHMrMTblDOcm/AXtXxHwZ3jpg1mqKQ==
react-hotkeys-hook@^4.5.1:
version "4.5.1"
resolved "https://registry.yarnpkg.com/react-hotkeys-hook/-/react-hotkeys-hook-4.5.1.tgz#990260ecc7e5a431414148a93b02a2f1a9707897"
integrity sha512-scAEJOh3Irm0g95NIn6+tQVf/OICCjsQsC9NBHfQws/Vxw4sfq1tDQut5fhTEvPraXhu/sHxRd9lOtxzyYuNAg==
react-is@^16.13.1: react-is@^16.13.1:
version "16.13.1" version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"