Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit ba93b77

Browse files
authored
Merge pull request #31 from readthedocs/hotkey-for-search-modal
Add forward slash as hotkey for search UI
2 parents 2c43687 + a786a0c commit ba93b77

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

sphinx_search/_static/js/rtd_sphinx_search.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ const MAX_SUGGESTIONS = 50;
22
const MAX_SECTION_RESULTS = 3;
33
const MAX_SUBSTRING_LIMIT = 100;
44

5+
// Possible states of search modal
6+
const SEARCH_MODAL_OPENED = "opened";
7+
const SEARCH_MODAL_CLOSED = "closed";
8+
9+
let SEARCH_MODAL_STATE = SEARCH_MODAL_CLOSED;
510
let TOTAL_PAGE_RESULTS = 0;
611
let SEARCH_QUERY = "";
712

@@ -603,6 +608,8 @@ const showSearchModal = custom_query => {
603608
// removes previous results (if there are any).
604609
removeResults();
605610

611+
SEARCH_MODAL_STATE = SEARCH_MODAL_OPENED;
612+
606613
// removes the focus from the initial input field
607614
// which as already present in the docs.
608615
let search_bar = getInputField();
@@ -634,6 +641,8 @@ const removeSearchModal = () => {
634641
// removes previous results before closing
635642
removeResults();
636643

644+
SEARCH_MODAL_STATE = SEARCH_MODAL_CLOSED;
645+
637646
// sets the value of input field to empty string and remove the focus.
638647
let search_outer_input = document.querySelector(".search__outer__input");
639648
if (search_outer_input !== null) {
@@ -786,6 +795,15 @@ window.addEventListener("DOMContentLoaded", evt => {
786795
}
787796
});
788797

798+
// open search modal if "forward slash" button is pressed
799+
document.addEventListener("keydown", e => {
800+
if (e.keyCode === 191) {
801+
if (SEARCH_MODAL_STATE !== SEARCH_MODAL_OPENED) {
802+
showSearchModal();
803+
}
804+
}
805+
});
806+
789807
// if "rtd_search" is present in URL parameters,
790808
// then open the search modal and show the results
791809
// for the value of "rtd_search"

sphinx_search/_static/js/rtd_sphinx_search.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)