Skip to content

Commit dd1a084

Browse files
saphidclaude
andcommitted
revert(web): keep the closing shell's focus rule to its own descendants
This reverts d386ef1. Treating body focus at close as panel-owned also moved focus to the toggle after a user had clicked the timeline, which breaks typing into the composer, and the toggle unmounts before the shell cleanup in ChatView's real order. Focus on the add-surface trigger or menu, or on a toggle that relocates, is left as it is on main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d386ef1 commit dd1a084

2 files changed

Lines changed: 2 additions & 55 deletions

File tree

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

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

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

@@ -49,16 +48,6 @@ async function renderPanel(
4948
<PreviewPanelShell mode={mode} {...(open === undefined ? {} : { open })}>
5049
<input aria-label="Preview URL" />
5150
<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}
6251
</PreviewPanelShell>
6352
) : null}
6453
<button type="button" data-testid="outside">
@@ -155,41 +144,6 @@ describe("right panel keyboard access", () => {
155144
);
156145
});
157146

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-
193147
it("preserves focus moved outside while the closing animation settles", async () => {
194148
await renderPanel(true);
195149
await act(async () => {

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ 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);
8079
const { width, handlers } = props.inlineSize;
8180
// Derive suppression before the layout commits so the browser never creates
8281
// a width transition for resize or maximize changes.
@@ -119,9 +118,7 @@ function PreviewPanelShellFrame(
119118
const host = hostRef.current;
120119
if (!host || !collapsible) return;
121120
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;
121+
if (!host.contains(host.ownerDocument.activeElement) && !returnedFocus.current) return;
125122
// The toggle moves when the shell unmounts, after an animation or immediately.
126123
// Restore focus at its committed location if removal cleared focus to body.
127124
queueMicrotask(() => {
@@ -135,13 +132,9 @@ function PreviewPanelShellFrame(
135132
const host = hostRef.current;
136133
if (!host) return;
137134
const collapsed = collapsible && !open;
138-
const closing = collapsed && wasOpen.current;
139-
wasOpen.current = !collapsed;
140135
if (!collapsed) returnedFocus.current = false;
141-
const active = host.ownerDocument.activeElement;
142136
// Move focus before making its current owner inert, while the toggle is reachable.
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))) {
137+
if (collapsed && host.contains(host.ownerDocument.activeElement)) {
145138
const toggle = host.ownerDocument.querySelector<HTMLButtonElement>(
146139
"[data-right-panel-toggle]",
147140
);

0 commit comments

Comments
 (0)