A platform for community discussion. Free, open, simple.
A11Y: improve reactions for screenreaders and keyboard nav (#38546)
Krisf59f5945722d
diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjsindex 7b09d1dceab..fca22c51d0a 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-counter.gjs@@ -8,6 +8,7 @@ import { uniqueItemsFromArray } from "discourse/lib/array-tools";import { bind } from "discourse/lib/decorators";import closeOnClickOutside from "discourse/modifiers/close-on-click-outside";import { and } from "discourse/truth-helpers";+import { i18n } from "discourse-i18n";import CustomReaction from "../models/discourse-reactions-custom-reaction";import DiscourseReactionsList from "./discourse-reactions-list";import DiscourseReactionsStatePanel from "./discourse-reactions-state-panel";@@ -54,6 +55,18 @@ export default class DiscourseReactionsCounter extends Component {event.stopImmediatePropagation();}+ @action+ keyDown(event) {+ if (event.key === "Enter" || event.key === " ") {+ event.preventDefault();+ this.click(event);+ } else if (event.key === "Escape" && this.args.statePanelExpanded) {+ event.stopPropagation();+ this.args.collapseStatePanel();+ document.getElementById(this.elementId)?.focus();+ }+ }+@actionclick(event) {if (event.target.closest("[data-user-card]")) {@@ -156,6 +169,12 @@ export default class DiscourseReactionsCounter extends Component {}}+ get counterAriaLabel() {+ return i18n("discourse_reactions.counter.aria_label", {+ count: this.args.post.reaction_users_count,+ });+ }+get onlyOneMainReaction() {return (this.args.post.reactions?.length === 1 &&@@ -169,6 +188,9 @@ export default class DiscourseReactionsCounter extends Component {<divid={{this.elementId}}class={{this.classes}}+ role="button"+ tabindex="0"+ aria-label={{this.counterAriaLabel}}{{on "mousedown" this.mouseDown}}{{on "mouseup" this.mouseUp}}{{closeOnClickOutside this.clickOutside}}@@ -176,6 +198,7 @@ export default class DiscourseReactionsCounter extends Component {{{on "pointerover" this.pointerOver}}{{on "pointerout" this.pointerOut}}{{on "click" this.click}}+ {{on "keydown" this.keyDown}}>{{#if @post.reaction_users_count}}<DiscourseReactionsStatePanel@@ -195,7 +218,7 @@ export default class DiscourseReactionsCounter extends Component {/>{{/unless}}- <span class="reactions-counter">+ <span class="reactions-counter" aria-hidden="true">{{@post.reaction_users_count}}</span>@@ -203,6 +226,7 @@ export default class DiscourseReactionsCounter extends Component {<div class="discourse-reactions-reaction-button my-likes"><DButtonclass="btn-toggle-reaction-like btn-flat btn-icon no-text reaction-button"+ @translatedTitle={{this.counterAriaLabel}}@icon={{this.siteSettings.discourse_reactions_like_icon}}/></div>diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel-reaction.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel-reaction.gjsindex 3f95d8f3676..c2dbf4b952c 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel-reaction.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel-reaction.gjs@@ -36,6 +36,13 @@ export default class DiscourseReactionsStatePanelReaction extends Component {: this.firstLineUsers.length;}+ get groupAriaLabel() {+ return i18n("discourse_reactions.state_panel.reaction_group", {+ reaction: this.args.reaction.id,+ count: this.args.reaction.count,+ });+ }+get moreLabel() {if (this.args.isDisplayed && this.args.reaction.count > MAX_USERS_COUNT) {return i18n("discourse_reactions.state_panel.more_users", {@@ -51,10 +58,12 @@ export default class DiscourseReactionsStatePanelReaction extends Component {"discourse-reactions-state-panel-reaction"(if @isDisplayed "is-displayed")}}+ role="listitem"+ aria-label={{this.groupAriaLabel}}{{on "click" this.click}}>{{#if @users}}- <div class="reaction-wrapper">+ <div class="reaction-wrapper" aria-hidden="true"><div class="emoji-wrapper">{{emoji @reaction.id}}</div>@@ -76,7 +85,15 @@ export default class DiscourseReactionsStatePanelReaction extends Component {{{/each}}{{#if (gt @users.length MIN_USERS_COUNT)}}- <button type="button" class="show-users">+ <button+ type="button"+ class="show-users"+ aria-label={{if+ @isDisplayed+ (i18n "discourse_reactions.state_panel.hide_users")+ (i18n "discourse_reactions.state_panel.show_users")+ }}+ >{{icon (if @isDisplayed "chevron-up" "chevron-down")}}</button>{{/if}}diff --git a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel.gjs b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel.gjsindex 0906db39353..0a406af481e 100644--- a/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel.gjs+++ b/plugins/discourse-reactions/assets/javascripts/discourse/components/discourse-reactions-state-panel.gjs@@ -2,6 +2,8 @@ import Component from "@glimmer/component";import { tracked } from "@glimmer/tracking";import { on } from "@ember/modifier";import { action, get } from "@ember/object";+import didInsert from "@ember/render-modifiers/modifiers/did-insert";+import { schedule } from "@ember/runloop";import concatClass from "discourse/helpers/concat-class";import { and, eq } from "discourse/truth-helpers";import DiscourseReactionsStatePanelReaction from "./discourse-reactions-state-panel-reaction";@@ -66,6 +68,16 @@ export default class DiscourseReactionsStatePanel extends Component {return !!Object.keys(this.args.reactionsUsers).length;}+ @action+ focusContainer(element) {+ schedule("afterRender", () => {+ const counter = element.closest(".discourse-reactions-counter");+ if (counter?.matches(":focus-visible")) {+ element.focus();+ }+ });+ }+<template><divclass={{concatClass "discourse-reactions-state-panel" this.classes}}@@ -75,7 +87,12 @@ export default class DiscourseReactionsStatePanel extends Component {{{#if (and @statePanelExpanded @post.reactions.length)}}<div class="container">{{#if this.hasReactionData}}- <div class="counters">+ <div+ class="counters"+ role="list"+ tabindex="-1"+ {{didInsert this.focusContainer}}+ >{{#each @post.reactions key="id" as |reaction|}}<DiscourseReactionsStatePanelReaction@reaction={{reaction}}diff --git a/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scss b/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scssindex 336dd8532fa..3a059b995ec 100644--- a/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scss+++ b/plugins/discourse-reactions/assets/stylesheets/common/discourse-reactions.scss@@ -470,6 +470,12 @@ html.discourse-reactions-no-select {align-items: center;text-align: center;cursor: pointer;+ border-radius: var(--d-button-border-radius);++ &:focus-visible {+ outline: none;+ background: var(--d-post-control-background--hover);+ }.reactions-counter {font-size: $font-up-1;diff --git a/plugins/discourse-reactions/config/locales/client.en.yml b/plugins/discourse-reactions/config/locales/client.en.ymlindex 6ebca568fb5..62ed727a720 100644--- a/plugins/discourse-reactions/config/locales/client.en.yml+++ b/plugins/discourse-reactions/config/locales/client.en.yml@@ -9,12 +9,21 @@ en:reactions_title: "Reactions"state_panel:more_users: "and %{count} more..."+ show_users: "Show more users"+ hide_users: "Show fewer users"+ reaction_group:+ one: "%{reaction}: %{count} user"+ other: "%{reaction}: %{count} users"reaction:forbidden: "That reaction was created too long ago. It can no longer be modified or removed."picker:react_with: "React to this post with: %{reaction}"cant_remove_reaction: "You can no longer remove your reaction"remove_reaction: "Remove this %{reaction} reaction"+ counter:+ aria_label:+ one: "%{count} reaction"+ other: "%{count} reactions"main_reaction:add: "Like this post"remove: "Remove this like"