From 3e3d8e3d769566d3f9d7f333b8b6ea2995980c1f Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Sun, 4 Oct 2026 21:48:30 +0800 Subject: [PATCH] fix(webui): restore sidebar menu focus on Escape --- docs/webui.md | 4 +- webui/src/components/ChatList.tsx | 56 +++++++---- webui/src/tests/chat-list-focus.test.tsx | 120 +++++++++++++++++++++++ 3 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 webui/src/tests/chat-list-focus.test.tsx diff --git a/docs/webui.md b/docs/webui.md index cc78c439d..8ccf02bc1 100644 --- a/docs/webui.md +++ b/docs/webui.md @@ -114,7 +114,9 @@ without changing the original thread. On touch devices, sidebar action buttons stay visible with larger touch areas for topics, conversation groups, panes, and projects. Tap a title to select it or the adjacent action button for its menu. Desktop actions still appear on -hover or keyboard focus. +hover or keyboard focus. Press Escape in an action menu to return focus to its +button and continue with Tab. Choosing Rename instead moves focus into the +dialog; clicking outside a menu keeps focus at the clicked destination. Drag a topic within its current sidebar group to keep frequently used work in your preferred order. Drag a topic from the sidebar into the composer when you diff --git a/webui/src/components/ChatList.tsx b/webui/src/components/ChatList.tsx index 77b0a6a56..0c7a5e2ab 100644 --- a/webui/src/components/ChatList.tsx +++ b/webui/src/components/ChatList.tsx @@ -7,7 +7,7 @@ import { useRef, useState, } from "react"; -import type { CSSProperties, MouseEvent as ReactMouseEvent, ReactElement } from "react"; +import type { CSSProperties, MouseEvent as ReactMouseEvent, ReactElement, ReactNode } from "react"; import { Archive, ArchiveRestore, @@ -87,6 +87,32 @@ interface SidebarActionMenuController { openFromContextMenu: (event: ReactMouseEvent, id: string) => void; } +function SidebarActionMenuContent({ + children, + portalContainer, +}: { + children: ReactNode; + portalContainer?: HTMLElement | null; +}) { + const restoreFocusOnEscape = useRef(false); + return ( + { restoreFocusOnEscape.current = true; }} + onCloseAutoFocus={(event) => { + // Actions can open a dialog or remove the trigger. Only Escape returns + // to it; Radix still owns the trigger and outside-interaction handling. + if (!restoreFocusOnEscape.current) event.preventDefault(); + restoreFocusOnEscape.current = false; + }} + > + {children} + + ); +} + function SidebarItemTooltip({ label, children, @@ -1056,11 +1082,8 @@ export const ChatList = memo(function ChatList({ > - event.preventDefault()} > onTogglePin(s.key)}> {isPinned ? ( @@ -1113,7 +1136,7 @@ export const ChatList = memo(function ChatList({ {t("chat.delete")} - + ) : null} @@ -1304,11 +1327,8 @@ function WorkbenchTabHeader({ > - event.preventDefault()} > {onRequestRename ? ( @@ -1330,7 +1350,7 @@ function WorkbenchTabHeader({ {t("workbench.deleteConversations")} - + @@ -1499,11 +1519,8 @@ function ActivePaneRows({ > - event.preventDefault()} > onTogglePin(pane.key)}> {isPinned ? ( @@ -1554,7 +1571,7 @@ function ActivePaneRows({ {t("chat.delete")} - + : null} @@ -1796,11 +1813,8 @@ function ProjectGroupHeader({ > - event.preventDefault()} > {onNewChat ? ( @@ -1814,7 +1828,7 @@ function ProjectGroupHeader({ {t("chat.rename")} ) : null} - + ) : null} diff --git a/webui/src/tests/chat-list-focus.test.tsx b/webui/src/tests/chat-list-focus.test.tsx new file mode 100644 index 000000000..ee3a8eb91 --- /dev/null +++ b/webui/src/tests/chat-list-focus.test.tsx @@ -0,0 +1,120 @@ +import { useState } from "react"; +import { render, screen, waitFor } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { ChatList } from "@/components/ChatList"; +import { RenameChatDialog } from "@/components/RenameChatDialog"; +import type { ChatSummary } from "@/lib/types"; +import { mockBrowserFocus } from "./browser-focus"; + +const topic: ChatSummary = { + key: "websocket:review", chatId: "review", channel: "websocket", + title: "Review", preview: "", createdAt: "2026-10-04T10:00:00Z", + updatedAt: "2026-10-04T10:00:00Z", +}; + +function SidebarFocusCase() { + const [rename, setRename] = useState(null); + return <> + setRename(title)} + onRequestRenameTab={(_key, title) => setRename(title)} + onRequestRenameProject={(_key, title) => setRename(title)} + /> + + setRename(null)} onConfirm={() => setRename(null)} /> + ; +} + +describe("sidebar action focus", () => { + let restoreBrowserFocus: () => void; + beforeEach(() => { restoreBrowserFocus = mockBrowserFocus(); }); + afterEach(() => { + restoreBrowserFocus(); + localStorage.removeItem("nanobot-webui.collapsed-pane-groups.v1"); + }); + + it.each([ + "Topic actions for Review", + "Topic actions for Group", + "Research pane actions", + "Topic actions for Photos", + ])("returns Escape focus to %s and allows continued tabbing", async (name) => { + const user = userEvent.setup(); + render(); + const trigger = screen.getByRole("button", { name, exact: true }); + trigger.focus(); + await user.keyboard("{Enter}"); + await screen.findByRole("menu"); + await user.keyboard("{Escape}"); + await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); + await waitFor(() => expect(trigger).toHaveFocus()); + await user.tab(); + expect(trigger).not.toHaveFocus(); + expect(document.body).not.toHaveFocus(); + }); + + it("does not reuse Escape focus restoration when the next action opens Rename", async () => { + const user = userEvent.setup(); + render(); + const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); + trigger.focus(); + await user.keyboard("{Enter}{Escape}"); + await waitFor(() => expect(trigger).toHaveFocus()); + await user.keyboard("{Enter}"); + await user.click(await screen.findByRole("menuitem", { name: "Rename", exact: true })); + const input = await screen.findByPlaceholderText("Topic name"); + await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); + await waitFor(() => expect(input).toHaveFocus()); + await user.keyboard("Updated"); + expect(input).toHaveValue("ReviewUpdated"); + }); + + it("keeps an outside click focused on its destination", async () => { + const user = userEvent.setup(); + render(); + const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); + trigger.focus(); + await user.keyboard("{Enter}"); + await screen.findByRole("menu"); + const outside = screen.getByRole("button", { name: "Outside destination" }); + await user.click(outside); + await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument()); + expect(outside).toHaveFocus(); + }); + + it("allows Select to remove the trigger without restoring focus to it", async () => { + const user = userEvent.setup(); + render(); + const trigger = screen.getByRole("button", { name: "Topic actions for Review" }); + trigger.focus(); + await user.keyboard("{Enter}"); + await user.click(await screen.findByRole("menuitem", { name: "Select", exact: true })); + await waitFor(() => expect(trigger).not.toBeInTheDocument()); + expect(trigger).not.toHaveFocus(); + await user.keyboard("{Escape}"); + expect(screen.getByRole("button", { name: "Topic actions for Review" })).toBeInTheDocument(); + }); +});