Skip to content

Commit 6317479

Browse files
Add possibility to focus on search input using keyboard
1 parent f7db895 commit 6317479

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

util/gh-pages/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ <h1>Clippy Lints</h1>
541541
<div class="col-12 col-md-5 search-control">
542542
<div class="input-group">
543543
<label class="input-group-addon" id="filter-label" for="search-input">Filter:</label>
544-
<input type="text" class="form-control filter-input" placeholder="Keywords or search string" id="search-input"
544+
<input type="text" class="form-control filter-input" placeholder="Keywords or search string (`S` or `/` to focus)" id="search-input"
545545
ng-model="search" ng-blur="updatePath()" ng-keyup="$event.keyCode == 13 && updatePath()"
546546
ng-model-options="{debounce: 50}" />
547547
<span class="input-group-btn">

util/gh-pages/script.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,32 @@ function setTheme(theme, store) {
579579
}
580580
}
581581

582+
function handleShortcut(ev) {
583+
if (ev.ctrlKey || ev.altKey || ev.metaKey) {
584+
return;
585+
}
586+
587+
if (document.activeElement.tagName === "INPUT") {
588+
if (ev.key === "Escape") {
589+
document.activeElement.blur();
590+
}
591+
} else {
592+
switch (ev.key) {
593+
case "s":
594+
case "S":
595+
case "/":
596+
ev.preventDefault(); // To prevent the key to be put into the input.
597+
document.getElementById("search-input").focus();
598+
break;
599+
default:
600+
break;
601+
}
602+
}
603+
}
604+
605+
document.addEventListener("keypress", handleShortcut);
606+
document.addEventListener("keydown", handleShortcut);
607+
582608
// loading the theme after the initial load
583609
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
584610
const theme = localStorage.getItem('clippy-lint-list-theme');

0 commit comments

Comments
 (0)