mirror of
https://github.com/snachodog/just-the-docs.git
synced 2025-09-12 21:03:32 -06:00
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:
@@ -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;
|
||||
|
Reference in New Issue
Block a user