Skip to content

Commit 3f8cef4

Browse files
fix(web): leave + menu focus as on main; keep this change to Files search
Moving focus into the panel when a surface is picked from + ran before an async surface (Browser) activated, so it focused the previous surface, such as a running terminal. That behavior is outside the Cmd+F fix; drop it. AI-assisted; verified with the tests and tools listed above.
1 parent 4669989 commit 3f8cef4

4 files changed

Lines changed: 57 additions & 148 deletions

File tree

Lines changed: 43 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import "~/index.css";
22

3-
import { type ComponentProps, useState } from "react";
43
import { describe, expect, it, vi } from "vite-plus/test";
5-
import { page, userEvent } from "vite-plus/test/browser";
64

75
import type { RightPanelSurface } from "~/rightPanelStore";
86
import { render, stubGlobal } from "../../test/render";
9-
import { startWorkspaceFocus, workspaceAreaProps } from "~/lib/workspaceFocus";
107
import { HistorySwipeNavigation } from "./HistorySwipeNavigation";
118
import { RightPanelTabs } from "./RightPanelTabs";
129

@@ -62,55 +59,6 @@ const surfaces: RightPanelSurface[] = Array.from({ length: 16 }, (_, index) => (
6259
activeTerminalId: `${index}`,
6360
}));
6461

