Skip to content

A platform for community discussion. Free, open, simple.

UX: Better up/down arrow Search navigation when Ask AI enabled (#43348)

Natalie Tayac76bb712964
diff --git a/frontend/discourse/app/services/search.js b/frontend/discourse/app/services/search.js
index 7aa523dd521..a4a9479389d 100644
--- a/frontend/discourse/app/services/search.js
+++ b/frontend/discourse/app/services/search.js
@@ -116,8 +116,12 @@ export default class Search extends Service {
}
if (index === -1 && e.key === "ArrowDown") {
- // change focus from the search input to the first navigation item
- const firstResult = navigationItems[0] || focusableItems[0];
+ const firstResult =
+ focused
+ .closest(".search-menu")
+ .querySelector(
+ ".search-result-topic .search-link, .search-result-post .search-link"
+ ) || navigationItems[0];
firstResult.focus();
} else if (index === 0 && e.key === "ArrowUp") {
this.focusSearchInput();
diff --git a/frontend/discourse/tests/integration/components/search-menu-test.gjs b/frontend/discourse/tests/integration/components/search-menu-test.gjs
index 0ea04f05cd9..d958c27fb59 100644
--- a/frontend/discourse/tests/integration/components/search-menu-test.gjs
+++ b/frontend/discourse/tests/integration/components/search-menu-test.gjs
@@ -70,6 +70,9 @@ module("Integration | Component | SearchMenu", function (hooks) {
assert
.dom(".search-result-topic")
.exists("search result is a list of topics");
+ assert
+ .dom("#icon-search-input")
+ .isFocused("search results keep input focus");
await triggerKeyEvent("#icon-search-input", "keydown", "Escape");
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs
index 959d8e31f25..4a362cd6883 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs
@@ -376,7 +376,6 @@ export default class AiDiscoveriesSearchOptions extends Component {
{{/each}}
<DButton
class="btn-default btn-small ai-discoveries-search-options__option --advanced"
- data-search-menu-navigation-item
@action={{@openAdvancedSearch}}
@ariaLabel="search.open_advanced"
@icon="sliders"
diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js
index 6ab8d2662d9..70a13322344 100644
--- a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js
+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js
@@ -4,6 +4,7 @@ import {
currentURL,
fillIn,
find,
+ findAll,
settled,
triggerKeyEvent,
visit,
@@ -398,6 +399,68 @@ acceptance("AI Discoveries - header search", function (needs) {
);
});
+ test("all topics keeps the query editable and down enters the results", async function (assert) {
+ await visit("/");
+ await click("#search-button");
+ await fillIn("#icon-search-input", "dev");
+
+ assert.dom("#icon-search-input").isFocused("typing keeps input focus");
+
+ await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
+
+ assert
+ .dom("#icon-search-input")
+ .isFocused("results leave focus in the input");
+ await fillIn("#icon-search-input", "dev 猫");
+ assert
+ .dom("#icon-search-input")
+ .hasValue("dev 猫", "the query can be edited");
+ await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
+ assert
+ .dom("#icon-search-input")
+ .isFocused("updated results leave focus in the input");
+
+ const results = findAll(".search-result-topic .search-link");
+ await triggerKeyEvent("#icon-search-input", "keyup", "ArrowDown");
+ assert
+ .dom(results[0])
+ .isFocused("down skips the buttons and enters the results");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");
+ assert.dom(results[1]).isFocused("down moves to the second result");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ assert.dom(results[0]).isFocused("up returns to the first result");
+ });
+
+ test("an empty search keeps focus in the input", async function (assert) {
+ pretender.get("/search/query", () => response({}));
+ await visit("/");
+ await click("#search-button");
+ await fillIn("#icon-search-input", "猫猫猫");
+ await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
+
+ assert.dom(".no-results").exists("the empty state appears");
+ assert.dom("#icon-search-input").isFocused("the query remains editable");
+ });
+
+ test("results do not take focus after moving away during a search", async function (assert) {
+ await visit("/");
+ await click("#search-button");
+ await fillIn("#icon-search-input", "dev");
+
+ find("#icon-search-input").dispatchEvent(
+ new KeyboardEvent("keyup", { key: "Enter", bubbles: true })
+ );
+ find(".ai-discoveries-search-options__option.--ask").focus();
+ await settled();
+
+ assert.dom(".search-result-topic").exists("the results arrive");
+ assert
+ .dom(".ai-discoveries-search-options__option.--ask")
+ .isFocused("the user's new focus is preserved");
+ });
+
test("scoping to a topic leaves the input alone", async function (assert) {
await visit("/t/internationalization-localization/280");
await click("#search-button");
@@ -455,6 +518,37 @@ acceptance("AI Discoveries - header search", function (needs) {
assert
.dom(".ai-discoveries-search-options__option.--topic")
.hasClass("is-active", "enter scopes the search to the topic");
+
+ const results = findAll(".search-result-post .search-link");
+ assert
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .isFocused("results leave focus on the selected scope button");
+
+ await click("#icon-search-input");
+ await triggerKeyEvent("#icon-search-input", "keyup", "ArrowDown");
+ assert.dom(results[0]).isFocused("down enters the matching posts");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");
+ assert.dom(results[1]).isFocused("down moves to the second matching post");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ assert.dom(results[0]).isFocused("up returns to the first matching post");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ assert
+ .dom(".ai-discoveries-search-options__option.--search")
+ .isFocused("up from the first result skips advanced search");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");
+ assert
+ .dom(results[0])
+ .isFocused("down from the buttons skips advanced search");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ assert
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .isFocused("the topic scope remains reachable with arrow keys");
});
test("still offers itself from a message inbox", async function (assert) {