Skip to content

Commit 461df46

Browse files
dbolserDan Bolsermaria-rcks
authored
fix(web): file context menu actions respond to pointer clicks (#26579)
Co-authored-by: Dan Bolser <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 9c30a1a commit 461df46

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎apps/web/src/components/files/FileBrowserPanel.browser.test.tsx‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { EnvironmentId, type ProjectEntry } from "@t3tools/contracts";
22
import { describe, expect, it, vi } from "vite-plus/test";
3+
import { page, userEvent } from "vite-plus/test/browser";
34

45
import { COMPOSER_MENTION_DRAG_TYPE } from "~/components/chat/composerMentionDrag";
6+
import { dismissContextMenu } from "~/contextMenuFallback";
57
import { render } from "../../../test/render";
68
import FileBrowserPanel from "./FileBrowserPanel";
79

@@ -43,6 +45,43 @@ function findTreeRow(path: string): HTMLElement | null {
4345
}
4446

4547
describe("FileBrowserPanel", () => {
48+
it("copies with the pointer and returns to the row after keyboard dismissal", async () => {
49+
await using _screen = await render(
50+
<FileBrowserPanel
51+
environmentId={EnvironmentId.make("env-files")}
52+
cwd="/repo"
53+
projectName="repo"
54+
selectedPath={null}
55+
selectedPathRevealId={0}
56+
onOpenFile={() => {}}
57+
workspaceMutationId={null}
58+
/>,
59+
);
60+
using _menu = { [Symbol.dispose]: () => dismissContextMenu() };
61+
using clipboard = vi.spyOn(navigator.clipboard, "writeText").mockResolvedValue(undefined);
62+
await vi.waitFor(() => expect(findTreeRow("README.md")).not.toBeNull());
63+
findTreeRow("README.md")!.dispatchEvent(
64+
new MouseEvent("contextmenu", {
65+
bubbles: true,
66+
cancelable: true,
67+
composed: true,
68+
button: 2,
69+
}),
70+
);
71+
const action = page.getByRole("menuitem", { name: "Copy mention", exact: true });
72+
await expect.element(action).toBeVisible();
73+
await userEvent.click(action);
74+
await vi.waitFor(() => expect(clipboard).toHaveBeenCalledWith("[README.md](README.md)"));
75+
76+
const row = findTreeRow("README.md")!;
77+
await userEvent.keyboard("{Tab}");
78+
row.focus();
79+
await userEvent.keyboard("{Shift>}{F10}{/Shift}");
80+
await expect.element(action).toHaveFocus();
81+
await userEvent.keyboard("{Escape}");
82+
await vi.waitFor(() => expect((row.getRootNode() as ShadowRoot).activeElement).toBe(row));
83+
});
84+
4685
it("dragging a file row carries a composer mention without opening it, and dragend releases it", async () => {
4786
const onOpenFile = vi.fn();
4887
await using _screen = await render(

‎apps/web/src/components/files/FileBrowserPanel.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ export default function FileBrowserPanel({
192192
: { x: anchorRect.left, y: anchorRect.bottom };
193193
const fileTarget = { environmentId, filePath: relativePath, workspaceRoot: cwd };
194194
const fileMenuItems = fileContextMenu.buildItems(fileTarget);
195+
// The app menu owns focus; the tree's outside-click handler would dismiss it before click.
196+
context.close({ restoreFocus: !pointerIsFresh });
195197
void settlePromise(async () => {
196198
const clicked = await api.contextMenu.show(
197199
[
@@ -246,7 +248,6 @@ export default function FileBrowserPanel({
246248
}
247249
}
248250
}).then((result) => {
249-
context.close();
250251
if (!AsyncResult.isSuccess(result)) throw Cause.squash(result.cause);
251252
});
252253
},

‎apps/web/src/contextMenuFallback.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,11 @@ export function showContextMenuFallback<T extends string>(
245245
): Promise<T | null> {
246246
dismissContextMenu();
247247
return new Promise<T | null>((resolve) => {
248-
const previouslyFocusedElement =
249-
document.activeElement instanceof HTMLElement ? document.activeElement : null;
248+
let focusedElement = document.activeElement;
249+
while (focusedElement?.shadowRoot?.activeElement) {
250+
focusedElement = focusedElement.shadowRoot.activeElement;
251+
}
252+
const previouslyFocusedElement = focusedElement instanceof HTMLElement ? focusedElement : null;
250253
const menuStack: HTMLDivElement[] = [];
251254
const submenuTriggerStack: Array<HTMLButtonElement | undefined> = [];
252255
let isDisposed = false;

0 commit comments

Comments
 (0)