TypeScript
fix(web): dialogs keep their content while they close (#21960)
oliver5bc19ac641e9
diff --git a/apps/web/src/components/GitActionsControl.tsx b/apps/web/src/components/GitActionsControl.tsxindex 42663d4439b..2f61cec1de4 100644--- a/apps/web/src/components/GitActionsControl.tsx+++ b/apps/web/src/components/GitActionsControl.tsx@@ -1008,8 +1008,6 @@ export default function GitActionsControl({const [inlineSuccess, setInlineSuccess] = useState<InlineGitActionSuccess | null>(null);const [pendingDefaultBranchAction, setPendingDefaultBranchAction] =useState<PendingDefaultBranchAction | null>(null);- // Keep the payload until the dialog finishes closing.- const [isDefaultBranchDialogOpen, setIsDefaultBranchDialogOpen] = useState(false);const sourceControlScope = { environmentId: activeEnvironmentId, cwd: gitCwd };const vcsActionState = useAtomValue(vcsActionManager.stateAtom(sourceControlScope));const visibleInlineSuccess = inlineSuccess?.scopeKey === successScopeKey ? inlineSuccess : null;@@ -1252,7 +1250,6 @@ export default function GitActionsControl({...(onConfirmed ? { onConfirmed } : {}),...(filePaths ? { filePaths } : {}),});- setIsDefaultBranchDialogOpen(true);return;}onConfirmed?.();@@ -1363,11 +1360,11 @@ export default function GitActionsControl({}const continuePendingDefaultBranchAction = (featureBranch: boolean) => {- if (!isDefaultBranchDialogOpen || !pendingDefaultBranchAction || !canWriteSourceControl) return;+ if (!pendingDefaultBranchAction || !canWriteSourceControl) return;if (featureBranch && (!canChangeThreadBranch || featureBranchUnavailableReason !== null))return;const { action, commitMessage, onConfirmed, filePaths } = pendingDefaultBranchAction;- setIsDefaultBranchDialogOpen(false);+ setPendingDefaultBranchAction(null);void runGitActionWithToast({action,...(commitMessage ? { commitMessage } : {}),@@ -2033,9 +2030,8 @@ export default function GitActionsControl({/><Dialog- open={isDefaultBranchDialogOpen}- onOpenChange={setIsDefaultBranchDialogOpen}- onOpenChangeComplete={(open) => {+ open={pendingDefaultBranchAction !== null}+ onOpenChange={(open) => {if (!open) setPendingDefaultBranchAction(null);}}>@@ -2054,7 +2050,7 @@ export default function GitActionsControl({className="w-full sm:mr-auto sm:w-auto"variant="outline"size="sm"- onClick={() => setIsDefaultBranchDialogOpen(false)}+ onClick={() => setPendingDefaultBranchAction(null)}>Abort</Button>diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsxindex ee0328f602f..bda517c4fe1 100644--- a/apps/web/src/components/settings/ConnectionsSettings.tsx+++ b/apps/web/src/components/settings/ConnectionsSettings.tsx@@ -2206,7 +2206,6 @@ export function ConnectionsSettings() {const [removingSavedEnvironmentId, setRemovingSavedEnvironmentId] =useState<EnvironmentId | null>(null);const [isUpdatingDesktopServerExposure, setIsUpdatingDesktopServerExposure] = useState(false);- const [isDesktopServerExposureDialogOpen, setIsDesktopServerExposureDialogOpen] = useState(false);const [isUpdatingTailscaleServe, setIsUpdatingTailscaleServe] = useState(false);const [isUpdatingWslBackend, setIsUpdatingWslBackend] = useState(false);const [desktopWslMutationError, setDesktopWslMutationError] = useState<string | null>(null);@@ -2354,11 +2353,11 @@ export function ConnectionsSettings() {try {await desktopBridge.setServerExposureMode(checked ? "network-accessible" : "local-only");refreshDesktopNetworkAccessState();- setIsDesktopServerExposureDialogOpen(false);+ setPendingDesktopServerExposureMode(null);setIsUpdatingDesktopServerExposure(false);} catch (error) {const message = error instanceof Error ? error.message : "Failed to update network exposure.";- setIsDesktopServerExposureDialogOpen(false);+ setPendingDesktopServerExposureMode(null);setDesktopServerExposureMutationError(message);toastManager.add(stackedThreadToast({@@ -3073,7 +3072,6 @@ export function ConnectionsSettings() {disabled={!desktopServerExposureState || isUpdatingDesktopServerExposure}onCheckedChange={(checked) => {setPendingDesktopServerExposureMode(checked ? "network-accessible" : "local-only");- setIsDesktopServerExposureDialogOpen(true);}}aria-label="Enable network access"/>@@ -3632,13 +3630,10 @@ export function ConnectionsSettings() {</FoldedSettingsSection>) : null}<AlertDialog- open={isDesktopServerExposureDialogOpen && canManageLocalBackend}+ open={pendingDesktopServerExposureMode !== null && canManageLocalBackend}onOpenChange={(open) => {- if (isUpdatingDesktopServerExposure) return;- setIsDesktopServerExposureDialogOpen(open);- }}- onOpenChangeComplete={(open) => {- if (!open) setPendingDesktopServerExposureMode(null);+ if (!open && !isUpdatingDesktopServerExposure)+ setPendingDesktopServerExposureMode(null);}}><AlertDialogPopup>diff --git a/apps/web/src/components/ui/alert-dialog.tsx b/apps/web/src/components/ui/alert-dialog.tsxindex ee086887557..b0d224b13c1 100644--- a/apps/web/src/components/ui/alert-dialog.tsx+++ b/apps/web/src/components/ui/alert-dialog.tsx@@ -8,8 +8,15 @@ import {DIALOG_MOBILE_SHEET_CLASS,DIALOG_POPUP_CLASS,} from "~/components/ui/dialog-styles";+import { ControlledOpenContext, renderRetainedPopup } from "~/components/ui/dialog";-const AlertDialog = AlertDialogPrimitive.Root;+function AlertDialog<Payload>(props: AlertDialogPrimitive.Root.Props<Payload>) {+ return (+ <ControlledOpenContext value={props.open}>+ <AlertDialogPrimitive.Root {...props} />+ </ControlledOpenContext>+ );+}const AlertDialogPortal = AlertDialogPrimitive.Portal;@@ -60,6 +67,7 @@ function AlertDialogPopup({className,)}data-slot="alert-dialog-popup"+ render={renderRetainedPopup}{...props}/></AlertDialogViewport>diff --git a/apps/web/src/components/ui/dialog.tsx b/apps/web/src/components/ui/dialog.tsxindex 11b27495657..c2bc3155139 100644--- a/apps/web/src/components/ui/dialog.tsx+++ b/apps/web/src/components/ui/dialog.tsx@@ -2,6 +2,7 @@import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";import { XIcon } from "lucide-react";+import { type ComponentProps, createContext, type ReactNode, use, useState } from "react";import { cn } from "~/lib/utils";import { Button } from "~/components/ui/button";import {@@ -13,7 +14,33 @@ import {} from "~/components/ui/dialog-styles";import { ScrollArea } from "~/components/ui/scroll-area";-const Dialog = DialogPrimitive.Root;+const ControlledOpenContext = createContext<boolean | undefined>(undefined);++function RetainedWhileClosed({ open, children }: { open: boolean; children: ReactNode }) {+ const controlledOpen = use(ControlledOpenContext);+ const isOpen = open && controlledOpen !== false;+ const [retained, setRetained] = useState(children);+ if (isOpen && retained !== children) {+ setRetained(children);+ }+ return isOpen ? children : retained;+}++function renderRetainedPopup(props: ComponentProps<"div">, state: { open: boolean }) {+ return (+ <div {...props}>+ <RetainedWhileClosed open={state.open}>{props.children}</RetainedWhileClosed>+ </div>+ );+}++function Dialog<Payload>(props: DialogPrimitive.Root.Props<Payload>) {+ return (+ <ControlledOpenContext value={props.open}>+ <DialogPrimitive.Root {...props} />+ </ControlledOpenContext>+ );+}const DialogPortal = DialogPrimitive.Portal;@@ -88,6 +115,7 @@ function DialogPopup({className,)}data-slot="dialog-popup"+ render={renderRetainedPopup}{...props}>{children}@@ -180,6 +208,8 @@ function DialogPanel({}export {+ ControlledOpenContext,+ renderRetainedPopup,Dialog,DialogTrigger,DialogPortal,diff --git a/apps/web/src/components/ui/sheet.tsx b/apps/web/src/components/ui/sheet.tsxindex ce4a7b21a8a..dc020fc59c2 100644--- a/apps/web/src/components/ui/sheet.tsx+++ b/apps/web/src/components/ui/sheet.tsx@@ -5,8 +5,15 @@ import { XIcon } from "lucide-react";import type { CSSProperties } from "react";import { cn } from "~/lib/utils";import { Button } from "~/components/ui/button";+import { ControlledOpenContext, renderRetainedPopup } from "~/components/ui/dialog";-const Sheet = SheetPrimitive.Root;+function Sheet<Payload>(props: SheetPrimitive.Root.Props<Payload>) {+ return (+ <ControlledOpenContext value={props.open}>+ <SheetPrimitive.Root {...props} />+ </ControlledOpenContext>+ );+}const SheetPortal = SheetPrimitive.Portal;@@ -101,6 +108,7 @@ function SheetPopup({className,)}data-slot="sheet-popup"+ render={keepMounted ? undefined : renderRetainedPopup}style={{ ...transitionStyle, ...style }}{...props}>