Skip to content

Commit f80ff16

Browse files
authored
feat: focus search input on page load (#815)
When a user opens [jsr.io](https://jsr.io), the page shows a big input element which is used to search for packages. It appears to be the primary call to action element and I believe by default, it should be focused on page load because most of the times, a user would visit jsr to search for a package. This behaviour would also align with [google.com](https://google.com)
1 parent d880926 commit f80ff16

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

frontend/islands/GlobalSearch.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { OramaPackageHit, SearchKind } from "../util.ts";
99
import { api, path } from "../utils/api.ts";
1010
import type { List, Package, RuntimeCompat } from "../utils/api_types.ts";
1111
import { PackageHit } from "../components/PackageHit.tsx";
12-
import { useMacLike } from "../utils/os.ts";
12+
import { useIsMobileDevice, useMacLike } from "../utils/os.ts";
1313
import type { ListDisplayItem } from "../components/List.tsx";
1414
import { RUNTIME_COMPAT_KEYS } from "../components/RuntimeCompatIndicator.tsx";
1515

@@ -55,6 +55,7 @@ export function GlobalSearch(
5555
isFocused.value && search.value.length > 0
5656
);
5757
const macLike = useMacLike();
58+
const isMobileDevice = useIsMobileDevice();
5859

5960
const orama = useMemo(() => {
6061
if (IS_BROWSER && indexId) {
@@ -65,6 +66,14 @@ export function GlobalSearch(
6566
}
6667
}, [indexId, apiKey]);
6768

69+
// focus the "search for packages" input box when the site loads
70+
useEffect(() => {
71+
if (location.pathname === "/" && !isMobileDevice) {
72+
(document.querySelector("#global-search-input") as HTMLInputElement)
73+
?.focus();
74+
}
75+
}, []);
76+
6877
useEffect(() => {
6978
const outsideClick = (e: Event) => {
7079
if (!ref.current) return;

frontend/utils/os.ts

+7
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export function useMacLike(): boolean | undefined {
66
if (!IS_BROWSER) return undefined;
77
return !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i);
88
}
9+
10+
export function useIsMobileDevice(): boolean | undefined {
11+
if (!IS_BROWSER) return undefined;
12+
return !!navigator.userAgent.match(
13+
/Android|webOS|iPhone|iPad|iPod|BlackBerry/i,
14+
);
15+
}

0 commit comments

Comments
 (0)