Skip to content

Commit accf686

Browse files
committed
Auto merge of #13178 - GuillaumeGomez:clippy-lints-page-improvement, r=Alexendoo
Add possibility to focus on search input using keyboard This PR adds the possibility to focus on the search input with `S` or `/` like in rustdoc and `mdbook` and `docs.rs` (unification++). Pressing escape will blur it. r? `@Alexendoo` changelog: Add possibility to focus on search input using keyboard
2 parents 834b691 + 6317479 commit accf686

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
@@ -575,6 +575,32 @@ function setTheme(theme, store) {
575575
}
576576
}
577577

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

0 commit comments

Comments
 (0)