π OpenHands: AI-Driven Development
fix(a11y): make the agent profile row menu keyboard-operable (#18112)
fengyue-xve724c5a310fc5
diff --git a/__tests__/components/settings/agent-profiles/agent-profile-actions-menu.test.tsx b/__tests__/components/settings/agent-profiles/agent-profile-actions-menu.test.tsxnew file mode 100644index 0000000000..657a3f3796--- /dev/null+++ b/__tests__/components/settings/agent-profiles/agent-profile-actions-menu.test.tsx@@ -0,0 +1,164 @@+import { useRef, useState } from "react";+import { render, screen } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import userEvent from "@testing-library/user-event";+import { AgentProfileActionsMenu } from "#/components/features/settings/agent-profiles/agent-profile-actions-menu";++const defaultProps = {+ onEdit: vi.fn(),+ onSetActive: vi.fn(),+ onDelete: vi.fn(),+ isActive: false,+ isActivating: false,+ onClose: vi.fn(),+};++describe("AgentProfileActionsMenu", () => {+ it("renders Edit, Set as active, and Delete", () => {+ render(<AgentProfileActionsMenu {...defaultProps} />);++ const items = screen.getAllByRole("menuitem");+ expect(items.map((item) => item.getAttribute("data-testid"))).toEqual([+ "agent-profile-edit",+ "agent-profile-set-active",+ "agent-profile-delete",+ ]);+ });++ it("disables Set as active when already active", () => {+ render(<AgentProfileActionsMenu {...defaultProps} isActive />);++ expect(screen.getByTestId("agent-profile-set-active")).toBeDisabled();+ });++ describe("keyboard navigation when anchored to a row trigger", () => {+ // Mirrors AgentProfileRow: the trigger toggles the menu, which is portaled+ // to <body> against the trigger and unmounted on close.+ function AnchoredMenu({+ isActive = false,+ isActivating = false,+ onEdit = defaultProps.onEdit,+ }: {+ isActive?: boolean;+ isActivating?: boolean;+ onEdit?: () => void;+ }) {+ const triggerRef = useRef<HTMLButtonElement>(null);+ const [open, setOpen] = useState(false);+ return (+ <>+ <button+ ref={triggerRef}+ type="button"+ data-testid="agent-profile-menu-trigger"+ onClick={() => setOpen((value) => !value)}+ />+ {open && (+ <AgentProfileActionsMenu+ {...defaultProps}+ onEdit={onEdit}+ isActive={isActive}+ isActivating={isActivating}+ anchorRef={triggerRef}+ onClose={() => setOpen(false)}+ />+ )}+ <button type="button" data-testid="next-page-control" />+ </>+ );+ }++ it("moves focus to Edit when opened with the mouse", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu />);++ await user.click(screen.getByTestId("agent-profile-menu-trigger"));++ expect(screen.getByTestId("agent-profile-edit")).toHaveFocus();+ });++ it("moves focus to Edit when opened with Enter on the trigger", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu />);++ screen.getByTestId("agent-profile-menu-trigger").focus();+ await user.keyboard("{Enter}");++ expect(screen.getByTestId("agent-profile-edit")).toHaveFocus();+ });++ it("arrows across the disabled Set as active item on the active row", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu isActive />);++ await user.click(screen.getByTestId("agent-profile-menu-trigger"));+ expect(screen.getByTestId("agent-profile-edit")).toHaveFocus();++ await user.keyboard("{ArrowDown}");+ expect(screen.getByTestId("agent-profile-delete")).toHaveFocus();++ await user.keyboard("{ArrowDown}");+ expect(screen.getByTestId("agent-profile-edit")).toHaveFocus();++ await user.keyboard("{ArrowUp}");+ expect(screen.getByTestId("agent-profile-delete")).toHaveFocus();+ });++ it("cycles through all three items with wrap on a non-active row", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu />);++ await user.click(screen.getByTestId("agent-profile-menu-trigger"));+ expect(screen.getByTestId("agent-profile-edit")).toHaveFocus();++ await user.keyboard("{ArrowDown}");+ expect(screen.getByTestId("agent-profile-set-active")).toHaveFocus();++ await user.keyboard("{ArrowDown}");+ expect(screen.getByTestId("agent-profile-delete")).toHaveFocus();++ await user.keyboard("{ArrowDown}");+ expect(screen.getByTestId("agent-profile-edit")).toHaveFocus();++ await user.keyboard("{ArrowUp}");+ expect(screen.getByTestId("agent-profile-delete")).toHaveFocus();+ });++ it("closes on Escape and returns focus to the trigger", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu />);+ await user.click(screen.getByTestId("agent-profile-menu-trigger"));++ await user.keyboard("{Escape}");++ expect(+ screen.queryByTestId("agent-profile-actions-menu"),+ ).not.toBeInTheDocument();+ expect(screen.getByTestId("agent-profile-menu-trigger")).toHaveFocus();+ });++ it("closes on Tab and moves focus to the control after the trigger", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu />);+ await user.click(screen.getByTestId("agent-profile-menu-trigger"));++ await user.keyboard("{Tab}");++ expect(+ screen.queryByTestId("agent-profile-actions-menu"),+ ).not.toBeInTheDocument();+ expect(screen.getByTestId("next-page-control")).toHaveFocus();+ });++ it("runs the focused action when Enter is pressed", async () => {+ const onEdit = vi.fn();+ const user = userEvent.setup();+ render(<AnchoredMenu onEdit={onEdit} />);+ await user.click(screen.getByTestId("agent-profile-menu-trigger"));++ await user.keyboard("{Enter}");++ expect(onEdit).toHaveBeenCalledTimes(1);+ });+ });+});diff --git a/src/components/features/settings/agent-profiles/agent-profile-actions-menu.tsx b/src/components/features/settings/agent-profiles/agent-profile-actions-menu.tsxindex 113a2e1cee..beca508dd8 100644--- a/src/components/features/settings/agent-profiles/agent-profile-actions-menu.tsx+++ b/src/components/features/settings/agent-profiles/agent-profile-actions-menu.tsx@@ -15,6 +15,13 @@ import CheckCircleIcon from "#/icons/u-check-circle.svg?react";import DeleteIcon from "#/icons/u-delete.svg?react";import { MenuItem } from "#/components/features/settings/llm-profiles/profile-actions-menu-item";+// A natively disabled button ignores focus(), so keyboard navigation has to+// skip it rather than stop on it.+const getEnabledItems = (items: (HTMLButtonElement | null)[]) =>+ items.filter((item): item is HTMLButtonElement =>+ Boolean(item && !item.disabled),+ );+interface AgentProfileActionsMenuProps {onEdit: () => void;onSetActive: () => void;@@ -71,9 +78,12 @@ export function AgentProfileActionsMenu({};}, [anchorElement]);+ // The anchored menu renders nothing until its position is measured, so wait+ // for the items to exist before focusing the first enabled one.+ const isMenuRendered = !anchorElement || portalStyle !== undefined;useEffect(() => {- menuItemsRef.current[0]?.focus();- }, []);+ if (isMenuRendered) getEnabledItems(menuItemsRef.current)[0]?.focus();+ }, [isMenuRendered]);useEffect(() => {const handleClickOutside = (event: MouseEvent) => {@@ -83,7 +93,12 @@ export function AgentProfileActionsMenu({onClose();};const handleEscape = (event: KeyboardEvent) => {- if (event.key === "Escape") onClose();+ if (event.key === "Escape") {+ if (menuRef.current?.contains(document.activeElement)) {+ anchorElement?.focus();+ }+ onClose();+ }};document.addEventListener("mousedown", handleClickOutside);document.addEventListener("keydown", handleEscape);@@ -101,21 +116,24 @@ export function AgentProfileActionsMenu({const handleKeyDown = useCallback((e: React.KeyboardEvent, currentIndex: number) => {if (e.key === "Tab") {+ // The anchored menu is portaled to the end of <body>; move focus back+ // to its trigger so Tab continues from the row, not the page end.+ anchorElement?.focus();onClose();return;}- const itemCount = menuItemsRef.current.filter(Boolean).length;- if (e.key === "ArrowDown") {- e.preventDefault();- const nextIndex = (currentIndex + 1) % itemCount;- menuItemsRef.current[nextIndex]?.focus();- } else if (e.key === "ArrowUp") {- e.preventDefault();- const prevIndex = (currentIndex - 1 + itemCount) % itemCount;- menuItemsRef.current[prevIndex]?.focus();- }+ if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;+ e.preventDefault();+ const enabledItems = getEnabledItems(menuItemsRef.current);+ const position = enabledItems.findIndex(+ (item) => item === menuItemsRef.current[currentIndex],+ );+ const step = e.key === "ArrowDown" ? 1 : -1;+ const target =+ (position + step + enabledItems.length) % enabledItems.length;+ enabledItems[target]?.focus();},- [onClose],+ [anchorElement, onClose],);const setActiveDisabled = isActive || isActivating;