Skip to content

Commit d386ef1

Browse files
saphidclaude
andcommitted
fix(web): keep keyboard focus when the close removes or moves its control
Main removes the add-surface trigger and menu as soon as the panel closes, and the right-panel toggle relocates when the shell unmounts. Both dropped focus to the page body. Closing now hands body focus to the toggle, and the unmount cleanup also follows a toggle that was focused when it moved. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1ae51a5 commit d386ef1

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Dialog } from "@base-ui/react/dialog";
44
import { act } from "react";
5+
import { createPortal } from "react-dom";
56
import { createRoot, type Root } from "react-dom/client";
67
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
78

@@ -48,6 +49,16 @@ async function renderPanel(
4849
<PreviewPanelShell mode={mode} {...(open === undefined ? {} : { open })}>
4950
<input aria-label="Preview URL" />
5051
<button type="button">Reload preview</button>
52+
{/* Like the add-surface trigger, removed as soon as the panel closes. */}
53+
{open !== false ? (
54+
<button type="button" aria-label="Add panel surface">
55+
+
56+
</button>
57+
) : null}
58+
{/* Like the add-surface menu, portaled outside the panel. */}
59+
{open !== false
60+
? createPortal(<button type="button">Add terminal</button>, document.body)
61+
: null}
5162
</PreviewPanelShell>
5263
) : null}
5364
<button type="button" data-testid="outside">
@@ -144,6 +155,41 @@ describe("right panel keyboard access", () => {
144155
);
145156
});
146157

158+
it.each([
159+
[
160+
"control",
161+
() => document.querySelector<HTMLButtonElement>('[aria-label="Add panel surface"]'),
162+
],
163+
[
164+
"portaled menu item",
165+
() => [...document.querySelectorAll("button")].find((b) => b.textContent === "Add terminal"),
166+
],
167+
])("returns focus when the close removes a focused %s before the animation", async (_, find) => {
168+
await renderPanel(true);
169+
await act(async () => find()!.focus());
170+
expect(document.activeElement).toBe(find());
171+
await renderPanel(false);
172+
expect(document.activeElement).toBe(
173+
document.querySelector('[aria-label="Toggle right panel"]'),
174+
);
175+
});
176+
177+
it.each([
178+
["after the closing animation", true],
179+
["without an animation", false],
180+
])("keeps focus on a toggle that closed the panel and relocates %s", async (_, animated) => {
181+
await renderPanel(true);
182+
const closingToggle = document.querySelector<HTMLButtonElement>(
183+
'[aria-label="Toggle right panel"]',
184+
)!;
185+
await act(async () => closingToggle.focus());
186+
if (animated) await renderPanel(false);
187+
await renderPanel(false, "inline", false);
188+
const settledToggle = document.querySelector('[aria-label="Toggle right panel"]');
189+
expect(settledToggle).not.toBe(closingToggle);
190+
expect(document.activeElement).toBe(settledToggle);
191+
});
192+
147193
it("preserves focus moved outside while the closing animation settles", async () => {
148194
await renderPanel(true);
149195
await act(async () => {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function PreviewPanelShellFrame(
7676
const localHostRef = useRef<HTMLDivElement | null>(null);
7777
const hostRef = props.hostRef ?? localHostRef;
7878
const returnedFocus = useRef(false);
79+
const wasOpen = useRef(open);
7980
const { width, handlers } = props.inlineSize;
8081
// Derive suppression before the layout commits so the browser never creates
8182
// a width transition for resize or maximize changes.
@@ -118,7 +119,9 @@ function PreviewPanelShellFrame(
118119
const host = hostRef.current;
119120
if (!host || !collapsible) return;
120121
return () => {
121-
if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return;
122+
const active = host.ownerDocument.activeElement;
123+
const toggleFocused = active?.matches("[data-right-panel-toggle]") ?? false;
124+
if (!host.contains(active) && !returnedFocus.current && !toggleFocused) return;
122125
// The toggle moves when the shell unmounts, after an animation or immediately.
123126
// Restore focus at its committed location if removal cleared focus to body.
124127
queueMicrotask(() => {
@@ -132,9 +135,13 @@ function PreviewPanelShellFrame(
132135
const host = hostRef.current;
133136
if (!host) return;
134137
const collapsed = collapsible && !open;
138+
const closing = collapsed && wasOpen.current;
139+
wasOpen.current = !collapsed;
135140
if (!collapsed) returnedFocus.current = false;
141+
const active = host.ownerDocument.activeElement;
136142
// Move focus before making its current owner inert, while the toggle is reachable.
137-
if (collapsed && host.contains(host.ownerDocument.activeElement)) {
143+
// Focus already on body was in a control this close removed, like the add-surface trigger.
144+
if (collapsed && (host.contains(active) || (closing && active === host.ownerDocument.body))) {
138145
const toggle = host.ownerDocument.querySelector<HTMLButtonElement>(
139146
"[data-right-panel-toggle]",
140147
);

0 commit comments

Comments
 (0)