fix(webui): restore sidebar focus after submenu Escape

This commit is contained in:
Xubin Ren committed 2026-10-04 22:09:56 +08:00
1 parent 3e3d8e3d76
commit 1ea981e417
3 files changed
+67 -17

No files matched your search

+3 -1
View File
@@ -115,7 +115,9 @@ 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. Press Escape in an action menu to return focus to its
button and continue with Tab. Choosing Rename instead moves focus into the
button and continue with Tab, including from the **Move to** submenu. ArrowLeft
leaves the submenu and returns to **Move to** without closing the parent menu.
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
+24 -15
View File
@@ -1,6 +1,8 @@
import {
createContext,
memo,
useCallback,
useContext,
useEffect,
useLayoutEffect,
useMemo,
@@ -87,6 +89,8 @@ interface SidebarActionMenuController {
openFromContextMenu: (event: ReactMouseEvent<HTMLElement>, id: string) => void;
}
const SidebarActionMenuEscapeContext = createContext<(() => void) | undefined>(undefined);
function SidebarActionMenuContent({
children,
portalContainer,
@@ -95,21 +99,24 @@ function SidebarActionMenuContent({
portalContainer?: HTMLElement | null;
}) {
const restoreFocusOnEscape = useRef(false);
const markEscape = () => { restoreFocusOnEscape.current = true; };
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>
<SidebarActionMenuEscapeContext.Provider value={markEscape}>
<DropdownMenuContent
align="end"
className={ACTION_MENU_CONTENT_CLASS}
portalContainer={portalContainer}
onEscapeKeyDown={markEscape}
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>
</SidebarActionMenuEscapeContext.Provider>
);
}
@@ -1617,6 +1624,8 @@ function MoveToGroupSubmenu({
onMove: (targetKey: string) => void;
}) {
const { t } = useTranslation();
// Radix sends Escape only to the topmost submenu before closing the root.
const onEscapeKeyDown = useContext(SidebarActionMenuEscapeContext);
if (targets.length === 0) return null;
return (
<DropdownMenuSub>
@@ -1624,7 +1633,7 @@ function MoveToGroupSubmenu({
<MoveRight className="h-4 w-4 shrink-0" aria-hidden />
{t("workbench.moveTo")}
</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
<DropdownMenuSubContent onEscapeKeyDown={onEscapeKeyDown}>
{targets.map((target) => (
<DropdownMenuItem
key={target.key}
+40 -1
View File
@@ -1,5 +1,5 @@
import { useState } from "react";
import { render, screen, waitFor } from "@testing-library/react";
import { act, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -26,6 +26,10 @@ function SidebarFocusCase() {
}]}
activeKey={topic.key}
paneGroups={{
[topic.key]: {
tabKey: "tab:review", title: "Review", activePaneKey: topic.key, visible: false,
panes: [{ key: topic.key, chatId: topic.chatId, title: "Review" }],
},
"websocket:group": {
tabKey: "websocket:group", title: "Group", activePaneKey: "websocket:group",
panes: [
@@ -38,6 +42,7 @@ function SidebarFocusCase() {
onRequestDelete={vi.fn()}
onTogglePin={vi.fn()}
onToggleArchive={vi.fn()}
onAttachPane={vi.fn()}
onRequestRename={(_key, title) => setRename(title)}
onRequestRenameTab={(_key, title) => setRename(title)}
onRequestRenameProject={(_key, title) => setRename(title)}
@@ -105,6 +110,40 @@ describe("sidebar action focus", () => {
expect(outside).toHaveFocus();
});
it("returns Escape from Move to to the root action button", async () => {
const user = userEvent.setup();
render(<SidebarFocusCase />);
const trigger = screen.getByRole("button", { name: "Topic actions for Review" });
trigger.focus();
await user.keyboard("{Enter}");
const move = await screen.findByRole("menuitem", { name: "Move to", exact: true });
act(() => move.focus());
await user.keyboard("{ArrowRight}");
await screen.findByRole("menuitem", { name: "Group · 2/4" });
await user.keyboard("{Escape}");
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
await waitFor(() => expect(trigger).toHaveFocus());
});
it("keeps ArrowLeft within the parent menu, then hands Rename its input focus", async () => {
const user = userEvent.setup();
render(<SidebarFocusCase />);
const trigger = screen.getByRole("button", { name: "Topic actions for Review" });
trigger.focus();
await user.keyboard("{Enter}");
const move = await screen.findByRole("menuitem", { name: "Move to", exact: true });
act(() => move.focus());
await user.keyboard("{ArrowRight}");
await screen.findByRole("menuitem", { name: "Group · 2/4" });
await user.keyboard("{ArrowLeft}");
await waitFor(() => expect(move).toHaveFocus());
expect(screen.queryByRole("menuitem", { name: "Group · 2/4" })).not.toBeInTheDocument();
await user.click(screen.getByRole("menuitem", { name: "Rename", exact: true }));
const input = await screen.findByPlaceholderText("Topic name");
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(input).toHaveFocus();
});
it("allows Select to remove the trigger without restoring focus to it", async () => {
const user = userEvent.setup();
render(<SidebarFocusCase />);