65-
function Tabs(props: Partial<ComponentProps<typeof RightPanelTabs>>) {
66-
const noop = () => {};
67-
return (
68-
<RightPanelTabs
69-
mode="embedded"
70-
keybindings={[]}
71-
getShortcutContext={() => ({
72-
terminalFocus: false,
73-
terminalOpen: false,
74-
previewFocus: false,
75-
previewOpen: false,
76-
isWeb: true,
77-
isDesktop: false,
78-
})}
79-
surfaces={surfaces}
80-
environmentId={null}
81-
activeSurfaceId={null}
82-
pendingSurfaceIds={new Set()}
83-
previewSessions={{}}
84-
desktopByTabId={{}}
85-
terminalLabelsById={new Map(surfaces.map((surface) => [surface.id, "Terminal"]))}
86-
onActivate={noop}
87-
onCloseSurface={noop}
88-
onCloseOtherSurfaces={noop}
89-
onCloseSurfacesToRight={noop}
90-
onCloseAllSurfaces={noop}
91-
onCopyFilePath={noop}
92-
onAddBrowser={noop}
93-
onAddBrowserInProfile={noop}
94-
onAddTerminal={noop}
95-
onAddDiff={noop}
96-
onAddFiles={noop}
97-
onAddPullRequest={noop}
98-
onAddPullRequests={noop}
99-
onAddDevice={noop}
100-
browserAvailable={false}
101-
terminalAvailable
102-
diffAvailable={false}
103-
filesAvailable={false}
104-
pullRequestAvailable={false}
105-
pullRequestsAvailable={false}
106-
deviceAvailable={false}
107-
{...props}
108-
>
109-
{props.children ?? null}
110-
</RightPanelTabs>
111-
);
112-
}
113-
11462
describe("RightPanelTabs", () => {
11563
it("a flick that scrolls the tab strip to its left edge keeps the thread, a swipe from the edge goes back", async () => {
11664
navigateHistory.mockReset();
@@ -119,10 +67,52 @@ describe("RightPanelTabs", () => {
11967
using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("MacIntel");
12068
const panel = document.createElement("div");
12169
panel.style.cssText = "width: 320px; height: 200px; display: flex";
70+
const noop = () => {};
12271
await using screen = await render(
12372
<>
12473
<HistorySwipeNavigation />
125-
<Tabs />
74+
<RightPanelTabs
75+
mode="embedded"
76+
keybindings={[]}
77+
getShortcutContext={() => ({
78+
terminalFocus: false,
79+
terminalOpen: false,
80+
previewFocus: false,
81+
previewOpen: false,
82+
isWeb: true,
83+
isDesktop: false,
84+
})}
85+
surfaces={surfaces}
86+
environmentId={null}
87+
activeSurfaceId={null}
88+
pendingSurfaceIds={new Set()}
89+
previewSessions={{}}
90+
desktopByTabId={{}}
91+
terminalLabelsById={new Map(surfaces.map((surface) => [surface.id, "Terminal"]))}
92+
onActivate={noop}
93+
onCloseSurface={noop}
94+
onCloseOtherSurfaces={noop}
95+
onCloseSurfacesToRight={noop}
96+
onCloseAllSurfaces={noop}
97+
onCopyFilePath={noop}
98+
onAddBrowser={noop}
99+
onAddBrowserInProfile={noop}
100+
onAddTerminal={noop}
101+
onAddDiff={noop}
102+
onAddFiles={noop}
103+
onAddPullRequest={noop}
104+
onAddPullRequests={noop}
105+
onAddDevice={noop}
106+
browserAvailable={false}
107+
terminalAvailable
108+
diffAvailable={false}
109+
filesAvailable={false}
110+
pullRequestAvailable={false}
111+
pullRequestsAvailable={false}
112+
deviceAvailable={false}
113+
>
114+
{null}
115+
</RightPanelTabs>
126116
</>,
127117
{ container: panel },
128118
);
@@ -143,48 +133,4 @@ describe("RightPanelTabs", () => {
143133

144134
expect(navigateHistory).toHaveBeenCalledExactlyOnceWith("back");
145135
});
146-
it("a surface picked from the add menu takes focus, even when already open; dismissing the menu returns to +", async () => {
147-
using _focus = {
148-
[Symbol.dispose]: startWorkspaceFocus({ subscribe: () => () => {} } as never),
149-
};
150-
function Panel() {
151-
const [activeSurfaceId, setActiveSurfaceId] = useState("terminal:0");
152-
return (
153-
<>
154-
<div {...workspaceAreaProps("composer", 2, "Composer")}>
155-
<textarea aria-label="Message" />
156-
</div>
157-
<Tabs
158-
activeSurfaceId={activeSurfaceId}
159-
filesAvailable
160-
onAddFiles={() => setActiveSurfaceId("terminal:1")}
161-
>
162-
<input
163-
key={activeSurfaceId}
164-
aria-label={`Content of ${activeSurfaceId}`}
165-
data-workspace-focus-default
166-
/>
167-
</Tabs>
168-
</>
169-
);
170-
}
171-
await using _screen = await render(<Panel />);
172-
const add = page.getByRole("button", { name: "Add panel surface" });
173-
const content = page.getByRole("textbox", { name: "Content of terminal:1" });
174-
175-
for (let pick = 0; pick < 2; pick++) {
176-
await userEvent.click(add);
177-
await vi.waitFor(() =>
178-
expect(document.activeElement?.closest('[role="menu"]')).not.toBeNull(),
179-
);
180-
await userEvent.keyboard("f");
181-
await expect.element(page.getByRole("menu")).not.toBeInTheDocument();
182-
await expect.element(content).toHaveFocus();
183-
}
184-
185-
await userEvent.click(add);
186-
await userEvent.keyboard("{Escape}");
187-
await expect.element(page.getByRole("menu")).not.toBeInTheDocument();
188-
await expect.element(add).toHaveFocus();
189-
});
190136
});

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ import { resolvePullRequestState } from "./pullRequest/pullRequestPresentation";
104104
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
105105
import { observeResize } from "~/lib/observeResize";
106106
import { surfaceShortcutKey } from "~/lib/surfaceShortcutKey";
107-
import { requestWorkspaceFocus } from "~/lib/workspaceFocus";
108107

109108
export interface RightPanelTabsProps {
110109
mode: PreviewPanelMode;
@@ -1036,16 +1035,6 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
10361035
const addSurfaceTriggerRef = useRef<HTMLButtonElement>(null);
10371036
const [renamingDevice, setRenamingDevice] = useState<string | null>(null);
10381037
const [addSurfaceMenuOpen, setAddSurfaceMenuOpen] = useState(false);
1039-
const [surfacePicked, setSurfacePicked] = useState(false);
1040-
const openAddSurfaceMenu = (open: boolean) => {
1041-
if (open) setSurfacePicked(false);
1042-
setAddSurfaceMenuOpen(open);
1043-
};
1044-
const pickSurface = (open: () => void) => () => {
1045-
setSurfacePicked(true);
1046-
open();
1047-
requestWorkspaceFocus({ area: "panel" });
1048-
};
10491038
const [tabScrollState, setTabScrollState] = useState({
10501039
hasOverflow: false,
10511040
canScrollLeft: false,
@@ -1067,7 +1056,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
10671056
event.stopPropagation();
10681057
if (!event.repeat) {
10691058
addSurfaceTriggerRef.current?.focus();
1070-
openAddSurfaceMenu(true);
1059+
setAddSurfaceMenuOpen(true);
10711060
}
10721061
});
10731062
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes a document keydown listener for the new surface shortcut while the panel is open
@@ -1112,55 +1101,55 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
11121101
shortcut: "B",
11131102
available: props.browserAvailable,
11141103
disabledReason: SURFACE_DISABLED_REASONS.browser,
1115-
onClick: pickSurface(props.onAddBrowser),
1104+
onClick: props.onAddBrowser,
11161105
},
11171106
{
11181107
label: "Terminal",
11191108
icon: TerminalSquare,
11201109
shortcut: "T",
11211110
available: props.terminalAvailable,
11221111
disabledReason: SURFACE_DISABLED_REASONS.terminal,
1123-
onClick: pickSurface(props.onAddTerminal),
1112+
onClick: props.onAddTerminal,
11241113
},
11251114
{
11261115
label: "Files",
11271116
icon: Files,
11281117
shortcut: "F",
11291118
available: props.filesAvailable,
11301119
disabledReason: SURFACE_DISABLED_REASONS.files,
1131-
onClick: pickSurface(props.onAddFiles),
1120+
onClick: props.onAddFiles,
11321121
},
11331122
{
11341123
label: "Diff",
11351124
icon: FileDiff,
11361125
shortcut: "D",
11371126
available: props.diffAvailable,
11381127
disabledReason: SURFACE_DISABLED_REASONS.diff,
1139-
onClick: pickSurface(props.onAddDiff),
1128+
onClick: props.onAddDiff,
11401129
},
11411130
{
11421131
label: "Pull request",
11431132
icon: PullRequestGlyph.pullRequest,
11441133
shortcut: "P",
11451134
available: props.pullRequestAvailable,
11461135
disabledReason: SURFACE_DISABLED_REASONS.pullRequest,
1147-
onClick: pickSurface(props.onAddPullRequest),
1136+
onClick: props.onAddPullRequest,
11481137
},
11491138
{
11501139
label: "Linked pull requests",
11511140
icon: PullRequestGlyph.link,
11521141
shortcut: "L",
11531142
available: props.pullRequestsAvailable,
11541143
disabledReason: SURFACE_DISABLED_REASONS.pullRequests,
1155-
onClick: pickSurface(props.onAddPullRequests),
1144+
onClick: props.onAddPullRequests,
11561145
},
11571146
{
11581147
label: "Device",
11591148
icon: Smartphone,
11601149
shortcut: "M",
11611150
available: props.deviceAvailable,
11621151
disabledReason: SURFACE_DISABLED_REASONS.device,
1163-
onClick: pickSurface(props.onAddDevice),
1152+
onClick: props.onAddDevice,
11641153
},
11651154
...(props.onAddSideChat
11661155
? [
@@ -1170,7 +1159,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
11701159
shortcut: "S",
11711160
available: props.sideChatAvailable === true && !props.sideChatDisabledReason,
11721161
disabledReason: props.sideChatDisabledReason ?? SURFACE_DISABLED_REASONS.sideChat,
1173-
onClick: pickSurface(props.onAddSideChat),
1162+
onClick: props.onAddSideChat,
11741163
},
11751164
]
11761165
: []),
@@ -1407,7 +1396,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
14071396
})}
14081397
</SortableTabList>
14091398
{props.open !== false ? (
1410-
<Menu open={addSurfaceMenuOpen} onOpenChange={openAddSurfaceMenu}>
1399+
<Menu open={addSurfaceMenuOpen} onOpenChange={setAddSurfaceMenuOpen}>
14111400
<MenuTrigger
14121401
ref={addSurfaceTriggerRef}
14131402
render={
@@ -1425,7 +1414,6 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
14251414
align="start"
14261415
side="bottom"
14271416
sideOffset={6}
1428-
finalFocus={!surfacePicked}
14291417
onKeyDownCapture={handleAddSurfaceMenuKeyDown}
14301418
>
14311419
{addSurfaceActions.map((action) => {
@@ -1456,7 +1444,7 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
14561444
{browserProfiles.map((profile) => (
14571445
<MenuItem
14581446
key={profile.id}
1459-
onClick={pickSurface(() => props.onAddBrowserInProfile(profile.id))}
1447+
onClick={() => props.onAddBrowserInProfile(profile.id)}
14601448
>
14611449
<span className="min-w-0 truncate">{profile.name}</span>
14621450
</MenuItem>

‎apps/web/src/lib/workspaceFocus.browser.test.ts‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,6 @@ describe("workspace area focus", () => {
259259
expect(document.activeElement).toBe(clickedRow);
260260
});
261261

262-
it("restores a panel tab inside its content, not to the tab strip", () => {
263-
using fixture = setup();
264-
const panel = fixture.area("panel", 3);
265-
const content = document.createElement("div");
266-
content.setAttribute("data-right-panel-surface-content", "");
267-
const search = document.createElement("input");
268-
search.setAttribute("data-workspace-focus-default", "");
269-
content.append(search);
270-
panel.element.append(content);
271-
panel.element.dataset.workspaceFocusKey = "panel:files";
272-
panel.button.focus();
273-
expect(fixture.controller.focus("panel")).toBe(true);
274-
expect(document.activeElement).toBe(search);
275-
});
276-
277262
it("focuses a side chat's message input when the panel opens on it", () => {
278263
using fixture = setup();
279264
const panel = fixture.area("panel", 3);

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ export interface WorkspaceFocusTarget {
2525
restore?: boolean;
2626
}
2727

28-
function containsComposed(container: Element, element: Element): boolean {
29-
if (container.contains(element)) return true;
30-
const root = element.getRootNode();
31-
return root instanceof ShadowRoot && containsComposed(container, root.host);
32-
}
33-
3428
function workspaceArea(element: Element): HTMLElement | null {
3529
const area = element.closest<HTMLElement>(AREA_SELECTOR);
3630
if (area) return area;
@@ -141,15 +135,10 @@ export function createWorkspaceFocusController(doc: Document) {
141135

142136
const focusArea = (area: HTMLElement, restore = true, nested = true) => {
143137
const key = area.dataset.workspaceFocusKey ?? area.dataset.workspaceArea!;
144-
const surface = area.querySelector<HTMLElement>("[data-right-panel-surface-content]");
145138
const previous = restore ? lastFocused.get(key) : undefined;
146139
const usable = (element: HTMLElement) =>
147140
isWorkspaceElementVisible(element) && !element.matches(":disabled");
148141
const belongs = (element: HTMLElement) => workspaceArea(element) === area && usable(element);
149-
// A panel tab restores focus inside its content, never to the tab strip or "+" that the
150-
// shortcut or menu passed through on the way.
151-
const restorable = (element: HTMLElement) =>
152-
belongs(element) && (!surface || containsComposed(surface, element));
153142
// A side chat's message input sits in its own area inside the panel. Opening the
154143
// panel focuses it, but F6 visits that area on its own, so it moves past it here.
155144
const preferred = Array.from(
@@ -166,7 +155,7 @@ export function createWorkspaceFocusController(doc: Document) {
166155
previous && !previous.isConnected
167156
? Array.from(area.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)).find(
168157
(element) =>
169-
restorable(element) &&
158+
belongs(element) &&
170159
element.tagName === previous.tagName &&
171160
((previous.id && element.id === previous.id) ||
172161
(previous.getAttribute("aria-label") &&
@@ -176,6 +165,7 @@ export function createWorkspaceFocusController(doc: Document) {
176165
element.textContent === previous.textContent)),
177166
)
178167
: undefined;
168+
const surface = area.querySelector<HTMLElement>("[data-right-panel-surface-content]");
179169
const surfaceControl = surface
180170
? Array.from(surface.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)).find(
181171
(element) => belongs(element) && element.tabIndex >= 0,
@@ -185,7 +175,7 @@ export function createWorkspaceFocusController(doc: Document) {
185175
// so the conversation reuses a control only while it is still in view.
186176
const isConversation = area.dataset.workspaceArea === "conversation";
187177
const targets = new Set([
188-
previous && restorable(previous) && (!isConversation || isInScrollView(previous))
178+
previous && belongs(previous) && (!isConversation || isInScrollView(previous))
189179
? previous
190180
: undefined,
191181
isConversation ? undefined : restored,

0 commit comments

Comments
 (0)