@devries-cameron20 / t3code Mirror · read-only / commits / 310baf7c99
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

fix(web): closing Settings or Usage with Esc returns focus to the opener (#38731)

Julius Marminge committed Nov 18, 2026 at 12:53 UTC 310baf7c99c9d5c4ac65aec63247bf450ca36544
3 files changed +47 -2
apps/web/src/commandPaletteStore.ts
+7
@@ -48,6 +48,13 @@ export function getCommandPaletteReturnFocus(): HTMLElement | boolean {
48 return element?.isConnected && !element.matches(":disabled") ? element : true;
49 }
50
51 +/** The element the palette opened from, while focus is still inside the palette. */
52 +export function getCommandPaletteOpener(): HTMLElement | null {
53 + const active = document.activeElement;
54 + if (!returnFocus || !active?.closest("[data-command-palette]")) return null;
55 + return returnFocus.element;
56 +}
57 +
58 // The palette is only mounted on app-shell routes (not /pair, sign-in or
59 // landings), so its shortcuts must stay inert elsewhere.
60 let mountedPalettes = 0;
apps/web/src/components/AppRouteModal.browser.test.tsx
+21
@@ -27,6 +27,27 @@ describe("AppRouteModal", () => {
27 expect(onCommit).toHaveBeenCalledWith("Keep titles short");
28 });
29
30 + it("returns focus to the opener when Esc closes it", async () => {
31 + const opener = <button type="button">Open usage</button>;
32 + await using screen = await render(opener, { router: "/" });
33 + const openerButton = screen.getByRole("button", { name: "Open usage" });
34 + (openerButton.element() as HTMLElement).focus();
35 +
36 + await screen.rerender(
37 + <>
38 + {opener}
39 + <AppRouteModal label="Usage">
40 + <p>Usage</p>
41 + </AppRouteModal>
42 + </>,
43 + );
44 + await expect.element(page.getByRole("dialog", { name: "Usage" })).toHaveFocus();
45 + await userEvent.keyboard("{Escape}");
46 +
47 + await expect.element(page.getByRole("dialog", { name: "Usage" })).not.toBeInTheDocument();
48 + await expect.element(openerButton).toHaveFocus();
49 + });
50 +
51 it("stays open when Esc belongs to a modal outside Base UI", async () => {
52 await using _screen = await render(
53 <AppRouteModal label="Settings">
apps/web/src/components/AppRouteModal.tsx
+19 -2
@@ -3,7 +3,9 @@ import { T, useGT } from "gt-react";
3 import { XIcon } from "lucide-react";
4 import { useRef, useState, type CSSProperties, type ReactNode } from "react";
5
6 +import { getCommandPaletteOpener } from "../commandPaletteStore";
7 import { useNativeTitlebarHeight } from "../lib/windowControlsOverlay";
8 +import { requestWorkspaceFocus } from "../lib/workspaceFocus";
9 import { useCloseAppModal } from "./sidebar/mainAppLocation";
10 import { GlobalVoiceInputIndicator } from "./chat/GlobalVoiceInputIndicator";
11 import { Button } from "./ui/button";
@@ -21,8 +23,14 @@ export function AppRouteModal({ label, children }: { label: string; children: Re
23 // Text selection that ends on the backdrop is not a dismissal.
24 const backdropPressRef = useRef(false);
25 // Focus the surface, not its first control: no stray focus ring, no phone keyboard.
24 - // On close, leave focus alone; returning it to the opener paints a focus ring.
26 + // On close, return focus to the opener only after a keyboard close; after a pointer close it
27 + // would paint a focus ring.
28 const popupRef = useRef<HTMLDivElement>(null);
29 + const [opener] = useState(
30 + () =>
31 + getCommandPaletteOpener() ??
32 + (document.activeElement instanceof HTMLElement ? document.activeElement : null),
33 + );
34 // Clear the native window controls (macOS traffic lights, the Windows and Linux caption
35 // buttons). Below sm the modal fills the window and leaves that strip empty; up to lg its
36 // 1.5rem inset is shorter than the strip.
@@ -79,7 +87,16 @@ export function AppRouteModal({ label, children }: { label: string; children: Re
87 <DialogPrimitive.Popup
88 ref={popupRef}
89 initialFocus={popupRef}
82 - finalFocus={false}
90 + finalFocus={(closeType) => {
91 + if (closeType !== "keyboard" || !opener?.isConnected) return false;
92 + if (!opener.closest("[inert]")) return opener;
93 + // The thread behind the modal stays inert until the route closes, so its area
94 + // takes focus once it is usable again.
95 + const area = opener.closest<HTMLElement>("[data-workspace-area]");
96 + if (area?.dataset.workspaceArea)
97 + requestWorkspaceFocus({ area: area.dataset.workspaceArea });
98 + return false;
99 + }}
100 aria-label={label}
101 data-slot="dialog-popup"
102 data-app-modal=""