Skip to content

Commit 48fbf33

Browse files
committed
fix(web): remove the collapsed right panel from keyboard navigation
1 parent 54084ae commit 48fbf33

4 files changed

Lines changed: 231 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// @vitest-environment jsdom
2+
3+
import { act } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6+
7+
import { RightPanelTabs } from "./RightPanelTabs";
8+
import type { PreviewPanelMode } from "./preview/PreviewPanelShell";
9+
10+
let root: Root;
11+
let container: HTMLDivElement;
12+
const originalGetAnimations = Object.getOwnPropertyDescriptor(Element.prototype, "getAnimations");
13+
14+
beforeEach(() => {
15+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
16+
Object.defineProperty(Element.prototype, "getAnimations", {
17+
configurable: true,
18+
value: () => [],
19+
});
20+
vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
21+
vi.stubGlobal(
22+
"ResizeObserver",
23+
class {
24+
observe() {}
25+
unobserve() {}
26+
disconnect() {}
27+
},
28+
);
29+
container = document.createElement("div");
30+
document.body.append(container);
31+
root = createRoot(container);
32+
});
33+
34+
afterEach(async () => {
35+
await act(async () => root.unmount());
36+
container.remove();
37+
if (originalGetAnimations) {
38+
Object.defineProperty(Element.prototype, "getAnimations", originalGetAnimations);
39+
} else {
40+
Reflect.deleteProperty(Element.prototype, "getAnimations");
41+
}
42+
vi.unstubAllGlobals();
43+
vi.useRealTimers();
44+
});
45+
46+
const onAddTerminal = vi.fn();
47+
48+
async function renderTabs(open: boolean, mode: PreviewPanelMode = "inline") {
49+
await act(async () => {
50+
root.render(
51+
<RightPanelTabs
52+
mode={mode}
53+
open={open}
54+
surfaces={[{ id: "diff", kind: "diff" }]}
55+
environmentId={null}
56+
activeSurfaceId="diff"
57+
pendingSurfaceIds={new Set()}
58+
previewSessions={{}}
59+
desktopByTabId={{}}
60+
terminalLabelsById={new Map()}
61+
onActivate={() => undefined}
62+
onCloseSurface={() => undefined}
63+
onCloseOtherSurfaces={() => undefined}
64+
onCloseSurfacesToRight={() => undefined}
65+
onCloseAllSurfaces={() => undefined}
66+
onCopyFilePath={() => undefined}
67+
onAddBrowser={() => undefined}
68+
onAddBrowserInProfile={() => undefined}
69+
onAddTerminal={onAddTerminal}
70+
onAddPullRequest={() => undefined}
71+
onAddPullRequests={() => undefined}
72+
onAddDiff={() => undefined}
73+
onAddFiles={() => undefined}
74+
onAddAgents={() => undefined}
75+
onAddDevice={() => undefined}
76+
liveAgentCount={0}
77+
browserAvailable={false}
78+
terminalAvailable
79+
diffAvailable={false}
80+
filesAvailable={false}
81+
pullRequestAvailable={false}
82+
pullRequestsAvailable={false}
83+
agentsAvailable={false}
84+
deviceAvailable={false}
85+
>
86+
<div>content</div>
87+
</RightPanelTabs>,
88+
);
89+
});
90+
await act(async () => vi.advanceTimersToNextFrame());
91+
}
92+
93+
async function openMenu() {
94+
const trigger = document.querySelector<HTMLButtonElement>('[aria-label="Add panel surface"]')!;
95+
await act(async () => {
96+
trigger.focus();
97+
trigger.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));
98+
});
99+
await act(async () => vi.advanceTimersToNextFrame());
100+
const menu = document.querySelector<HTMLElement>('[role="menu"]')!;
101+
expect(menu).not.toBeNull();
102+
expect(container.contains(menu)).toBe(false);
103+
return menu;
104+
}
105+
106+
describe("collapsed panel portaled menu", () => {
107+
it.each(["sheet", "sidebar", "embedded"] as const)(
108+
"keeps the menu available in %s mode when open is false",
109+
async (mode) => {
110+
await renderTabs(true, mode);
111+
const menu = await openMenu();
112+
await renderTabs(false, mode);
113+
expect(document.querySelector('[role="menu"]')).toBe(menu);
114+
},
115+
);
116+
117+
it("closes the menu on collapse and does not reopen it with the panel", async () => {
118+
await renderTabs(true);
119+
await openMenu();
120+
await renderTabs(false);
121+
expect(document.querySelector('[role="menu"]')).toBeNull();
122+
await renderTabs(true);
123+
expect(document.querySelector('[role="menu"]')).toBeNull();
124+
const menu = await openMenu();
125+
await act(async () =>
126+
menu.dispatchEvent(new KeyboardEvent("keydown", { key: "t", bubbles: true })),
127+
);
128+
expect(onAddTerminal).toHaveBeenCalledOnce();
129+
});
130+
});

