Skip to content

Commit

Permalink
Close search field with Escape key
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed Jan 1, 2023
1 parent 3e8f463 commit 87b62c6
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/insipid_sphinx_theme/insipid/static/insipid.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
const search_button = document.getElementById('search-button');
if (search_button) {
const search_form = document.getElementById('search-form');
const search_field = search_form.querySelector('input');

function show_search() {
try {
Expand All @@ -81,7 +82,7 @@
} catch(e) {}
search_form.style.display = 'flex';
search_button.setAttribute('aria-expanded', 'true');
search_form.querySelector('input').focus();
search_field.focus();
document.body.classList.remove('topbar-folded');
}

Expand All @@ -104,6 +105,13 @@
// Monkey-patch function provided by Sphinx:
Documentation.focusSearchBar = show_search;
}

search_field.addEventListener('keydown', event => {
if (event.code === 'Escape') {
hide_search();
search_field.blur();
}
});
}

const fullscreen_button = document.getElementById('fullscreen-button');
Expand Down

0 comments on commit 87b62c6

Please sign in to comment.