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

docs: "/" or "CMD+K" should focus search bar #1852

Merged
merged 1 commit into from
Nov 21, 2023
Merged
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
29 changes: 29 additions & 0 deletions docs/src/pages/_layout.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<script context="module">
// Pressing these keys will focus the search bar.
const FOCUS_KEYS = new Set(["/", "Metak"]);
</script>

<script>
import {
isActive,
Expand Down Expand Up @@ -42,6 +47,7 @@
const deprecated = [];
const new_components = [];

let keys = "";
let value = "";
let active = false;
$: results = miniSearch.search(value).slice(0, 10);
Expand All @@ -64,6 +70,29 @@
<!-- routify:options bundle=true -->
<svelte:window bind:innerWidth />

<svelte:body
on:keydown="{(e) => {
keys += e.key;
}}"
on:keyup="{(e) => {
if (active) return;

if (FOCUS_KEYS.has(keys)) {
if (
document.activeElement instanceof HTMLInputElement ||
document.activeElement instanceof HTMLTextAreaElement
) {
// Exit early if an inputtable element is already focused.
return;
}

e.preventDefault();
active = true;
}

keys = '';
}}" />

<Theme persist bind:theme="{$theme}">
<Header
aria-label="Navigation"
Expand Down
Loading