Add configurable keyboard shortcut to focus search input (#1411)

Adds a keyboard shortcut to use Ctrl/Cmd and a configurable key to focus the search input. The key is configurable with `search.focus_shortcut_key`; enables it on the core docs site with `'k'` (default on many sites).

---------
Co-authored-by: Matt Wang <mxw@cs.washington.edu>
This commit is contained in:
Roman KC
2024-02-22 02:28:45 +05:45
committed by GitHub
parent 52b4b444d0
commit 01719a8752
5 changed files with 35 additions and 1 deletions

View File

@@ -145,6 +145,18 @@ function searchLoaded(index, docs) {
var currentInput;
var currentSearchIndex = 0;
{%- if site.search.focus_shortcut_key %}
// add event listener on ctrl + <focus_shortcut_key> for showing the search input
jtd.addEvent(document, 'keydown', function (e) {
if ((e.ctrlKey || e.metaKey) && e.key === '{{ site.search.focus_shortcut_key }}') {
e.preventDefault();
mainHeader.classList.add('nav-open');
searchInput.focus();
}
});
{%- endif %}
function showSearch() {
document.documentElement.classList.add('search-active');
}
@@ -488,7 +500,7 @@ jtd.setTheme = function(theme) {
function navLink() {
var pathname = document.location.pathname;
var navLink = document.getElementById('site-nav').querySelector('a[href="' + pathname + '"]');
if (navLink) {
return navLink;