Skip to content

Commit b476a93

Browse files
authored
docs: Fix Keyboard Navigation for Search Results (#19416)
* docs: Fix Keyboard Navigation for Search Results * remove explicit tab key handling
1 parent ccb60c0 commit b476a93

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

docs/src/assets/js/search.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ if (searchInput)
199199
}
200200

201201
searchQuery = query
202-
203202
});
204203

205204

@@ -218,17 +217,15 @@ if (poweredByLink) {
218217
}
219218

220219
document.addEventListener('keydown', function (e) {
221-
222220
const searchResults = Array.from(document.querySelectorAll('.search-results__item'));
221+
const isArrowKey = e.key === "ArrowUp" || e.key === "ArrowDown";
223222

224223
if (e.key === "Escape") {
225224
e.preventDefault();
226225
if (searchResults.length) {
227226
clearSearchResults(true);
228227
searchInput.focus();
229-
} else if (
230-
document.activeElement === searchInput
231-
) {
228+
} else if (document.activeElement === searchInput) {
232229
clearNoResults();
233230
searchInput.blur();
234231
}
@@ -242,21 +239,23 @@ document.addEventListener('keydown', function (e) {
242239

243240
if (!searchResults.length) return;
244241

245-
switch (e.key) {
246-
case "ArrowUp":
247-
e.preventDefault();
242+
if (isArrowKey) {
243+
e.preventDefault();
244+
245+
if (e.key === "ArrowUp") {
248246
activeIndex = activeIndex - 1 < 0 ? searchResults.length - 1 : activeIndex - 1;
249-
break;
250-
case "ArrowDown":
251-
e.preventDefault();
247+
} else if (e.key === "ArrowDown") {
252248
activeIndex = activeIndex + 1 < searchResults.length ? activeIndex + 1 : 0;
253-
break;
254-
}
249+
}
250+
251+
if (activeIndex !== -1) {
252+
const activeSearchResult = searchResults[activeIndex];
253+
activeSearchResult.querySelector('a').focus();
255254

256-
if (activeIndex === -1) return;
257-
const activeSearchResult = searchResults[activeIndex];
258-
activeSearchResult.querySelector('a').focus();
259-
if (isScrollable(resultsElement)) {
260-
maintainScrollVisibility(activeSearchResult, resultsElement);
255+
if (isScrollable(resultsElement)) {
256+
maintainScrollVisibility(activeSearchResult, resultsElement);
257+
}
258+
}
261259
}
262260
});
261+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy