@devries-cameron20 / t3code-1 / commits / 43821388a9
Fork 0

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

fix(web): focus the primary action when confirmation dialogs open (#18187)

Co-authored-by: Roger Vilaseca <roger@tres65.es> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Roger V committed Nov 11, 2026 at 11:35 UTC 43821388a9af0bdd2e41c36af31cbc859fba5a71
12 files changed +240 -48
apps/web/src/components/ConfirmDialogHost.tsx
+9 -3
@@ -9,6 +9,7 @@ import {
9 import {
10 AlertDialog,
11 AlertDialogClose,
12 + AlertDialogAction,
13 AlertDialogDescription,
14 AlertDialogFooter,
15 AlertDialogHeader,
@@ -59,6 +60,7 @@ export function ConfirmDialogHost() {
60
61 const copy = resolveConfirmDialogCopy(state.status === "idle" ? "" : state.message);
62 const confirmVariant = state.status === "idle" ? "default" : state.variant;
63 + const focusConfirm = state.status !== "idle" && state.initialFocus === "confirm";
64 const onCancel = () => respondToConfirmDialog(false);
65 const onConfirm = () => respondToConfirmDialog(true);
66
@@ -83,9 +85,13 @@ export function ConfirmDialogHost() {
85 </AlertDialogHeader>
86 <AlertDialogFooter>
87 <AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
86 - <Button variant={confirmVariant} onClick={onConfirm}>
87 - Confirm
88 - </Button>
88 + {focusConfirm && confirmVariant === "default" ? (
89 + <AlertDialogAction onClick={onConfirm}>Confirm</AlertDialogAction>
90 + ) : (
91 + <Button variant={confirmVariant} onClick={onConfirm}>
92 + Confirm
93 + </Button>
94 + )}
95 </AlertDialogFooter>
96 </AlertDialogPopup>
97 </AlertDialog>
apps/web/src/components/ServerUpdateAction.tsx
+2
@@ -202,6 +202,7 @@ export function ServerUpdatesAction({
202 const confirmed =
203 (await requestConfirmDialog(
204 `Update the T3 Code desktop apps on ${desktopTargets.map((target) => target.serverLabel).join(", ")}? They will close and relaunch on those machines.`,
205 + { initialFocus: "confirm" },
206 )) ?? true;
207 if (!confirmed) return;
208 }
@@ -308,6 +309,7 @@ export function ServerUpdateAction({
309 const confirmed =
310 (await requestConfirmDialog(
311 `Update the T3 Code desktop app that runs the ${serverLabel}? It will close and relaunch on that machine.`,
312 + { initialFocus: "confirm" },
313 )) ?? true;
314 if (!confirmed) {
315 return;
apps/web/src/components/cloud/RelayClientInstallDialog.tsx
+3 -2
@@ -12,6 +12,7 @@ import { Button } from "../ui/button";
12 import {
13 Dialog,
14 DialogDescription,
15 + DialogAction,
16 DialogFooter,
17 DialogHeader,
18 DialogPanel,
@@ -112,9 +113,9 @@ export function RelayClientInstallDialog() {
113 >
114 Cancel
115 </Button>
115 - <Button onClick={() => respondToRelayClientInstallConfirmation(true)}>
116 + <DialogAction onClick={() => respondToRelayClientInstallConfirmation(true)}>
117 Download and install
117 - </Button>
118 + </DialogAction>
119 </DialogFooter>
120 ) : null}
121 </DialogPopup>
apps/web/src/components/settings/BrowserImportWizard.tsx
+3 -2
@@ -12,6 +12,7 @@ import {
12 Dialog,
13 DialogClose,
14 DialogDescription,
15 + DialogAction,
16 DialogFooter,
17 DialogHeader,
18 DialogPanel,
@@ -201,7 +202,7 @@ function QuitStep({
202 <Button variant="outline" onClick={onCancel}>
203 Cancel
204 </Button>
204 - <Button onClick={onRechecked}>I&rsquo;ve quit it</Button>
205 + <DialogAction onClick={onRechecked}>I&rsquo;ve quit it</DialogAction>
206 </DialogFooter>
207 </>
208 );
@@ -575,7 +576,7 @@ function BlockedStep({
576 <Button variant="outline" onClick={onClose}>
577 Close
578 </Button>
578 - {onRetry ? <Button onClick={onRetry}>Try again</Button> : null}
579 + {onRetry ? <DialogAction onClick={onRetry}>Try again</DialogAction> : null}
580 </DialogFooter>
581 </>
582 );
apps/web/src/components/settings/SettingsPanels.tsx
+1
@@ -223,6 +223,7 @@ function AboutVersionSection() {
223 getDesktopUpdateInstallConfirmationMessage(
224 updateState ?? { availableVersion: null, downloadedVersion: null },
225 ),
226 + { initialFocus: "confirm" },
227 );
228 } catch (error) {
229 setIsUpdateActionPending(false);
apps/web/src/components/sidebar/SidebarUpdatePill.tsx
+1
@@ -308,6 +308,7 @@ function SidebarUpdateControl() {
308 try {
309 confirmed = await ensureLocalApi().dialogs.confirm(
310 getDesktopUpdateInstallConfirmationMessage(state),
311 + { initialFocus: "confirm" },
312 );
313 } catch (error) {
314 setIsActionPending(false);
apps/web/src/components/ui/alert-dialog.tsx
+27 -12
@@ -1,4 +1,5 @@
1 import { AlertDialog as AlertDialogPrimitive } from "@base-ui/react/alert-dialog";
2 +import { useRender } from "@base-ui/react/use-render";
3
4 import { cn } from "~/lib/utils";
5 import {
@@ -6,7 +7,12 @@ import {
7 DIALOG_MOBILE_SHEET_CLASS,
8 DIALOG_POPUP_CLASS,
9 } from "~/components/ui/dialog-styles";
9 -import { ControlledOpenContext, renderRetainedPopup } from "~/components/ui/dialog";
10 +import {
11 + ControlledOpenContext,
12 + DialogAction,
13 + renderRetainedPopup,
14 + useDialogActionInitialFocus,
15 +} from "~/components/ui/dialog";
16
17 function AlertDialog<Payload>(props: AlertDialogPrimitive.Root.Props<Payload>) {
18 return (
@@ -51,23 +57,31 @@ function AlertDialogPopup({
57 bottomStickOnMobile?: boolean;
58 portalContainer?: AlertDialogPrimitive.Portal.Props["container"];
59 }) {
60 + const { popupRef, initialFocus } = useDialogActionInitialFocus();
61 + const popup = useRender({
62 + ref: popupRef,
63 + render: (
64 + <AlertDialogPrimitive.Popup
65 + className={cn(
66 + DIALOG_POPUP_CLASS,
67 + "row-start-2 max-h-full max-w-lg text-popover-foreground",
68 + bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
69 + className,
70 + )}
71 + data-slot="alert-dialog-popup"
72 + initialFocus={initialFocus}
73 + render={renderRetainedPopup}
74 + {...props}
75 + />
76 + ),
77 + });
78 return (
79 <AlertDialogPortal container={portalContainer}>
80 <AlertDialogBackdrop />
81 <AlertDialogViewport
82 className={cn(bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12")}
83 >
60 - <AlertDialogPrimitive.Popup
61 - className={cn(
62 - DIALOG_POPUP_CLASS,
63 - "row-start-2 max-h-full max-w-lg text-popover-foreground",
64 - bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
65 - className,
66 - )}
67 - data-slot="alert-dialog-popup"
68 - render={renderRetainedPopup}
69 - {...props}
70 - />
84 + {popup}
85 </AlertDialogViewport>
86 </AlertDialogPortal>
87 );
@@ -146,5 +160,6 @@ export {
160 AlertDialogTitle,
161 AlertDialogDescription,
162 AlertDialogClose,
163 + DialogAction as AlertDialogAction,
164 AlertDialogViewport,
165 };
apps/web/src/components/ui/dialog.browser.test.tsx new
+108
@@ -0,0 +1,108 @@
1 +import { createRef } from "react";
2 +import { describe, expect, it } from "vite-plus/test";
3 +import { page, userEvent } from "vite-plus/test/browser";
4 +
5 +import { requestConfirmDialog, resetConfirmDialogForTests } from "~/confirmDialog";
6 +import { act, render } from "../../../test/render";
7 +import { ConfirmDialogHost } from "../ConfirmDialogHost";
8 +import { Button } from "./button";
9 +import {
10 + Dialog,
11 + DialogAction,
12 + DialogClose,
13 + DialogPopup,
14 + DialogTitle,
15 + DialogTrigger,
16 +} from "./dialog";
17 +
18 +describe("dialog initial focus", () => {
19 + it.each([
20 + { label: "generic", options: undefined, focused: "Cancel", confirmed: false },
21 + {
22 + label: "destructive opt-in",
23 + options: { variant: "destructive", initialFocus: "confirm" },
24 + focused: "Cancel",
25 + confirmed: false,
26 + },
27 + { label: "opt-in", options: { initialFocus: "confirm" }, focused: "Confirm", confirmed: true },
28 + ] as const)(
29 + "focuses $focused for a $label confirmation",
30 + async ({ options, focused, confirmed }) => {
31 + resetConfirmDialogForTests();
32 + await using _screen = await render(<ConfirmDialogHost />);
33 + let confirmation: Promise<boolean> | undefined;
34 + await act(() => {
35 + confirmation = requestConfirmDialog("Continue?", options);
36 + });
37 +
38 + await expect.element(page.getByRole("button", { name: focused, exact: true })).toHaveFocus();
39 + await userEvent.keyboard("{Enter}");
40 + await expect(confirmation).resolves.toBe(confirmed);
41 + },
42 + );
43 +
44 + it("focuses the dialog action and keeps the caller's popup ref", async () => {
45 + const popup = createRef<HTMLDivElement>();
46 + await using _screen = await render(
47 + <Dialog>
48 + <DialogTrigger>Open retry</DialogTrigger>
49 + <DialogPopup ref={popup}>
50 + <DialogTitle>Import failed</DialogTitle>
51 + <DialogClose>Cancel</DialogClose>
52 + <DialogAction>Try again</DialogAction>
53 + </DialogPopup>
54 + </Dialog>,
55 + );
56 + await act(() => page.getByRole("button", { name: "Open retry" }).element().focus());
57 + await userEvent.keyboard("{Enter}");
58 +
59 + await expect.element(page.getByRole("button", { name: "Try again" })).toHaveFocus();
60 + expect(popup.current).toBe(page.getByRole("dialog").element());
61 + });
62 +
63 + it.each(["disabled action", "caller initialFocus"] as const)(
64 + "falls back to Cancel for a %s",
65 + async (reason) => {
66 + const cancel = createRef<HTMLButtonElement>();
67 + await using _screen = await render(
68 + <Dialog defaultOpen>
69 + <DialogPopup {...(reason === "caller initialFocus" ? { initialFocus: cancel } : {})}>
70 + <DialogTitle>Install update?</DialogTitle>
71 + <DialogClose ref={cancel} render={<Button variant="outline" />}>
72 + Cancel
73 + </DialogClose>
74 + <DialogAction disabled={reason === "disabled action"}>Install</DialogAction>
75 + </DialogPopup>
76 + </Dialog>,
77 + );
78 +
79 + await expect.element(page.getByRole("button", { name: "Cancel", exact: true })).toHaveFocus();
80 + },
81 + );
82 +
83 + it("focuses the popup instead of the action when opened by touch", async () => {
84 + await using _screen = await render(
85 + <Dialog>
86 + <DialogTrigger>Open installer</DialogTrigger>
87 + <DialogPopup>
88 + <DialogTitle>Install relay client</DialogTitle>
89 + <DialogClose>Cancel</DialogClose>
90 + <DialogAction>Install</DialogAction>
91 + </DialogPopup>
92 + </Dialog>,
93 + );
94 +
95 + const trigger = page.getByRole("button", { name: "Open installer" }).element();
96 + await act(() => {
97 + trigger.dispatchEvent(
98 + new PointerEvent("pointerdown", { bubbles: true, pointerType: "touch" }),
99 + );
100 + trigger.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, pointerType: "touch" }));
101 + trigger.dispatchEvent(
102 + new PointerEvent("click", { bubbles: true, pointerType: "touch", detail: 1 }),
103 + );
104 + });
105 +
106 + await expect.element(page.getByRole("dialog")).toHaveFocus();
107 + });
108 +});
apps/web/src/components/ui/dialog.tsx
+58 -25
@@ -1,8 +1,9 @@
1 import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
2 +import { useRender } from "@base-ui/react/use-render";
3 import { XIcon } from "lucide-react";
3 -import { type ComponentProps, createContext, type ReactNode, use, useState } from "react";
4 +import { type ComponentProps, createContext, type ReactNode, use, useRef, useState } from "react";
5 import { cn } from "~/lib/utils";
5 -import { Button } from "~/components/ui/button";
6 +import { Button, type ButtonVariant } from "~/components/ui/button";
7 import {
8 DIALOG_BACKDROP_CLASS,
9 DIALOG_MEDIA_BACKDROP_CLASS,
@@ -40,6 +41,20 @@ function Dialog<Payload>(props: DialogPrimitive.Root.Props<Payload>) {
41 );
42 }
43
44 +function useDialogActionInitialFocus() {
45 + const popupRef = useRef<HTMLDivElement>(null);
46 + const initialFocus = (openType: string) => {
47 + // Match Base UI: touch opens focus the popup to avoid opening the virtual keyboard.
48 + if (openType === "touch") return popupRef.current;
49 + return (
50 + popupRef.current?.querySelector<HTMLElement>(
51 + '[data-slot="dialog-action"]:not(:disabled):not([aria-disabled="true"])',
52 + ) ?? true
53 + );
54 + };
55 + return { popupRef, initialFocus };
56 +}
57 +
58 const DialogPortal = DialogPrimitive.Portal;
59
60 function DialogTrigger(props: DialogPrimitive.Trigger.Props) {
@@ -93,6 +108,36 @@ function DialogPopup({
108 bottomStickOnMobile?: boolean;
109 variant?: "default" | "media";
110 }) {
111 + const { popupRef, initialFocus } = useDialogActionInitialFocus();
112 + const popup = useRender({
113 + ref: popupRef,
114 + render: (
115 + <DialogPrimitive.Popup
116 + className={cn(
117 + variant === "media" ? DIALOG_MEDIA_POPUP_CLASS : DIALOG_POPUP_CLASS,
118 + "row-start-2 text-popover-foreground",
119 + variant === "default" && "max-h-full max-w-lg",
120 + bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
121 + className,
122 + )}
123 + data-slot="dialog-popup"
124 + initialFocus={initialFocus}
125 + render={renderRetainedPopup}
126 + {...props}
127 + >
128 + {children}
129 + {showCloseButton && (
130 + <DialogPrimitive.Close
131 + aria-label="Close"
132 + className="absolute end-2 top-2"
133 + render={<Button size="icon" variant="ghost" />}
134 + >
135 + <XIcon />
136 + </DialogPrimitive.Close>
137 + )}
138 + </DialogPrimitive.Popup>
139 + ),
140 + });
141 return (
142 <DialogPortal>
143 {/* Media opens from inside other overlays (a composer chip, a popover), so it sits above them. */}
@@ -104,34 +149,20 @@ function DialogPopup({
149 "z-[60] grid-rows-1 place-items-center px-4 pb-6 pt-[calc(var(--native-titlebar-height,0px)+1.5rem)] [-webkit-app-region:no-drag]",
150 )}
151 >
107 - <DialogPrimitive.Popup
108 - className={cn(
109 - variant === "media" ? DIALOG_MEDIA_POPUP_CLASS : DIALOG_POPUP_CLASS,
110 - "row-start-2 text-popover-foreground",
111 - variant === "default" && "max-h-full max-w-lg",
112 - bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
113 - className,
114 - )}
115 - data-slot="dialog-popup"
116 - render={renderRetainedPopup}
117 - {...props}
118 - >
119 - {children}
120 - {showCloseButton && (
121 - <DialogPrimitive.Close
122 - aria-label="Close"
123 - className="absolute end-2 top-2"
124 - render={<Button size="icon" variant="ghost" />}
125 - >
126 - <XIcon />
127 - </DialogPrimitive.Close>
128 - )}
129 - </DialogPrimitive.Popup>
152 + {popup}
153 </DialogViewport>
154 </DialogPortal>
155 );
156 }
157
158 +type DialogActionProps = Omit<React.ComponentProps<typeof Button>, "variant"> & {
159 + variant?: Exclude<ButtonVariant, `${string}destructive${string}`>;
160 +};
161 +
162 +function DialogAction(props: DialogActionProps) {
163 + return <Button data-slot="dialog-action" {...props} />;
164 +}
165 +
166 function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
167 return (
168 <div
@@ -212,6 +243,8 @@ export {
243 DialogTrigger,
244 DialogPortal,
245 DialogClose,
246 + DialogAction,
247 + useDialogActionInitialFocus,
248 DialogBackdrop,
249 DialogBackdrop as DialogOverlay,
250 DialogPopup,
apps/web/src/confirmDialog.test.ts
+4 -1
@@ -25,7 +25,9 @@ describe("confirm dialog coordinator", () => {
25
26 it("serializes concurrent confirmations", async () => {
27 const unregister = mountHost();
28 - const first = requireConfirmation(requestConfirmDialog("Delete the project?"));
28 + const first = requireConfirmation(
29 + requestConfirmDialog("Delete the project?", { initialFocus: "confirm" }),
30 + );
31 const second = requireConfirmation(requestConfirmDialog("Delete the worktree too?"));
32
33 respondToConfirmDialog(false);
@@ -34,6 +36,7 @@ describe("confirm dialog coordinator", () => {
36 status: "closing",
37 message: "Delete the project?",
38 variant: "default",
39 + initialFocus: "confirm",
40 });
41
42 completeConfirmDialogClose();
apps/web/src/confirmDialog.ts
+22 -3
@@ -6,16 +6,19 @@ export type ConfirmDialogState =
6 readonly status: "confirming";
7 readonly message: string;
8 readonly variant: ConfirmDialogVariant;
9 + readonly initialFocus: ConfirmDialogOptions["initialFocus"];
10 }
11 | {
12 readonly status: "closing";
13 readonly message: string;
14 readonly variant: ConfirmDialogVariant;
15 + readonly initialFocus: ConfirmDialogOptions["initialFocus"];
16 };
17
18 type PendingConfirmation = {
19 readonly message: string;
20 readonly variant: ConfirmDialogVariant;
21 + readonly initialFocus: ConfirmDialogOptions["initialFocus"];
22 readonly resolve: (confirmed: boolean) => void;
23 };
24
@@ -69,6 +72,7 @@ export function requestConfirmDialog(
72 const pending = {
73 message,
74 variant: options?.variant ?? "default",
75 + initialFocus: options?.initialFocus,
76 resolve,
77 } satisfies PendingConfirmation;
78 if (activeConfirmation || state.status === "closing") {
@@ -77,7 +81,12 @@ export function requestConfirmDialog(
81 }
82
83 activeConfirmation = pending;
80 - publish({ status: "confirming", message, variant: pending.variant });
84 + publish({
85 + status: "confirming",
86 + message,
87 + variant: pending.variant,
88 + initialFocus: pending.initialFocus,
89 + });
90 });
91
92 return confirmation;
@@ -93,7 +102,12 @@ export function respondToConfirmDialog(confirmed: boolean): void {
102 const confirmation = activeConfirmation;
103 activeConfirmation = null;
104 confirmation.resolve(confirmed);
96 - publish({ status: "closing", message: state.message, variant: state.variant });
105 + publish({
106 + status: "closing",
107 + message: state.message,
108 + variant: state.variant,
109 + initialFocus: state.initialFocus,
110 + });
111 }
112
113 export function completeConfirmDialogClose(): void {
@@ -106,7 +120,12 @@ export function completeConfirmDialogClose(): void {
120 }
121
122 activeConfirmation = next;
109 - publish({ status: "confirming", message: next.message, variant: next.variant });
123 + publish({
124 + status: "confirming",
125 + message: next.message,
126 + variant: next.variant,
127 + initialFocus: next.initialFocus,
128 + });
129 }
130
131 export function resetConfirmDialogForTests(): void {
packages/contracts/src/ipc.ts
+2
@@ -1333,6 +1333,8 @@ export type ConfirmDialogVariant = "default" | "destructive";
1333
1334 export interface ConfirmDialogOptions {
1335 readonly variant?: ConfirmDialogVariant;
1336 + /** Which button takes focus on open. Defaults to Cancel; destructive confirms always keep Cancel. */
1337 + readonly initialFocus?: "confirm" | "cancel";
1338 }
1339
1340 export interface LocalApi {