Skip to content

Commit

Permalink
docs: rework keyboard shortcuts to focus search bar
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Nov 21, 2023
1 parent f630007 commit 979255d
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions docs/src/pages/_layout.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<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 @@ -47,7 +42,6 @@
const deprecated = [];
const new_components = [];
let keys = "";
let value = "";
let active = false;
$: results = miniSearch.search(value).slice(0, 10);
Expand All @@ -72,25 +66,18 @@

<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;
}

if (
document.activeElement instanceof HTMLInputElement ||
document.activeElement instanceof HTMLTextAreaElement
) {
// Exit early if an inputtable element is already focused.
return;
}
if ((e.metaKey && e.key === 'k') || e.key === '/') {
e.preventDefault();
active = true;
}

keys = '';
}}" />

<Theme persist bind:theme="{$theme}">
Expand Down

0 comments on commit 979255d

Please sign in to comment.