Skip to content

Commit

Permalink
docs: fix search bar focus on Esc (#16700)
Browse files Browse the repository at this point in the history
* fix: search bar focus on `Esc`

* chore: remove new line
  • Loading branch information
shanpriyan committed Dec 24, 2022
1 parent f62b722 commit a638673
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions docs/src/assets/js/search.js
Expand Up @@ -136,33 +136,33 @@ const debouncedFetchSearchResults = debounce((query) => {
//-----------------------------------------------------------------------------

// listen for input changes
if(searchInput)
if (searchInput)
searchInput.addEventListener('keyup', function (e) {
const query = searchInput.value;

if(query === searchQuery) return;
if (query === searchQuery) return;

if(query.length) searchClearBtn.removeAttribute('hidden');
if (query.length) searchClearBtn.removeAttribute('hidden');
else searchClearBtn.setAttribute('hidden', '');

if (query.length > 2) {

debouncedFetchSearchResults(query);

document.addEventListener('click', function(e) {
if(e.target !== resultsElement) clearSearchResults();
document.addEventListener('click', function (e) {
if (e.target !== resultsElement) clearSearchResults();
});
} else {
clearSearchResults();
}

searchQuery = query
searchQuery = query

});


if(searchClearBtn)
searchClearBtn.addEventListener('click', function(e) {
if (searchClearBtn)
searchClearBtn.addEventListener('click', function (e) {
searchInput.value = '';
searchInput.focus();
clearSearchResults();
Expand All @@ -171,10 +171,14 @@ if(searchClearBtn)

document.addEventListener('keydown', function (e) {

const searchResults = Array.from(document.querySelectorAll('.search-results__item'));

if (e.key === 'Escape') {
e.preventDefault();
clearSearchResults();
searchInput.focus();
if (searchResults.length) {
clearSearchResults();
searchInput.focus();
}
}

if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
Expand All @@ -183,7 +187,6 @@ document.addEventListener('keydown', function (e) {
document.querySelector('.search').scrollIntoView({ behavior: "smooth", block: "start" });
}

const searchResults = Array.from(document.querySelectorAll('.search-results__item'));
if (!searchResults.length) return;

switch (e.key) {
Expand Down

0 comments on commit a638673

Please sign in to comment.