‎apps/web/src/components/RightPanelTabs.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,10 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
831831
const tabListRef = useRef<HTMLDivElement>(null);
832832
const [renamingDevice, setRenamingDevice] = useState<string | null>(null);
833833
const [addSurfaceMenuOpen, setAddSurfaceMenuOpen] = useState(false);
834+
// The menu portal sits outside the collapsed shell's inert subtree.
835+
if (props.mode === "inline" && props.open === false && addSurfaceMenuOpen) {
836+
setAddSurfaceMenuOpen(false);
837+
}
834838
const [tabScrollState, setTabScrollState] = useState({
835839
hasOverflow: false,
836840
canScrollLeft: false,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// @vitest-environment jsdom
2+
3+
import { Dialog } from "@base-ui/react/dialog";
4+
import { act } from "react";
5+
import { createRoot, type Root } from "react-dom/client";
6+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
7+
8+
import { PreviewPanelShell, type PreviewPanelMode } from "./PreviewPanelShell";
9+
10+
let root: Root;
11+
let container: HTMLDivElement;
12+
13+
beforeEach(() => {
14+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
15+
vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
16+
vi.stubGlobal(
17+
"ResizeObserver",
18+
class {
19+
observe() {}
20+
unobserve() {}
21+
disconnect() {}
22+
},
23+
);
24+
container = document.createElement("div");
25+
document.body.append(container);
26+
root = createRoot(container);
27+
});
28+
29+
afterEach(async () => {
30+
await act(async () => root.unmount());
31+
container.remove();
32+
vi.unstubAllGlobals();
33+
vi.useRealTimers();
34+
});
35+
36+
async function renderPanel(open: boolean | undefined, mode: PreviewPanelMode = "inline") {
37+
await act(async () => {
38+
root.render(
39+
<Dialog.Root open>
40+
<Dialog.Portal>
41+
<Dialog.Popup aria-label="Workspace" finalFocus={false}>
42+
<PreviewPanelShell mode={mode} {...(open === undefined ? {} : { open })}>
43+
<input aria-label="Preview URL" />
44+
<button type="button">Reload preview</button>
45+
</PreviewPanelShell>
46+
<button type="button" data-testid="outside">
47+
Send message
48+
</button>
49+
</Dialog.Popup>
50+
</Dialog.Portal>
51+
</Dialog.Root>,
52+
);
53+
});
54+
await act(async () => vi.advanceTimersToNextFrame());
55+
}
56+
57+
async function enterFocusScope() {
58+
// Base UI's focus guard models Tab wrapping into the scope. Its production
59+
// focus manager chooses the first tabbable descendant, including inert rules
60+
// that jsdom's native focus() does not implement.
61+
const guards = document.querySelectorAll<HTMLElement>("[data-base-ui-focus-guard]");
62+
expect(guards.length).toBe(2);
63+
await act(async () => guards[1]!.focus());
64+
await act(async () => vi.advanceTimersToNextFrame());
65+
}
66+
67+
describe("right panel keyboard access", () => {
68+
it("skips collapsed controls and restores access after reopening", async () => {
69+
await renderPanel(false);
70+
const outside = document.querySelector<HTMLElement>('[data-testid="outside"]')!;
71+
expect(document.activeElement).toBe(outside);
72+
await enterFocusScope();
73+
expect(document.activeElement).toBe(outside);
74+
75+
await renderPanel(true);
76+
await enterFocusScope();
77+
expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
78+
79+
await renderPanel(false);
80+
await enterFocusScope();
81+
expect(document.activeElement).toBe(outside);
82+
});
83+
84+
it.each(["sheet", "sidebar", "embedded"] as const)(
85+
"keeps %s controls reachable when the inline open flag is false",
86+
async (mode) => {
87+
await renderPanel(false, mode);
88+
expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
89+
},
90+
);
91+
92+
it("keeps non-collapsible inline controls reachable", async () => {
93+
await renderPanel(undefined);
94+
expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
95+
});
96+
});

‎apps/web/src/components/preview/PreviewPanelShell.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export function PreviewPanelShell(props: {
118118
return (
119119
<div
120120
ref={hostRef}
121+
inert={collapsible && !open}
121122
className={cn(
122123
"relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
123124
isInline

0 commit comments

Comments
 (0)