Skip to content

Commit 2703081

Browse files
committed
Navigate to first search result on enter
It's common for search boxes like ours to automatically navigate to the first search result when the `enter` / `select` key is pressed, as that can allow for rapid navigation. E.g., the MDN documentation does this. Let's similarly navigate to the first result when, in the search box, the user presses the `enter` key and there is a first result to which to navigate.
1 parent 512d429 commit 2703081

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/front-end/searcher/searcher.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,16 @@ window.search = window.search || {};
367367
showSearch(true);
368368
window.scrollTo(0, 0);
369369
searchbar.select();
370-
} else if (hasFocus() && e.keyCode === DOWN_KEYCODE) {
370+
} else if (hasFocus() && (e.keyCode === DOWN_KEYCODE
371+
|| e.keyCode === SELECT_KEYCODE)) {
371372
e.preventDefault();
372373
const first = searchresults.firstElementChild;
373374
if (first !== null) {
374375
unfocusSearchbar();
375376
first.classList.add('focus');
377+
if (e.keyCode === SELECT_KEYCODE) {
378+
window.location.assign(first.querySelector('a'));
379+
}
376380
}
377381
} else if (!hasFocus() && (e.keyCode === DOWN_KEYCODE
378382
|| e.keyCode === UP_KEYCODE

0 commit comments

Comments
 (0)