Skip to content

Commit 5bc19ac

Browse files
authored
fix(web): dialogs keep their content while they close (#21960)
1 parent 3719fd2 commit 5bc19ac

5 files changed

Lines changed: 59 additions & 22 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,8 +1008,6 @@ export default function GitActionsControl({
10081008
const [inlineSuccess, setInlineSuccess] = useState<InlineGitActionSuccess | null>(null);
10091009
const [pendingDefaultBranchAction, setPendingDefaultBranchAction] =
10101010
useState<PendingDefaultBranchAction | null>(null);
1011-
// Keep the payload until the dialog finishes closing.
1012-
const [isDefaultBranchDialogOpen, setIsDefaultBranchDialogOpen] = useState(false);
10131011
const sourceControlScope = { environmentId: activeEnvironmentId, cwd: gitCwd };
10141012
const vcsActionState = useAtomValue(vcsActionManager.stateAtom(sourceControlScope));
10151013
const visibleInlineSuccess = inlineSuccess?.scopeKey === successScopeKey ? inlineSuccess : null;
@@ -1252,7 +1250,6 @@ export default function GitActionsControl({
12521250
...(onConfirmed ? { onConfirmed } : {}),
12531251
...(filePaths ? { filePaths } : {}),
12541252
});
1255-
setIsDefaultBranchDialogOpen(true);
12561253
return;
12571254
}
12581255
onConfirmed?.();
@@ -1363,11 +1360,11 @@ export default function GitActionsControl({
13631360
}
13641361

13651362
const continuePendingDefaultBranchAction = (featureBranch: boolean) => {
1366-
if (!isDefaultBranchDialogOpen || !pendingDefaultBranchAction || !canWriteSourceControl) return;
1363+
if (!pendingDefaultBranchAction || !canWriteSourceControl) return;
13671364
if (featureBranch && (!canChangeThreadBranch || featureBranchUnavailableReason !== null))
13681365
return;
13691366
const { action, commitMessage, onConfirmed, filePaths } = pendingDefaultBranchAction;
1370-
setIsDefaultBranchDialogOpen(false);
1367+
setPendingDefaultBranchAction(null);
13711368
void runGitActionWithToast({
13721369
action,
13731370
...(commitMessage ? { commitMessage } : {}),
@@ -2033,9 +2030,8 @@ export default function GitActionsControl({
20332030
/>
20342031

20352032
<Dialog
2036-
open={isDefaultBranchDialogOpen}
2037-
onOpenChange={setIsDefaultBranchDialogOpen}
2038-
onOpenChangeComplete={(open) => {
2033+
open={pendingDefaultBranchAction !== null}
2034+
onOpenChange={(open) => {
20392035
if (!open) setPendingDefaultBranchAction(null);
20402036
}}
20412037
>
@@ -2054,7 +2050,7 @@ export default function GitActionsControl({
20542050
className="w-full sm:mr-auto sm:w-auto"
20552051
variant="outline"
20562052
size="sm"
2057-
onClick={() => setIsDefaultBranchDialogOpen(false)}
2053+
onClick={() => setPendingDefaultBranchAction(null)}
20582054
>
20592055
Abort
20602056
</Button>

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,6 @@ export function ConnectionsSettings() {
22062206
const [removingSavedEnvironmentId, setRemovingSavedEnvironmentId] =
22072207
useState<EnvironmentId | null>(null);
22082208
const [isUpdatingDesktopServerExposure, setIsUpdatingDesktopServerExposure] = useState(false);
2209-
const [isDesktopServerExposureDialogOpen, setIsDesktopServerExposureDialogOpen] = useState(false);
22102209
const [isUpdatingTailscaleServe, setIsUpdatingTailscaleServe] = useState(false);
22112210
const [isUpdatingWslBackend, setIsUpdatingWslBackend] = useState(false);
22122211
const [desktopWslMutationError, setDesktopWslMutationError] = useState<string | null>(null);
@@ -2354,11 +2353,11 @@ export function ConnectionsSettings() {
23542353
try {
23552354
await desktopBridge.setServerExposureMode(checked ? "network-accessible" : "local-only");
23562355
refreshDesktopNetworkAccessState();
2357-
setIsDesktopServerExposureDialogOpen(false);
2356+
setPendingDesktopServerExposureMode(null);
23582357
setIsUpdatingDesktopServerExposure(false);
23592358
} catch (error) {
23602359
const message = error instanceof Error ? error.message : "Failed to update network exposure.";
2361-
setIsDesktopServerExposureDialogOpen(false);
2360+
setPendingDesktopServerExposureMode(null);
23622361
setDesktopServerExposureMutationError(message);
23632362
toastManager.add(
23642363
stackedThreadToast({
@@ -3073,7 +3072,6 @@ export function ConnectionsSettings() {
30733072
disabled={!desktopServerExposureState || isUpdatingDesktopServerExposure}
30743073
onCheckedChange={(checked) => {
30753074
setPendingDesktopServerExposureMode(checked ? "network-accessible" : "local-only");
3076-
setIsDesktopServerExposureDialogOpen(true);
30773075
}}
30783076
aria-label="Enable network access"
30793077
/>
@@ -3632,13 +3630,10 @@ export function ConnectionsSettings() {
36323630
</FoldedSettingsSection>
36333631
) : null}
36343632
<AlertDialog
3635-
open={isDesktopServerExposureDialogOpen && canManageLocalBackend}
3633+
open={pendingDesktopServerExposureMode !== null && canManageLocalBackend}
36363634
onOpenChange={(open) => {
3637-
if (isUpdatingDesktopServerExposure) return;
3638-
setIsDesktopServerExposureDialogOpen(open);
3639-
}}
3640-
onOpenChangeComplete={(open) => {
3641-
if (!open) setPendingDesktopServerExposureMode(null);
3635+
if (!open && !isUpdatingDesktopServerExposure)
3636+
setPendingDesktopServerExposureMode(null);
36423637
}}
36433638
>
36443639
<AlertDialogPopup>

‎apps/web/src/components/ui/alert-dialog.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ import {
88
DIALOG_MOBILE_SHEET_CLASS,
99
DIALOG_POPUP_CLASS,
1010
} from "~/components/ui/dialog-styles";
11+
import { ControlledOpenContext, renderRetainedPopup } from "~/components/ui/dialog";
1112

12-
const AlertDialog = AlertDialogPrimitive.Root;
13+
function AlertDialog<Payload>(props: AlertDialogPrimitive.Root.Props<Payload>) {
14+
return (
15+
<ControlledOpenContext value={props.open}>
16+
<AlertDialogPrimitive.Root {...props} />
17+
</ControlledOpenContext>
18+
);
19+
}
1320

1421
const AlertDialogPortal = AlertDialogPrimitive.Portal;
1522

@@ -60,6 +67,7 @@ function AlertDialogPopup({
6067
className,
6168
)}
6269
data-slot="alert-dialog-popup"
70+
render={renderRetainedPopup}
6371
{...props}
6472
/>
6573
</AlertDialogViewport>

‎apps/web/src/components/ui/dialog.tsx‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
44
import { XIcon } from "lucide-react";
5+
import { type ComponentProps, createContext, type ReactNode, use, useState } from "react";
56
import { cn } from "~/lib/utils";
67
import { Button } from "~/components/ui/button";
78
import {
@@ -13,7 +14,33 @@ import {
1314
} from "~/components/ui/dialog-styles";
1415
import { ScrollArea } from "~/components/ui/scroll-area";
1516

16-
const Dialog = DialogPrimitive.Root;
17+
const ControlledOpenContext = createContext<boolean | undefined>(undefined);
18+
19+
function RetainedWhileClosed({ open, children }: { open: boolean; children: ReactNode }) {
20+
const controlledOpen = use(ControlledOpenContext);
21+
const isOpen = open && controlledOpen !== false;
22+
const [retained, setRetained] = useState(children);
23+
if (isOpen && retained !== children) {
24+
setRetained(children);
25+
}
26+
return isOpen ? children : retained;
27+
}
28+
29+
function renderRetainedPopup(props: ComponentProps<"div">, state: { open: boolean }) {
30+
return (
31+
<div {...props}>
32+
<RetainedWhileClosed open={state.open}>{props.children}</RetainedWhileClosed>
33+
</div>
34+
);
35+
}
36+
37+
function Dialog<Payload>(props: DialogPrimitive.Root.Props<Payload>) {
38+
return (
39+
<ControlledOpenContext value={props.open}>
40+
<DialogPrimitive.Root {...props} />
41+
</ControlledOpenContext>
42+
);
43+
}
1744

1845
const DialogPortal = DialogPrimitive.Portal;
1946

@@ -88,6 +115,7 @@ function DialogPopup({
88115
className,
89116
)}
90117
data-slot="dialog-popup"
118+
render={renderRetainedPopup}
91119
{...props}
92120
>
93121
{children}
@@ -180,6 +208,8 @@ function DialogPanel({
180208
}
181209

182210
export {
211+
ControlledOpenContext,
212+
renderRetainedPopup,
183213
Dialog,
184214
DialogTrigger,
185215
DialogPortal,

‎apps/web/src/components/ui/sheet.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import { XIcon } from "lucide-react";
55
import type { CSSProperties } from "react";
66
import { cn } from "~/lib/utils";
77
import { Button } from "~/components/ui/button";
8+
import { ControlledOpenContext, renderRetainedPopup } from "~/components/ui/dialog";
89

9-
const Sheet = SheetPrimitive.Root;
10+
function Sheet<Payload>(props: SheetPrimitive.Root.Props<Payload>) {
11+
return (
12+
<ControlledOpenContext value={props.open}>
13+
<SheetPrimitive.Root {...props} />
14+
</ControlledOpenContext>
15+
);
16+
}
1017

1118
const SheetPortal = SheetPrimitive.Portal;
1219

@@ -101,6 +108,7 @@ function SheetPopup({
101108
className,
102109
)}
103110
data-slot="sheet-popup"
111+
render={keepMounted ? undefined : renderRetainedPopup}
104112
style={{ ...transitionStyle, ...style }}
105113
{...props}
106114
>

0 commit comments

Comments
 (0)