Skip to content

Commit 5088aff

Browse files
saphidclaude
andauthored
fix(web): move keyboard interaction out of the closing right panel (pingdotgg#14786)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 44a5883 commit 5088aff

3 files changed

Lines changed: 210 additions & 0 deletions

File tree

‎apps/web/src/components/chat/PanelLayoutControls.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const PanelLayoutControls = memo(function PanelLayoutControls({
113113
pressed={rightPanelOpen}
114114
onPressedChange={onToggleRightPanel}
115115
aria-label="Toggle right panel"
116+
data-right-panel-toggle
116117
variant="ghost"
117118
size="sm"
118119
disabled={!rightPanelAvailable}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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 { PanelLayoutControls } from "../chat/PanelLayoutControls";
9+
import { PreviewPanelShell, type PreviewPanelMode } from "./PreviewPanelShell";
10+
11+
let root: Root;
12+
let container: HTMLDivElement;
13+
14+
beforeEach(() => {
15+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
16+
vi.useFakeTimers({ toFake: ["requestAnimationFrame", "cancelAnimationFrame"] });
17+
vi.stubGlobal(
18+
"ResizeObserver",
19+
class {
20+
observe() {}
21+
unobserve() {}
22+
disconnect() {}
23+
},
24+
);
25+
container = document.createElement("div");
26+
document.body.append(container);
27+
root = createRoot(container);
28+
});
29+
30+
afterEach(async () => {
31+
await act(async () => root.unmount());
32+
container.remove();
33+
vi.unstubAllGlobals();
34+
vi.useRealTimers();
35+
});
36+
37+
async function renderPanel(
38+
open: boolean | undefined,
39+
mode: PreviewPanelMode = "inline",
40+
present = true,
41+
) {
42+
await act(async () => {
43+
root.render(
44+
<Dialog.Root open>
45+
<Dialog.Portal>
46+
<Dialog.Popup aria-label="Workspace" finalFocus={false}>
47+
{present ? (
48+
<PreviewPanelShell mode={mode} {...(open === undefined ? {} : { open })}>
49+
<input aria-label="Preview URL" />
50+
<button type="button">Reload preview</button>
51+
</PreviewPanelShell>
52+
) : null}
53+
<button type="button" data-testid="outside">
54+
Send message
55+
</button>
56+
<div key={present ? "root-controls" : "header-controls"}>
57+
<PanelLayoutControls
58+
showTerminalControl={false}
59+
showThreadPanelControl={false}
60+
terminalAvailable={false}
61+
terminalOpen={false}
62+
terminalShortcutLabel={null}
63+
threadPanelOpen={false}
64+
threadPanelPresentation="inline"
65+
threadPanelShortcutLabel={null}
66+
rightPanelAvailable
67+
rightPanelOpen={open ?? true}
68+
rightPanelShortcutLabel={null}
69+
onToggleTerminal={() => {}}
70+
onToggleThreadPanel={() => {}}
71+
onToggleRightPanel={() => {}}
72+
/>
73+
</div>
74+
</Dialog.Popup>
75+
</Dialog.Portal>
76+
</Dialog.Root>,
77+
);
78+
});
79+
await act(async () => vi.advanceTimersToNextFrame());
80+
}
81+
82+
async function enterFocusScope() {
83+
// Base UI's focus guard models Tab wrapping into the scope. Its production
84+
// focus manager chooses the first tabbable descendant, including inert rules
85+
// that jsdom's native focus() does not implement.
86+
const guards = document.querySelectorAll<HTMLElement>("[data-base-ui-focus-guard]");
87+
expect(guards.length).toBe(2);
88+
await act(async () => guards[1]!.focus());
89+
await act(async () => vi.advanceTimersToNextFrame());
90+
}
91+
92+
describe("right panel keyboard access", () => {
93+
it("skips collapsed controls and restores access after reopening", async () => {
94+
await renderPanel(false);
95+
const outside = document.querySelector<HTMLElement>('[data-testid="outside"]')!;
96+
expect(document.activeElement).toBe(outside);
97+
await enterFocusScope();
98+
expect(document.activeElement).toBe(outside);
99+
100+
await renderPanel(true);
101+
await enterFocusScope();
102+
expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
103+
104+
await renderPanel(false);
105+
await enterFocusScope();
106+
expect(document.activeElement).toBe(outside);
107+
});
108+
109+
it("returns focus to the right-panel toggle when a focused panel closes", async () => {
110+
await renderPanel(true);
111+
const input = document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!;
112+
await act(async () => input.focus());
113+
await renderPanel(false);
114+
expect(document.activeElement).toBe(
115+
document.querySelector('[aria-label="Toggle right panel"]'),
116+
);
117+
await renderPanel(true);
118+
await enterFocusScope();
119+
expect(document.activeElement).toBe(input);
120+
});
121+
122+
it("retains focus after the closing animation relocates the toggle", async () => {
123+
await renderPanel(true);
124+
await act(async () => {
125+
document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!.focus();
126+
});
127+
await renderPanel(false);
128+
const closingToggle = document.querySelector('[aria-label="Toggle right panel"]');
129+
expect(document.activeElement).toBe(closingToggle);
130+
await renderPanel(false, "inline", false);
131+
const settledToggle = document.querySelector('[aria-label="Toggle right panel"]');
132+
expect(settledToggle).not.toBe(closingToggle);
133+
expect(document.activeElement).toBe(settledToggle);
134+
});
135+
136+
it("returns focus when the panel closes without an animation", async () => {
137+
await renderPanel(true);
138+
await act(async () => {
139+
document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!.focus();
140+
});
141+
await renderPanel(false, "inline", false);
142+
expect(document.activeElement).toBe(
143+
document.querySelector('[aria-label="Toggle right panel"]'),
144+
);
145+
});
146+
147+
it("preserves focus moved outside while the closing animation settles", async () => {
148+
await renderPanel(true);
149+
await act(async () => {
150+
document.querySelector<HTMLInputElement>('[aria-label="Preview URL"]')!.focus();
151+
});
152+
await renderPanel(false);
153+
const outside = document.querySelector<HTMLButtonElement>('[data-testid="outside"]')!;
154+
await act(async () => outside.focus());
155+
await renderPanel(false, "inline", false);
156+
expect(document.activeElement).toBe(outside);
157+
});
158+
159+
it("preserves focus outside the panel when it closes", async () => {
160+
await renderPanel(true);
161+
const outside = document.querySelector<HTMLButtonElement>('[data-testid="outside"]')!;
162+
await act(async () => outside.focus());
163+
await renderPanel(false);
164+
expect(document.activeElement).toBe(outside);
165+
});
166+
167+
it.each(["sheet", "sidebar", "embedded"] as const)(
168+
"keeps %s controls reachable when the inline open flag is false",
169+
async (mode) => {
170+
await renderPanel(false, mode);
171+
expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
172+
},
173+
);
174+
175+
it("keeps non-collapsible inline controls reachable", async () => {
176+
await renderPanel(undefined);
177+
expect(document.activeElement).toBe(document.querySelector('[aria-label="Preview URL"]'));
178+
});
179+
});

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ function PreviewPanelShellFrame(
6868
const maximized = props.maximized ?? false;
6969
const localHostRef = useRef<HTMLDivElement | null>(null);
7070
const hostRef = props.hostRef ?? localHostRef;
71+
const returnedFocus = useRef(false);
7172
const { width, handlers } = props.inlineSize;
7273
// Derive suppression before the layout commits so the browser never creates
7374
// a width transition for resize or maximize changes.
@@ -106,6 +107,35 @@ function PreviewPanelShellFrame(
106107
window.cancelAnimationFrame(restoreFrame);
107108
};
108109
}, [suppressWidthTransition]);
110+
useLayoutEffect(() => {
111+
const host = hostRef.current;
112+
if (!host || !collapsible) return;
113+
return () => {
114+
if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return;
115+
// The toggle moves when the shell unmounts, after an animation or immediately.
116+
// Restore focus at its committed location if removal cleared focus to body.
117+
queueMicrotask(() => {
118+
if (host.ownerDocument.activeElement === host.ownerDocument.body) {
119+
host.ownerDocument.querySelector<HTMLButtonElement>("[data-right-panel-toggle]")?.focus();
120+
}
121+
});
122+
};
123+
}, [collapsible, hostRef]);
124+
useLayoutEffect(() => {
125+
const host = hostRef.current;
126+
if (!host) return;
127+
const collapsed = collapsible && !open;
128+
if (!collapsed) returnedFocus.current = false;
129+
// Move focus before making its current owner inert, while the toggle is reachable.
130+
if (collapsed && host.contains(host.ownerDocument.activeElement)) {
131+
const toggle = host.ownerDocument.querySelector<HTMLButtonElement>(
132+
"[data-right-panel-toggle]",
133+
);
134+
toggle?.focus();
135+
returnedFocus.current = toggle !== null && host.ownerDocument.activeElement === toggle;
136+
}
137+
host.toggleAttribute("inert", collapsed);
138+
}, [collapsible, open, hostRef]);
109139
return (
110140
<div
111141
ref={hostRef}

0 commit comments

Comments
 (0)