Skip to content

Commit de4c284

Browse files
fix(web): reopening a hidden drawer keeps its terminal focus request
The collapsed drawer clips its viewport rather than hiding it, so the viewport's wrapper took focus while the hidden surface refused it, and the request counted as done. Showing the drawer then left focus on a wrapper that ignores typing. A hidden viewport now declines focus, so the request waits for it to show. A viewport's auto-focus goes through the same request, so a first focus refused for want of the operate grant retries when the grant arrives. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4250c66 commit de4c284

3 files changed

Lines changed: 94 additions & 11 deletions

File tree

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ import { openTerminalLinkInPreview } from "./preview/openTerminalLinkInPreview";
8989
import { useAtomCommand } from "../state/use-atom-command";
9090
import { preventTerminalCloseShortcut } from "../lib/terminalCloseShortcut";
9191
import { observeResize } from "../lib/observeResize";
92-
import { consumePendingTerminalFocus, registerTerminalFocusTarget } from "../lib/terminalFocus";
92+
import {
93+
consumePendingTerminalFocus,
94+
focusTerminal,
95+
focusTerminalViewport,
96+
registerTerminalFocusTarget,
97+
} from "../lib/terminalFocus";
9398
import {
9499
resolveTerminalFontPreference,
95100
resolveTerminalFontSizePreference,
@@ -1119,19 +1124,19 @@ function TerminalSessionViewport({
11191124

11201125
const attachContainer = (mount: HTMLDivElement) => {
11211126
containerRef.current = mount;
1122-
// Focus lands on the mount until the surface exists or is shown; the
1123-
// surface takes it over from there.
1124-
const focus = () => {
1125-
if (!readEnvironmentScope(environmentId, AuthTerminalOperateScope)) return false;
1126-
mount.focus({ preventScroll: true });
1127-
terminalRef.current?.focus();
1128-
return mount.contains(document.activeElement);
1129-
};
1127+
const focus = () =>
1128+
focusTerminalViewport({
1129+
mount,
1130+
surface: terminalRef.current,
1131+
canOperate: readEnvironmentScope(environmentId, AuthTerminalOperateScope),
1132+
visible: visibleRef.current,
1133+
});
11301134
// Once per viewport: a re-attached ref (a fresh callback identity) must not
1131-
// pull focus back on every render of a chatty terminal.
1135+
// pull focus back on every render of a chatty terminal. As a request, it
1136+
// waits for the grant or for the viewport to show when it cannot focus yet.
11321137
if (autoFocus && !autoFocusedRef.current) {
11331138
autoFocusedRef.current = true;
1134-
focus();
1139+
focusTerminal(threadRef, terminalId);
11351140
}
11361141
const unregister = registerTerminalFocusTarget({
11371142
threadRef,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import {
6+
consumePendingTerminalFocus,
7+
focusTerminal,
8+
focusTerminalViewport,
9+
registerTerminalFocusTarget,
10+
} from "./terminalFocus";
11+
12+
// The drawer's collapsed shape: a 0fr grid row that clips the viewport, which
13+
// stays focusable, around a surface whose input refuses focus while hidden.
14+
function mountClippedViewport() {
15+
const drawer = document.createElement("div");
16+
drawer.style.cssText = "display:grid;grid-template-rows:0fr;overflow:clip";
17+
const clip = document.createElement("div");
18+
clip.style.cssText = "min-height:0;overflow:clip";
19+
const mount = document.createElement("div");
20+
mount.tabIndex = -1;
21+
const input = document.createElement("textarea");
22+
mount.append(input);
23+
clip.append(mount);
24+
drawer.append(clip);
25+
document.body.append(drawer);
26+
const state = { visible: false };
27+
const surface = {
28+
focus() {
29+
if (state.visible) input.focus({ preventScroll: true });
30+
},
31+
};
32+
const focus = () =>
33+
focusTerminalViewport({ mount, surface, canOperate: true, visible: state.visible });
34+
return {
35+
input,
36+
state,
37+
focus,
38+
[Symbol.dispose]: () => drawer.remove(),
39+
};
40+
}
41+
42+
describe("focusTerminalViewport", () => {
43+
it("keeps a request made while the drawer is hidden until it shows", () => {
44+
const ref = scopeThreadRef(EnvironmentId.make("env"), ThreadId.make("hidden-drawer"));
45+
using viewport = mountClippedViewport();
46+
const unregister = registerTerminalFocusTarget({
47+
threadRef: ref,
48+
terminalId: "a",
49+
focus: viewport.focus,
50+
});
51+
using _unregister = { [Symbol.dispose]: unregister };
52+
53+
focusTerminal(ref, "a");
54+
viewport.state.visible = true;
55+
consumePendingTerminalFocus(ref, "a");
56+
57+
expect(document.activeElement).toBe(viewport.input);
58+
});
59+
});

‎apps/web/src/lib/terminalFocus.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,22 @@ export function registerTerminalFocusTarget(target: TerminalFocusTarget): () =>
161161
focusTargets.delete(target);
162162
};
163163
}
164+
165+
/**
166+
* A viewport's `focus` for its target. Focus lands on the mount until the
167+
* surface exists, and the surface takes it over from there.
168+
*/
169+
export function focusTerminalViewport(input: {
170+
readonly mount: HTMLElement;
171+
readonly surface: { focus(): void } | null;
172+
readonly canOperate: boolean;
173+
readonly visible: boolean;
174+
}): boolean {
175+
// A hidden drawer is clipped, not display:none: the mount would take focus
176+
// while the surface refuses it, and the request would be spent on a wrapper
177+
// that ignores typing.
178+
if (!input.canOperate || !input.visible) return false;
179+
input.mount.focus({ preventScroll: true });
180+
input.surface?.focus();
181+
return input.mount.contains(document.activeElement);
182+
}

0 commit comments

Comments
 (0)