Skip to content

Commit

Permalink
Monkey-patch focusSearchBar() to support "/" key binding
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed Aug 25, 2022
1 parent df1bebf commit 0ecb886
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#'body_centered': False,
#'body_max_width': None,
#'breadcrumbs': True,
#'enable_search_shortcuts': False,
#'globaltoc_collapse': False,
#'globaltoc_includehidden': True,
#'initial_sidebar_visibility_threshold': None,
Expand Down
38 changes: 27 additions & 11 deletions src/insipid_sphinx_theme/insipid/static/insipid.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,42 @@
event.preventDefault();
});

// show search
const search_form = document.getElementById('search-form');
const search_button = document.getElementById('search-button');
search_button.addEventListener('click', event => {
try {
// https://readthedocs-sphinx-search.readthedocs.io/
showSearchModal();
return;
} catch(e) {}
if (window.getComputedStyle(search_form).display === 'none') {
if (search_button) {
const search_form = document.getElementById('search-form');

function show_search() {
try {
// https://readthedocs-sphinx-search.readthedocs.io/
showSearchModal();
return;
} catch(e) {}
search_form.style.display = 'flex';
search_button.setAttribute('aria-expanded', 'true');
search_form.querySelector('input').focus();
document.body.classList.remove('topbar-folded');
} else {
}

function hide_search() {
search_form.style.display = 'none';
search_button.setAttribute('aria-expanded', 'false');
search_button.blur();
}
});

function toggle_search() {
if (window.getComputedStyle(search_form).display === 'none') {
show_search();
} else {
hide_search();
}
}

search_button.addEventListener('click', toggle_search);
if (Documentation.focusSearchBar) {
// Monkey-patch function provided by Sphinx:
Documentation.focusSearchBar = show_search;
}
}

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

0 comments on commit 0ecb886

Please sign in to comment.