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.
This commit is contained in:
Travis Cross 2025-05-11 09:33:01 +00:00
parent 84a5ba9707
commit 3e871d1971

View file

@ -367,12 +367,16 @@ window.search = window.search || {};
showSearch(true);
window.scrollTo(0, 0);
searchbar.select();
} else if (hasFocus() && e.keyCode === DOWN_KEYCODE) {
} else if (hasFocus() && (e.keyCode === DOWN_KEYCODE
|| e.keyCode === SELECT_KEYCODE)) {
e.preventDefault();
const first = searchresults.firstElementChild;
if (first !== null) {
unfocusSearchbar();
first.classList.add('focus');
if (e.keyCode === SELECT_KEYCODE) {
window.location.assign(first.querySelector('a'));
}
}
} else if (!hasFocus() && (e.keyCode === DOWN_KEYCODE
|| e.keyCode === UP_KEYCODE