fix(webui): restore sidebar menu focus on Escape

This commit is contained in:
Xubin Ren committed 2026-10-04 21:52:35 +08:00
1 parent 6b24ed80ad
commit 3e3d8e3d76
3 files changed
+158 -22

No files matched your search

+3 -1
View File
@@ -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
+35 -21
View File
@@ -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<HTMLElement>, id: string) => void;
}
function SidebarActionMenuContent({
children,
portalContainer,
}: {
children: ReactNode;
portalContainer?: HTMLElement | null;
}) {
const restoreFocusOnEscape = useRef(false);
return (
<DropdownMenuContent
align="end"
className={ACTION_MENU_CONTENT_CLASS}
portalContainer={portalContainer}
onEscapeKeyDown={() => { 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}
</DropdownMenuContent>
);
}
function SidebarItemTooltip({
label,
children,
@@ -1056,11 +1082,8 @@ export const ChatList = memo(function ChatList({
>
<MoreHorizontal className="h-3.5 w-3.5" />
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
className={ACTION_MENU_CONTENT_CLASS}
<SidebarActionMenuContent
portalContainer={actionMenuPortalContainer}
onCloseAutoFocus={(event) => event.preventDefault()}
>
<DropdownMenuItem onSelect={() => onTogglePin(s.key)}>
{isPinned ? (
@@ -1113,7 +1136,7 @@ export const ChatList = memo(function ChatList({
<Trash2 className="h-4 w-4 shrink-0" />
{t("chat.delete")}
</DropdownMenuItem>
</DropdownMenuContent>
</SidebarActionMenuContent>
</DropdownMenu>
) : null}
</div>
@@ -1304,11 +1327,8 @@ function WorkbenchTabHeader({
>
<MoreHorizontal className="h-3.5 w-3.5" />
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
className={ACTION_MENU_CONTENT_CLASS}
<SidebarActionMenuContent
portalContainer={actionMenuPortalContainer}
onCloseAutoFocus={(event) => event.preventDefault()}
>
{onRequestRename ? (
<DropdownMenuItem onSelect={onRequestRename}>
@@ -1330,7 +1350,7 @@ function WorkbenchTabHeader({
<Trash2 className="h-4 w-4 shrink-0" />
{t("workbench.deleteConversations")}
</DropdownMenuItem>
</DropdownMenuContent>
</SidebarActionMenuContent>
</DropdownMenu>
</>
@@ -1499,11 +1519,8 @@ function ActivePaneRows({
>
<MoreHorizontal className="h-3.5 w-3.5" />
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
className={ACTION_MENU_CONTENT_CLASS}
<SidebarActionMenuContent
portalContainer={actionMenuPortalContainer}
onCloseAutoFocus={(event) => event.preventDefault()}
>
<DropdownMenuItem onSelect={() => onTogglePin(pane.key)}>
{isPinned ? (
@@ -1554,7 +1571,7 @@ function ActivePaneRows({
<Trash2 className="h-4 w-4 shrink-0" />
{t("chat.delete")}
</DropdownMenuItem>
</DropdownMenuContent>
</SidebarActionMenuContent>
</DropdownMenu> : null}
</div>
</li>
@@ -1796,11 +1813,8 @@ function ProjectGroupHeader({
>
<MoreHorizontal className="h-3.5 w-3.5" />
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
className={ACTION_MENU_CONTENT_CLASS}
<SidebarActionMenuContent
portalContainer={actionMenuPortalContainer}
onCloseAutoFocus={(event) => event.preventDefault()}
>
{onNewChat ? (
<DropdownMenuItem onSelect={onNewChat}>
@@ -1814,7 +1828,7 @@ function ProjectGroupHeader({
{t("chat.rename")}
</DropdownMenuItem>
) : null}
</DropdownMenuContent>
</SidebarActionMenuContent>
</DropdownMenu>
) : null}
+120
View File
@@ -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<string | null>(null);
return <>
<ChatList
sessions={[topic, {
...topic, key: "websocket:group", chatId: "group", title: "Group",
}, {
...topic, key: "websocket:project", chatId: "project", title: "Project task",
workspaceScope: { project_path: "/workspace/photos", project_name: "Photos", access_mode: "restricted" },
}]}
activeKey={topic.key}
paneGroups={{
"websocket:group": {
tabKey: "websocket:group", title: "Group", activePaneKey: "websocket:group",
panes: [
{ key: "websocket:group", chatId: "group", title: "Group" },
{ key: "websocket:research", chatId: "research", title: "Research" },
],
},
}}
onSelect={vi.fn()}
onRequestDelete={vi.fn()}
onTogglePin={vi.fn()}
onToggleArchive={vi.fn()}
onRequestRename={(_key, title) => setRename(title)}
onRequestRenameTab={(_key, title) => setRename(title)}
onRequestRenameProject={(_key, title) => setRename(title)}
/>
<button>Outside destination</button>
<RenameChatDialog open={rename !== null} title={rename ?? ""}
onCancel={() => 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(<SidebarFocusCase />);
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(<SidebarFocusCase />);
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(<SidebarFocusCase />);
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(<SidebarFocusCase />);
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();
});
});