fix(webui): restore sidebar focus after submenu Escape
This commit is contained in:
1 parent
3e3d8e3d76
commit
1ea981e417
3 files changed
+67
-17
No files matched your search
+3
-1
@@ -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
|
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
|
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
|
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.
|
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
|
Drag a topic within its current sidebar group to keep frequently used work in
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
|
createContext,
|
||||||
memo,
|
memo,
|
||||||
useCallback,
|
useCallback,
|
||||||
|
useContext,
|
||||||
useEffect,
|
useEffect,
|
||||||
useLayoutEffect,
|
useLayoutEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
@@ -87,6 +89,8 @@ interface SidebarActionMenuController {
|
|||||||
openFromContextMenu: (event: ReactMouseEvent<HTMLElement>, id: string) => void;
|
openFromContextMenu: (event: ReactMouseEvent<HTMLElement>, id: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SidebarActionMenuEscapeContext = createContext<(() => void) | undefined>(undefined);
|
||||||
|
|
||||||
function SidebarActionMenuContent({
|
function SidebarActionMenuContent({
|
||||||
children,
|
children,
|
||||||
portalContainer,
|
portalContainer,
|
||||||
@@ -95,21 +99,24 @@ function SidebarActionMenuContent({
|
|||||||
portalContainer?: HTMLElement | null;
|
portalContainer?: HTMLElement | null;
|
||||||
}) {
|
}) {
|
||||||
const restoreFocusOnEscape = useRef(false);
|
const restoreFocusOnEscape = useRef(false);
|
||||||
|
const markEscape = () => { restoreFocusOnEscape.current = true; };
|
||||||
return (
|
return (
|
||||||
<DropdownMenuContent
|
<SidebarActionMenuEscapeContext.Provider value={markEscape}>
|
||||||
align="end"
|
<DropdownMenuContent
|
||||||
className={ACTION_MENU_CONTENT_CLASS}
|
align="end"
|
||||||
portalContainer={portalContainer}
|
className={ACTION_MENU_CONTENT_CLASS}
|
||||||
onEscapeKeyDown={() => { restoreFocusOnEscape.current = true; }}
|
portalContainer={portalContainer}
|
||||||
onCloseAutoFocus={(event) => {
|
onEscapeKeyDown={markEscape}
|
||||||
// Actions can open a dialog or remove the trigger. Only Escape returns
|
onCloseAutoFocus={(event) => {
|
||||||
// to it; Radix still owns the trigger and outside-interaction handling.
|
// Actions can open a dialog or remove the trigger. Only Escape returns
|
||||||
if (!restoreFocusOnEscape.current) event.preventDefault();
|
// to it; Radix still owns the trigger and outside-interaction handling.
|
||||||
restoreFocusOnEscape.current = false;
|
if (!restoreFocusOnEscape.current) event.preventDefault();
|
||||||
}}
|
restoreFocusOnEscape.current = false;
|
||||||
>
|
}}
|
||||||
{children}
|
>
|
||||||
</DropdownMenuContent>
|
{children}
|
||||||
|
</DropdownMenuContent>
|
||||||
|
</SidebarActionMenuEscapeContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1617,6 +1624,8 @@ function MoveToGroupSubmenu({
|
|||||||
onMove: (targetKey: string) => void;
|
onMove: (targetKey: string) => void;
|
||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation();
|
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;
|
if (targets.length === 0) return null;
|
||||||
return (
|
return (
|
||||||
<DropdownMenuSub>
|
<DropdownMenuSub>
|
||||||
@@ -1624,7 +1633,7 @@ function MoveToGroupSubmenu({
|
|||||||
<MoveRight className="h-4 w-4 shrink-0" aria-hidden />
|
<MoveRight className="h-4 w-4 shrink-0" aria-hidden />
|
||||||
{t("workbench.moveTo")}
|
{t("workbench.moveTo")}
|
||||||
</DropdownMenuSubTrigger>
|
</DropdownMenuSubTrigger>
|
||||||
<DropdownMenuSubContent>
|
<DropdownMenuSubContent onEscapeKeyDown={onEscapeKeyDown}>
|
||||||
{targets.map((target) => (
|
{targets.map((target) => (
|
||||||
<DropdownMenuItem
|
<DropdownMenuItem
|
||||||
key={target.key}
|
key={target.key}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { useState } from "react";
|
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 userEvent from "@testing-library/user-event";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
@@ -26,6 +26,10 @@ function SidebarFocusCase() {
|
|||||||
}]}
|
}]}
|
||||||
activeKey={topic.key}
|
activeKey={topic.key}
|
||||||
paneGroups={{
|
paneGroups={{
|
||||||
|
[topic.key]: {
|
||||||
|
tabKey: "tab:review", title: "Review", activePaneKey: topic.key, visible: false,
|
||||||
|
panes: [{ key: topic.key, chatId: topic.chatId, title: "Review" }],
|
||||||
|
},
|
||||||
"websocket:group": {
|
"websocket:group": {
|
||||||
tabKey: "websocket:group", title: "Group", activePaneKey: "websocket:group",
|
tabKey: "websocket:group", title: "Group", activePaneKey: "websocket:group",
|
||||||
panes: [
|
panes: [
|
||||||
@@ -38,6 +42,7 @@ function SidebarFocusCase() {
|
|||||||
onRequestDelete={vi.fn()}
|
onRequestDelete={vi.fn()}
|
||||||
onTogglePin={vi.fn()}
|
onTogglePin={vi.fn()}
|
||||||
onToggleArchive={vi.fn()}
|
onToggleArchive={vi.fn()}
|
||||||
|
onAttachPane={vi.fn()}
|
||||||
onRequestRename={(_key, title) => setRename(title)}
|
onRequestRename={(_key, title) => setRename(title)}
|
||||||
onRequestRenameTab={(_key, title) => setRename(title)}
|
onRequestRenameTab={(_key, title) => setRename(title)}
|
||||||
onRequestRenameProject={(_key, title) => setRename(title)}
|
onRequestRenameProject={(_key, title) => setRename(title)}
|
||||||
@@ -105,6 +110,40 @@ describe("sidebar action focus", () => {
|
|||||||
expect(outside).toHaveFocus();
|
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 () => {
|
it("allows Select to remove the trigger without restoring focus to it", async () => {
|
||||||
const user = userEvent.setup();
|
const user = userEvent.setup();
|
||||||
render(<SidebarFocusCase />);
|
render(<SidebarFocusCase />);
|
||||||
|
|||||||
Reference in new issue
Block a user