A platform for community discussion. Free, open, simple.
A11Y: Make the Ask AI search options a single tab stop with arrow key nav (#43709)
Kris177017e5a6e7
diff --git a/frontend/discourse/app/services/search.js b/frontend/discourse/app/services/search.jsindex 081dbd3ca2a..d28809f7531 100644--- a/frontend/discourse/app/services/search.js+++ b/frontend/discourse/app/services/search.js@@ -101,11 +101,13 @@ export default class Search extends Service {return;}+ const groupedItem =+ ".search-menu .results [data-search-menu-navigation-item]:not([tabindex='-1'])";const focusableItems = document.querySelectorAll(- ".search-menu .results a, .search-menu .results [data-search-menu-navigation-item]"+ `.search-menu .results a, ${groupedItem}`);const navigationItems = document.querySelectorAll(- ".search-menu .results .search-link, .search-menu .results [data-search-menu-navigation-item]"+ `.search-menu .results .search-link, ${groupedItem}`);if (!navigationItems.length) {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.gjsindex 2b391a6eeb5..53d63374655 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@@ -5,6 +5,7 @@ import { service } from "@ember/service";import { MODIFIER_REGEXP } from "discourse/components/search-menu";import escapeRegExp from "discourse/lib/escape-regexp";import DButton from "discourse/ui-kit/d-button";+import dRovingFocus from "discourse/ui-kit/modifiers/d-roving-focus";import { i18n } from "discourse-i18n";import shortcutLabel from "../lib/shortcut-label";@@ -235,6 +236,10 @@ export default class AiDiscoveriesSearchOptions extends Component {: [...scopes, search, ask];}+ get optionsKey() {+ return this.options.map((option) => option.kind).join();+ }+get resolveOptions() {const search = {kind: "search",@@ -352,10 +357,17 @@ export default class AiDiscoveriesSearchOptions extends Component {}<template>- {{! eslint-disable ember/template-no-invalid-interactive }}<div+ aria-label={{i18n "discourse_ai.discobot_discoveries.search_options"}}class="ai-discoveries-search-options"+ role="toolbar"{{on "keydown" this.search.handleArrowUpOrDown}}+ {{dRovingFocus+ orientation="horizontal"+ itemSelector=".ai-discoveries-search-options__option"+ entryFocus="first"+ itemsKey=this.optionsKey+ }}>{{#each this.options key="kind" as |option|}}<DButton@@ -371,6 +383,7 @@ export default class AiDiscoveriesSearchOptions extends Component {{{/each}}<DButtonclass="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/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scssindex 4ca211a915a..bcc0c84d21f 100644--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss@@ -258,6 +258,10 @@background: var(--d-selected);color: var(--d-selected-text-color);}++ &:focus-visible {+ @include default-focus;+ }}}diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.ymlindex 5882417f6f6..7014dd5f350 100644--- a/plugins/discourse-ai/config/locales/client.en.yml+++ b/plugins/discourse-ai/config/locales/client.en.yml@@ -1704,6 +1704,7 @@ en:dismiss_default_preference: "Hide this setting"default_preference_dismissed: "This setting is also available in your user preferences"shortcut_hint: "or %{shortcut}"+ search_options: "Search options"advanced_with_shortcut: "Open advanced search (%{shortcut})"search_all_topics: "All topics"search_this_topic: "This topic"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.jsindex 7765b3b7ea9..b8b36c7f93d 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@@ -562,7 +562,7 @@ acceptance("AI Discoveries - header search", function (needs) {await triggerKeyEvent(document, "keypress", "/".charCodeAt(0));await fillIn("#icon-search-input", "雰囲");await triggerKeyEvent("#icon-search-input", "keyup", "ArrowDown");- await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowRight");await triggerKeyEvent(document.activeElement, "keydown", "Enter");await triggerKeyEvent(document.activeElement, "keyup", "Enter");@@ -716,11 +716,11 @@ acceptance("AI Discoveries - header search", function (needs) {await fillIn("#icon-search-input", "dev");await triggerKeyEvent("#icon-search-input", "keyup", "ArrowDown");- await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowRight");assert.dom(".ai-discoveries-search-options__option.--topic")- .isFocused("arrow navigation focuses the topic scope option");+ .isFocused("right moves along the options to the topic scope");await triggerKeyEvent(document.activeElement, "keydown", "Enter");assert@@ -743,19 +743,19 @@ acceptance("AI Discoveries - header search", function (needs) {await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");assert- .dom(".ai-discoveries-search-options__option.--search")- .isFocused("up from the first result skips advanced search");+ .dom(".ai-discoveries-search-options__option.--topic")+ .isFocused("up from the first result returns to the option last used");await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");assert.dom(results[0])- .isFocused("down from the buttons skips advanced search");+ .isFocused("down from the options enters the results");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");+ .dom("#icon-search-input")+ .isFocused("the options are a single stop on the way up to the query");});test("still offers itself from a message inbox", async function (assert) {diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjsindex d4de5d0eedf..0fe730d53ff 100644--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjs+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjs@@ -1,6 +1,12 @@import { tracked } from "@glimmer/tracking";import Service from "@ember/service";-import { click, render, settled } from "@ember/test-helpers";+import {+ click,+ focus,+ render,+ settled,+ triggerKeyEvent,+} from "@ember/test-helpers";import { module, test } from "qunit";import { formatShortcut } from "discourse/lib/shortcut-format";import Category from "discourse/models/category";@@ -888,6 +894,59 @@ module();});+ test("the options are one tab stop, walked with the arrow keys", async function (assert) {+ await render(<template><AiDiscoveriesSearchOptions /></template>);++ assert+ .dom(".ai-discoveries-search-options")+ .hasAttribute("role", "toolbar", "the options read as one group");+ assert+ .dom(".ai-discoveries-search-options__option[tabindex='0']")+ .exists({ count: 1 }, "only one option is reachable with Tab");+ assert+ .dom(".ai-discoveries-search-options__option.--search")+ .hasAttribute("tabindex", "0", "and it is the one listed first");++ await focus(".ai-discoveries-search-options__option.--search");+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowRight");++ assert+ .dom(".ai-discoveries-search-options__option.--ask")+ .isFocused("right moves to the next option");+ assert+ .dom(".ai-discoveries-search-options__option.--ask")+ .hasAttribute("tabindex", "0", "and Tab comes back to it");++ await triggerKeyEvent(document.activeElement, "keydown", "ArrowRight");++ assert+ .dom(".ai-discoveries-search-options__option.--advanced")+ .isFocused("advanced search is part of the group");++ await triggerKeyEvent(document.activeElement, "keydown", "ArrowLeft");++ assert+ .dom(".ai-discoveries-search-options__option.--ask")+ .isFocused("left moves back");+ });++ test("an option joining the group does not add a tab stop", async function (assert) {+ await render(<template><AiDiscoveriesSearchOptions /></template>);++ this.owner.lookup("service:search").searchContext = {+ type: "topic",+ id: 280,+ };+ await settled();++ assert+ .dom(".ai-discoveries-search-options__option.--topic")+ .exists("the topic scope joins the group");+ assert+ .dom(".ai-discoveries-search-options__option[tabindex='0']")+ .exists({ count: 1 }, "and the group is still a single tab stop");+ });+test("offers the search options before typing", async function (assert) {const search = this.owner.lookup("service:search");search.activeGlobalSearchTerm = "";