π OpenHands: AI-Driven Development
fix(settings): make the LLM profile row menu keyboard-operable (#18019)
Engel Nyst321d4927dfcf
diff --git a/__tests__/components/settings/llm-profiles/profile-actions-menu.test.tsx b/__tests__/components/settings/llm-profiles/profile-actions-menu.test.tsxindex 0aa044ca63..d2dbead294 100644--- a/__tests__/components/settings/llm-profiles/profile-actions-menu.test.tsx+++ b/__tests__/components/settings/llm-profiles/profile-actions-menu.test.tsx@@ -1,3 +1,4 @@+import { useRef, useState } from "react";import { render, screen, fireEvent } from "@testing-library/react";import { describe, expect, it, vi } from "vitest";import userEvent from "@testing-library/user-event";@@ -7,12 +8,12 @@ vi.mock("react-i18next", () => ({useTranslation: () => ({t: (key: string) => {const translations: Record<string, string> = {- "SETTINGS$PROFILE_EDIT": "Edit",- "BUTTON$RENAME": "Rename",- "BUTTON$DUPLICATE": "Duplicate",- "SETTINGS$PROFILE_SET_ACTIVE": "Set as active",- "SETTINGS$PROFILE_SET_DEFAULT": "Set as default",- "BUTTON$DELETE": "Delete",+ SETTINGS$PROFILE_EDIT: "Edit",+ BUTTON$RENAME: "Rename",+ BUTTON$DUPLICATE: "Duplicate",+ SETTINGS$PROFILE_SET_ACTIVE: "Set as active",+ SETTINGS$PROFILE_SET_DEFAULT: "Set as default",+ BUTTON$DELETE: "Delete",};return translations[key] || key;},@@ -39,7 +40,9 @@ describe("ProfileActionsMenu", () => {expect(screen.getByTestId("profile-duplicate")).toHaveTextContent("Duplicate",);- expect(screen.getByTestId("profile-set-active")).toHaveTextContent("Set as default");+ expect(screen.getByTestId("profile-set-active")).toHaveTextContent(+ "Set as default",+ );expect(screen.getByTestId("profile-delete")).toHaveTextContent("Delete");});@@ -323,5 +326,100 @@ describe("ProfileActionsMenu", () => {await user.keyboard("{Tab}");expect(handleClose).toHaveBeenCalled();});++ it("skips the disabled Set as default item in both directions", async () => {+ const user = userEvent.setup();+ render(<ProfileActionsMenu {...defaultProps} isActive />);++ await user.keyboard("{ArrowDown}{ArrowDown}"); // Rename, Duplicate+ expect(screen.getByTestId("profile-duplicate")).toHaveFocus();++ await user.keyboard("{ArrowDown}");+ expect(screen.getByTestId("profile-delete")).toHaveFocus();++ await user.keyboard("{ArrowUp}");+ expect(screen.getByTestId("profile-duplicate")).toHaveFocus();+ });+ });++ describe("keyboard navigation when anchored to a row trigger", () => {+ // Mirrors ProfileRow: the trigger toggles the menu, which is portaled to+ // <body> against the trigger and unmounted on close.+ function AnchoredMenu({ isActive = false }: { isActive?: boolean }) {+ const triggerRef = useRef<HTMLButtonElement>(null);+ const [open, setOpen] = useState(false);+ return (+ <>+ <button+ ref={triggerRef}+ type="button"+ data-testid="profile-menu-trigger"+ onClick={() => setOpen((value) => !value)}+ />+ {open && (+ <ProfileActionsMenu+ {...defaultProps}+ isActive={isActive}+ 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("profile-menu-trigger"));++ expect(screen.getByTestId("profile-edit")).toHaveFocus();+ });++ it("moves focus to Edit when opened with Enter, then arrows across the disabled item", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu isActive />);++ screen.getByTestId("profile-menu-trigger").focus();+ await user.keyboard("{Enter}");+ expect(screen.getByTestId("profile-edit")).toHaveFocus();++ await user.keyboard("{ArrowDown}");+ expect(screen.getByTestId("profile-rename")).toHaveFocus();++ await user.keyboard("{ArrowDown}{ArrowDown}");+ expect(screen.getByTestId("profile-delete")).toHaveFocus();++ await user.keyboard("{ArrowUp}");+ expect(screen.getByTestId("profile-duplicate")).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("profile-menu-trigger"));++ await user.keyboard("{Tab}");++ expect(+ screen.queryByTestId("profile-actions-menu"),+ ).not.toBeInTheDocument();+ expect(screen.getByTestId("next-page-control")).toHaveFocus();+ });++ it("closes on Escape and returns focus to the trigger", async () => {+ const user = userEvent.setup();+ render(<AnchoredMenu />);+ await user.click(screen.getByTestId("profile-menu-trigger"));++ await user.keyboard("{Escape}");++ expect(+ screen.queryByTestId("profile-actions-menu"),+ ).not.toBeInTheDocument();+ expect(screen.getByTestId("profile-menu-trigger")).toHaveFocus();+ });});});diff --git a/src/components/features/settings/llm-profiles/profile-actions-menu.tsx b/src/components/features/settings/llm-profiles/profile-actions-menu.tsxindex 8cc9c59184..285ccfaa0d 100644--- a/src/components/features/settings/llm-profiles/profile-actions-menu.tsx+++ b/src/components/features/settings/llm-profiles/profile-actions-menu.tsx@@ -34,6 +34,13 @@ interface ProfileActionsMenuProps {anchorRef?: React.RefObject<HTMLElement | null>;}+// 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),+ );+export function ProfileActionsMenu({onEdit,onRename,@@ -77,10 +84,12 @@ export function ProfileActionsMenu({};}, [anchorElement]);- // Focus first item when menu opens+ // 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) => {@@ -92,6 +101,9 @@ export function ProfileActionsMenu({const handleEscape = (event: KeyboardEvent) => {if (event.key === "Escape") {+ if (menuRef.current?.contains(document.activeElement)) {+ anchorElement?.focus();+ }onClose();}};@@ -113,21 +125,24 @@ export function ProfileActionsMenu({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;