Skip to content

Commit 0d93189

Browse files
committed
fix(web): remove the collapsed right panel from keyboard navigation
1 parent 7ff2eab commit 0d93189

4 files changed

Lines changed: 228 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
onAddDevice={() => undefined}
75+
browserAvailable={false}
76+
terminalAvailable
77+
diffAvailable={false}
78+
filesAvailable={false}
79+
pullRequestAvailable={false}
80+
pullRequestsAvailable={false}
81+
deviceAvailable={false}
82+
>
83+
<div>content</div>
84+
</RightPanelTabs>,
85+
);
86+
});
87+
await act(async () => vi.advanceTimersToNextFrame());
88+
}
89+
90+
async function openMenu() {
91+
const trigger = document.querySelector<HTMLButtonElement>('[aria-label="Add panel surface"]')!;
92+
await act(async () => {
93+
trigger.focus();
94+
trigger.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }));
95+
});
96+
await act(async () => vi.advanceTimersToNextFrame());
97+
const menu = document.querySelector<HTMLElement>('[role="menu"]')!;
98+
expect(menu).not.toBeNull();
99+
expect(container.contains(menu)).toBe(false);
100+
return menu;
101+
}
102+
103+
describe("collapsed panel portaled menu", () => {
104+
it.each(["sheet", "sidebar", "embedded"] as const)(
105+
"keeps the menu available in %s mode when open is false",
106+
async (mode) => {
107+
await renderTabs(true, mode);
108+
const menu = await openMenu();
109+
await renderTabs(false, mode);
110+
expect(document.querySelector('[role="menu"]')).toBe(menu);
111+
},
112+
);
113+
114+
it("closes the menu on collapse and does not reopen it with the panel", async () => {
115+
await renderTabs(true);
116+
await openMenu();
117+
await renderTabs(false);
118+
expect(document.querySelector('[role="menu"]')).toBeNull();
119+
await renderTabs(true);
120+
expect(document.querySelector('[role="menu"]')).toBeNull();
121+
const menu = await openMenu();
122+
await act(async () =>
123+
menu.dispatchEvent(new KeyboardEvent("keydown", { key: "t", bubbles: true })),
124+
);
125+
expect(onAddTerminal).toHaveBeenCalledOnce();
126+
});
127+
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
795795
const tabListRef = useRef<HTMLDivElement>(null);
796796
const [renamingDevice, setRenamingDevice] = useState<string | null>(null);
797797
const [addSurfaceMenuOpen, setAddSurfaceMenuOpen] = useState(false);
798+
// The menu portal sits outside the collapsed shell's inert subtree.
799+
if (props.mode === "inline" && props.open === false && addSurfaceMenuOpen) {
800+
setAddSurfaceMenuOpen(false);
801+
}
798802
const [tabScrollState, setTabScrollState] = useState({
799803
hasOverflow: false,
800804
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
@@ -108,6 +108,7 @@ function PreviewPanelShellFrame(
108108
return (
109109
<div
110110
ref={hostRef}
111+
inert={collapsible && !open}
111112
className={cn(
112113
"relative flex h-full min-h-0 min-w-0 max-w-full flex-col self-stretch bg-background",
113114
isInline

0 commit comments

Comments
 (0)