Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: focus search input on page load #815

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion frontend/islands/GlobalSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { OramaPackageHit, SearchKind } from "../util.ts";
import { api, path } from "../utils/api.ts";
import type { List, Package, RuntimeCompat } from "../utils/api_types.ts";
import { PackageHit } from "../components/PackageHit.tsx";
import { useMacLike } from "../utils/os.ts";
import { useIsMobileDevice, useMacLike } from "../utils/os.ts";
import type { ListDisplayItem } from "../components/List.tsx";
import { RUNTIME_COMPAT_KEYS } from "../components/RuntimeCompatIndicator.tsx";

Expand Down Expand Up @@ -55,6 +55,7 @@ export function GlobalSearch(
isFocused.value && search.value.length > 0
);
const macLike = useMacLike();
const isMobileDevice = useIsMobileDevice();

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

// focus the "search for packages" input box when the site loads
useEffect(() => {
if (location.pathname === "/" && !isMobileDevice) {
(document.querySelector("#global-search-input") as HTMLInputElement)
?.focus();
}
}, []);

useEffect(() => {
const outsideClick = (e: Event) => {
if (!ref.current) return;
Expand Down
7 changes: 7 additions & 0 deletions frontend/utils/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ export function useMacLike(): boolean | undefined {
if (!IS_BROWSER) return undefined;
return !!navigator.platform.match(/(Mac|iPhone|iPod|iPad)/i);
}

export function useIsMobileDevice(): boolean | undefined {
if (!IS_BROWSER) return undefined;
return !!navigator.userAgent.match(
/Android|webOS|iPhone|iPad|iPod|BlackBerry/i,
);
}
Loading