Skip to content

Commit 6555972

Browse files
chore: autofocus the search input without scrolling the page
1 parent e118dbf commit 6555972

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Diff for: src/lib/links/SearchInput.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
import { useEffect, useRef } from "react";
2+
13
interface SearchInputProps {
24
searchQuery: string;
35
setSearchQuery: (query: string) => void;
46
}
57
export const SearchInput: React.FC<
68
SearchInputProps & React.HTMLProps<HTMLDivElement>
79
> = ({ searchQuery, setSearchQuery, ...props }) => {
10+
const ref = useRef<HTMLInputElement>(null);
11+
12+
useEffect(() => {
13+
if (ref.current) {
14+
// Autofocus the input field, without scrolling the page
15+
ref.current.focus({ preventScroll: true });
16+
}
17+
}, []);
18+
819
return (
920
<div className="relative flex w-full items-center gap-2" {...props}>
1021
<input
22+
ref={ref}
1123
autoComplete="off"
1224
spellCheck={false}
1325
className="inset-0 h-10 w-full resize-none rounded-lg border-2 border-brand-violet bg-pagebg p-3 text-base caret-brand-violet outline-none dark:text-white"

0 commit comments

Comments
 (0)