From 3e871d1971479cf38b12047d07204608c5b07076 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Sun, 11 May 2025 09:33:01 +0000 Subject: [PATCH] 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. --- src/front-end/searcher/searcher.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/front-end/searcher/searcher.js b/src/front-end/searcher/searcher.js index d7f9f580..476c17e4 100644 --- a/src/front-end/searcher/searcher.js +++ b/src/front-end/searcher/searcher.js @@ -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