🙌 OpenHands: AI-Driven Development
fix(a11y): name the icon-only composer and diff viewer buttons (#18033)
Engel Nyst790e4b265b14
diff --git a/__tests__/components/features/chat/components/chat-input-actions.test.tsx b/__tests__/components/features/chat/components/chat-input-actions.test.tsxindex 6e4ebeaac2..fb3b6faf44 100644--- a/__tests__/components/features/chat/components/chat-input-actions.test.tsx+++ b/__tests__/components/features/chat/components/chat-input-actions.test.tsx@@ -15,6 +15,7 @@ import {setRegisteredBackends,} from "#/api/backend-registry/active-store";import type { Backend } from "#/api/backend-registry/types";+import { I18nKey } from "#/i18n/declaration";const useActiveConversationMock = vi.fn<() => {@@ -260,6 +261,16 @@ describe("ChatInputActions", () => {).not.toBeInTheDocument();});+ it("gives the icon-only send button an accessible name", () => {+ renderWithProviders(<ChatInputActions disabled={false} />);++ expect(+ screen.getByRole("button", {+ name: I18nKey.CHAT_INTERFACE$TOOLTIP_SEND_MESSAGE,+ }),+ ).toHaveAttribute("data-testid", "submit-button");+ });+it("hides the Change Agent button on a local backend", () => {renderWithProviders(<ChatInputActions disabled={false} />);diff --git a/__tests__/components/features/chat/remove-file-button.test.tsx b/__tests__/components/features/chat/remove-file-button.test.tsxnew file mode 100644index 0000000000..4573cb19e9--- /dev/null+++ b/__tests__/components/features/chat/remove-file-button.test.tsx@@ -0,0 +1,63 @@+import { render, screen } from "@testing-library/react";+import userEvent from "@testing-library/user-event";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { UploadedFile } from "#/components/features/chat/uploaded-file";+import { UploadedImage } from "#/components/features/chat/uploaded-image";+import { I18nKey } from "#/i18n/declaration";++vi.mock("react-i18next", async (importOriginal) => ({+ ...(await importOriginal<typeof import("react-i18next")>()),+ useTranslation: () => ({+ t: (key: string, options?: { fileName?: string }) =>+ options?.fileName ? `${key} ${options.fileName}` : key,+ }),+}));++describe("attachment remove buttons", () => {+ beforeEach(() => {+ vi.stubGlobal("URL", {+ createObjectURL: (file: File) => `blob:preview/${file.name}`,+ revokeObjectURL: vi.fn(),+ });+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("names the remove button after the attached file it removes", async () => {+ const user = userEvent.setup();+ const onRemove = vi.fn();+ render(+ <UploadedFile+ file={new File(["notes"], "qa-note.txt", { type: "text/plain" })}+ onRemove={onRemove}+ />,+ );++ await user.click(+ screen.getByRole("button", {+ name: `${I18nKey.CHAT_INTERFACE$REMOVE_FILE} qa-note.txt`,+ }),+ );++ expect(onRemove).toHaveBeenCalledTimes(1);+ });++ it("names the remove button of an attached image", async () => {+ const user = userEvent.setup();+ const onRemove = vi.fn();+ render(+ <UploadedImage+ image={new File(["image"], "diagram.png", { type: "image/png" })}+ onRemove={onRemove}+ />,+ );++ await user.click(+ screen.getByRole("button", { name: I18nKey.BUTTON$REMOVE_IMAGE }),+ );++ expect(onRemove).toHaveBeenCalledTimes(1);+ });+});diff --git a/__tests__/components/features/conversation/agent-status.test.tsx b/__tests__/components/features/conversation/agent-status.test.tsxindex 0f6a57f9df..026b655f6a 100644--- a/__tests__/components/features/conversation/agent-status.test.tsx+++ b/__tests__/components/features/conversation/agent-status.test.tsx@@ -280,6 +280,7 @@ describe("AgentStatus", () => {const label = screen.getByText(I18nKey.AGENT_STATUS$RUNNING_TASK);const stopButton = screen.getByTestId("stop-button");+ expect(stopButton).toHaveAccessibleName(I18nKey.BUTTON$STOP);expect(label).toHaveAttribute("title", I18nKey.AGENT_STATUS$RUNNING_TASK);expect(label.parentElement).toHaveClass("custom-status-class");expect(stopButton.parentElement).toHaveClass("cursor-pointer");@@ -302,6 +303,7 @@ describe("AgentStatus", () => {});const playButton = screen.getByTestId("play-button");+ expect(playButton).toHaveAccessibleName(I18nKey.AGENT$RESUME_TASK);expect(playButton).toBeEnabled();expect(playButton.parentElement).toHaveClass("cursor-pointer");diff --git a/__tests__/components/features/diff-viewer/file-diff-viewer.test.tsx b/__tests__/components/features/diff-viewer/file-diff-viewer.test.tsxindex 7bdb37e28e..ef0e0617af 100644--- a/__tests__/components/features/diff-viewer/file-diff-viewer.test.tsx+++ b/__tests__/components/features/diff-viewer/file-diff-viewer.test.tsx@@ -5,6 +5,7 @@ import {FileDiffViewer,MAX_DIFF_EDITOR_HEIGHT_PX,} from "#/components/features/diff-viewer/file-diff-viewer";+import { I18nKey } from "#/i18n/declaration";const MOCK_DIFF = { original: "old content", modified: "new content" };const MOCK_MD_DIFF = {@@ -224,4 +225,37 @@ describe("FileDiffViewer", () => {"false",);});++ it("gives the icon-only view mode and collapse buttons accessible names", async () => {+ const user = userEvent.setup();+ render(<FileDiffViewer path="src/index.ts" type="M" />);++ expect(screen.getByTestId("collapse")).toHaveAccessibleName(+ I18nKey.BUTTON$EXPAND,+ );++ await expand(user);++ expect(screen.getByTestId("collapse")).toHaveAccessibleName(+ I18nKey.BUTTON$COLLAPSE,+ );+ expect(+ screen.getByRole("button", {+ name: I18nKey.DIFF_VIEWER$VIEW_MODE_OLD,+ pressed: false,+ }),+ ).toHaveAttribute("data-testid", "view-mode-old");+ expect(+ screen.getByRole("button", {+ name: I18nKey.FILES$DIFF_VIEW,+ pressed: true,+ }),+ ).toHaveAttribute("data-testid", "view-mode-diff");+ expect(+ screen.getByRole("button", {+ name: I18nKey.DIFF_VIEWER$VIEW_MODE_NEW,+ pressed: false,+ }),+ ).toHaveAttribute("data-testid", "view-mode-new");+ });});diff --git a/src/components/features/chat/chat-play-button.tsx b/src/components/features/chat/chat-play-button.tsxindex c68615c4d8..9cca3243ef 100644--- a/src/components/features/chat/chat-play-button.tsx+++ b/src/components/features/chat/chat-play-button.tsx@@ -1,4 +1,6 @@+import { useTranslation } from "react-i18next";import PlayIcon from "#/icons/play-solid.svg?react";+import { I18nKey } from "#/i18n/declaration";import { cn } from "#/utils/utils";export interface ChatResumeAgentButtonProps {@@ -10,11 +12,14 @@ export function ChatResumeAgentButton({onAgentResumed,disabled = false,}: ChatResumeAgentButtonProps) {+ const { t } = useTranslation("openhands");+return (<buttontype="button"onClick={onAgentResumed}data-testid="play-button"+ aria-label={t(I18nKey.AGENT$RESUME_TASK)}disabled={disabled}className={cn("cursor-pointer", disabled && "cursor-not-allowed")}>diff --git a/src/components/features/chat/chat-send-button.tsx b/src/components/features/chat/chat-send-button.tsxindex 5aed2a662c..c2fad8ecf7 100644--- a/src/components/features/chat/chat-send-button.tsx+++ b/src/components/features/chat/chat-send-button.tsx@@ -1,4 +1,6 @@import { ArrowUp, Loader2 } from "lucide-react";+import { useTranslation } from "react-i18next";+import { I18nKey } from "#/i18n/declaration";import { cn } from "#/utils/utils";export interface ChatSendButtonProps {@@ -14,6 +16,8 @@ export function ChatSendButton({disabled,isPending = false,}: ChatSendButtonProps) {+ const { t } = useTranslation("openhands");+return (<buttontype="button"@@ -29,6 +33,7 @@ export function ChatSendButton({onClick={handleSubmit}disabled={disabled}aria-busy={isPending}+ aria-label={t(I18nKey.CHAT_INTERFACE$TOOLTIP_SEND_MESSAGE)}>{isPending ? (<Loader2diff --git a/src/components/features/chat/chat-stop-button.tsx b/src/components/features/chat/chat-stop-button.tsxindex 7d591e4ff8..43786749f4 100644--- a/src/components/features/chat/chat-stop-button.tsx+++ b/src/components/features/chat/chat-stop-button.tsx@@ -1,15 +1,20 @@+import { useTranslation } from "react-i18next";import PauseIcon from "#/icons/pause.svg?react";+import { I18nKey } from "#/i18n/declaration";export interface ChatStopButtonProps {handleStop: () => void;}export function ChatStopButton({ handleStop }: ChatStopButtonProps) {+ const { t } = useTranslation("openhands");+return (<buttontype="button"onClick={handleStop}data-testid="stop-button"+ aria-label={t(I18nKey.BUTTON$STOP)}className="cursor-pointer"><PauseIcon className="block max-w-none w-4 h-4 text-current" />diff --git a/src/components/features/chat/remove-file-button.tsx b/src/components/features/chat/remove-file-button.tsxindex dfe7c912a0..7b524855dc 100644--- a/src/components/features/chat/remove-file-button.tsx+++ b/src/components/features/chat/remove-file-button.tsx@@ -3,15 +3,21 @@ import { cn, isMobileDevice } from "#/utils/utils";interface RemoveFileButtonProps {onClick: () => void;+ /** Names the attachment this icon-only button removes. */+ ariaLabel: string;}-export function RemoveFileButton({ onClick }: RemoveFileButtonProps) {+export function RemoveFileButton({+ onClick,+ ariaLabel,+}: RemoveFileButtonProps) {const isMobile = isMobileDevice();return (<buttontype="button"onClick={onClick}+ aria-label={ariaLabel}className={cn("z-10 flex w-4 h-4 rounded-full items-center justify-center bg-surface hover:bg-muted cursor-pointer absolute top-1 right-1 opacity-0 group-hover:opacity-100 transition-opacity duration-200",isMobile && "opacity-100",diff --git a/src/components/features/chat/uploaded-file.tsx b/src/components/features/chat/uploaded-file.tsxindex b5ce1c427b..e03cc64e50 100644--- a/src/components/features/chat/uploaded-file.tsx+++ b/src/components/features/chat/uploaded-file.tsx@@ -1,7 +1,9 @@import { LoaderCircle } from "lucide-react";+import { useTranslation } from "react-i18next";import FileIcon from "#/icons/file.svg?react";import { RemoveFileButton } from "./remove-file-button";import { cn, getFileExtension } from "#/utils/utils";+import { I18nKey } from "#/i18n/declaration";interface UploadedFileProps {file: File;@@ -14,12 +16,18 @@ export function UploadedFile({onRemove,isLoading = false,}: UploadedFileProps) {+ const { t } = useTranslation("openhands");const fileExtension = getFileExtension(file.name);return (<div className="group flex gap-2 rounded-lg bg-interactive-hover max-w-40 px-3 py-1 relative"><div className="flex flex-col justify-center gap-0.25">- <RemoveFileButton onClick={onRemove} />+ <RemoveFileButton+ onClick={onRemove}+ ariaLabel={t(I18nKey.CHAT_INTERFACE$REMOVE_FILE, {+ fileName: file.name,+ })}+ /><div className="flex items-center gap-2 w-full"><spanclassName={cn(diff --git a/src/components/features/chat/uploaded-image.tsx b/src/components/features/chat/uploaded-image.tsxindex d7547331e7..19954c5500 100644--- a/src/components/features/chat/uploaded-image.tsx+++ b/src/components/features/chat/uploaded-image.tsx@@ -1,5 +1,7 @@import React from "react";import { LoaderCircle } from "lucide-react";+import { useTranslation } from "react-i18next";+import { I18nKey } from "#/i18n/declaration";import { PastedImageUploadAsFileButton } from "./pasted-image-upload-as-file-button";import { RemoveFileButton } from "./remove-file-button";@@ -20,6 +22,7 @@ export function UploadedImage({uploadAsFileActive = false,onToggleUploadAsFile,}: UploadedImageProps) {+ const { t } = useTranslation("openhands");const [imageUrl, setImageUrl] = React.useState<string>("");React.useEffect(() => {@@ -46,7 +49,10 @@ export function UploadedImage({/>))}- <RemoveFileButton onClick={onRemove} />+ <RemoveFileButton+ onClick={onRemove}+ ariaLabel={t(I18nKey.BUTTON$REMOVE_IMAGE)}+ />{showUploadAsFileToggle && onToggleUploadAsFile && (<PastedImageUploadAsFileButtonactive={uploadAsFileActive}diff --git a/src/components/features/diff-viewer/file-diff-viewer.tsx b/src/components/features/diff-viewer/file-diff-viewer.tsxindex 3c28135051..c4805b26c5 100644--- a/src/components/features/diff-viewer/file-diff-viewer.tsx+++ b/src/components/features/diff-viewer/file-diff-viewer.tsx@@ -27,10 +27,14 @@ import { AccordionPanel } from "./accordion-panel";type ViewMode = "diff" | "old" | "new";-const VIEW_MODES: { mode: ViewMode; icon: IconType }[] = [- { mode: "old", icon: LuHistory },- { mode: "diff", icon: LuGitCompareArrows },- { mode: "new", icon: LuFileCheck },+const VIEW_MODES: { mode: ViewMode; icon: IconType; labelKey: I18nKey }[] = [+ { mode: "old", icon: LuHistory, labelKey: I18nKey.DIFF_VIEWER$VIEW_MODE_OLD },+ { mode: "diff", icon: LuGitCompareArrows, labelKey: I18nKey.FILES$DIFF_VIEW },+ {+ mode: "new",+ icon: LuFileCheck,+ labelKey: I18nKey.DIFF_VIEWER$VIEW_MODE_NEW,+ },];const SHARED_EDITOR_OPTIONS: editor_t.IEditorOptions = {@@ -366,13 +370,14 @@ export function FileDiffViewer({onClick={(e) => e.stopPropagation()}aria-hidden={!viewModeControlsVisible}>- {VIEW_MODES.map(({ mode, icon: Icon }) => (+ {VIEW_MODES.map(({ mode, icon: Icon, labelKey }) => (<buttonkey={mode}data-testid={`view-mode-${mode}`}type="button"tabIndex={viewModeControlsVisible ? 0 : -1}aria-pressed={viewMode === mode}+ aria-label={t(labelKey)}onClick={() => setViewMode(mode)}className={cn("p-1 rounded transition-colors cursor-pointer",@@ -389,6 +394,9 @@ export function FileDiffViewer({<buttondata-testid="collapse"type="button"+ aria-label={t(+ isCollapsed ? I18nKey.BUTTON$EXPAND : I18nKey.BUTTON$COLLAPSE,+ )}className="shrink-0 text-muted">{isCollapsed ? (diff --git a/src/i18n/translation.json b/src/i18n/translation.jsonindex 27894e4f5e..21b5a5f779 100644--- a/src/i18n/translation.json+++ b/src/i18n/translation.json@@ -12273,6 +12273,23 @@"uk": "Надіслати повідомлення","ca": "Envia el missatge"},+ "CHAT_INTERFACE$REMOVE_FILE": {+ "en": "Remove {{fileName}}",+ "ja": "{{fileName}} を削除",+ "zh-CN": "移除 {{fileName}}",+ "zh-TW": "移除 {{fileName}}",+ "ko-KR": "{{fileName}} 제거",+ "no": "Fjern {{fileName}}",+ "it": "Rimuovi {{fileName}}",+ "pt": "Remover {{fileName}}",+ "es": "Eliminar {{fileName}}",+ "ar": "إزالة {{fileName}}",+ "fr": "Supprimer {{fileName}}",+ "tr": "{{fileName}} öğesini kaldır",+ "de": "{{fileName}} entfernen",+ "uk": "Видалити {{fileName}}",+ "ca": "Elimina {{fileName}}"+ },"CHAT_INTERFACE$UPLOAD_IMAGES_AS_FILES": {"en": "Upload as file","ja": "ファイルとしてアップロード",@@ -22116,6 +22133,40 @@"uk": "Цей файл було видалено. Його попередній вміст більше недоступний для перегляду.","ca": "Aquest fitxer s'ha eliminat. El seu contingut anterior ja no està disponible per a la previsualització."},+ "DIFF_VIEWER$VIEW_MODE_OLD": {+ "en": "Old version",+ "ja": "旧バージョン",+ "zh-CN": "旧版本",+ "zh-TW": "舊版本",+ "ko-KR": "이전 버전",+ "no": "Gammel versjon",+ "it": "Versione precedente",+ "pt": "Versão anterior",+ "es": "Versión anterior",+ "ar": "الإصدار القديم",+ "fr": "Ancienne version",+ "tr": "Eski sürüm",+ "de": "Alte Version",+ "uk": "Стара версія",+ "ca": "Versió antiga"+ },+ "DIFF_VIEWER$VIEW_MODE_NEW": {+ "en": "New version",+ "ja": "新バージョン",+ "zh-CN": "新版本",+ "zh-TW": "新版本",+ "ko-KR": "새 버전",+ "no": "Ny versjon",+ "it": "Nuova versione",+ "pt": "Nova versão",+ "es": "Versión nueva",+ "ar": "الإصدار الجديد",+ "fr": "Nouvelle version",+ "tr": "Yeni sürüm",+ "de": "Neue Version",+ "uk": "Нова версія",+ "ca": "Versió nova"+ },"DIFF_VIEWER$WAITING_FOR_RUNTIME": {"en": "Waiting for runtime to start...","ja": "ランタイムの起動を待っています...",