The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Dialog as DialogPrimitive } from "@base-ui/react/dialog"; |
| 2 | 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"; |
| 12 | import { DialogBackdrop } from "./ui/dialog"; |
| 13 | import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; |
| 14 | |
| 15 | /** |
| 16 | * Large modal for the Settings and Usage routes. Esc, the close button, or a press on the |
| 17 | * backdrop returns to the page that opened it. Keyboard guards that skip dialogs exempt |
| 18 | * `[data-app-modal]`, because these pages own their shortcuts. |
| 19 | */ |
| 20 | export function AppRouteModal({ label, children }: { label: string; children: ReactNode }) { |
| 21 | const closeAppModal = useCloseAppModal(); |
| 22 | const [open, setOpen] = useState(true); |
| 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. |
| 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. |
| 37 | const nativeTitlebarHeight = useNativeTitlebarHeight(); |
| 38 | |
| 39 | return ( |
| 40 | <DialogPrimitive.Root |
| 41 | open={open} |
| 42 | // Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses. |
| 43 | disablePointerDismissal |
| 44 | onOpenChange={(nextOpen, eventDetails) => { |
| 45 | if (nextOpen) return; |
| 46 | // Esc from a layer outside Base UI (Clerk's modals) is that layer's to handle. |
| 47 | const target = eventDetails.event.target; |
| 48 | if ( |
| 49 | eventDetails.reason === "escape-key" && |
| 50 | target !== document.body && |
| 51 | target instanceof Node && |
| 52 | !popupRef.current?.contains(target) |
| 53 | ) { |
| 54 | return eventDetails.cancel(); |
| 55 | } |
| 56 | // Closing unmounts a focused field without a blur, so commit-on-blur rows would |
| 57 | // drop what was typed. Blur first so they commit. |
| 58 | const focused = document.activeElement; |
| 59 | if (focused instanceof HTMLElement && popupRef.current?.contains(focused)) focused.blur(); |
| 60 | setOpen(false); |
| 61 | }} |
| 62 | onOpenChangeComplete={(nextOpen) => { |
| 63 | if (!nextOpen) void closeAppModal(); |
| 64 | }} |
| 65 | > |
| 66 | <DialogPrimitive.Portal> |
| 67 | <DialogBackdrop /> |
| 68 | <DialogPrimitive.Viewport |
| 69 | style={ |
| 70 | { |
| 71 | "--app-modal-titlebar-height": `${nativeTitlebarHeight}px`, |
| 72 | "--app-modal-top-inset": `max(env(safe-area-inset-top), ${nativeTitlebarHeight}px)`, |
| 73 | "--app-modal-sm-top": `max(1.5rem, ${nativeTitlebarHeight}px)`, |
| 74 | } as CSSProperties |
| 75 | } |
| 76 | // Electron hit-tests drag regions under the backdrop regardless of z-index; without |
| 77 | // this, presses above the modal move the window instead of dismissing it. |
| 78 | className="fixed inset-0 z-50 flex items-center justify-center [-webkit-app-region:no-drag] sm:p-6 sm:pt-(--app-modal-sm-top) lg:p-10" |
| 79 | onPointerDown={(event) => { |
| 80 | backdropPressRef.current = event.target === event.currentTarget; |
| 81 | }} |
| 82 | onClick={(event) => { |
| 83 | if (backdropPressRef.current && event.target === event.currentTarget) setOpen(false); |
| 84 | backdropPressRef.current = false; |
| 85 | }} |
| 86 | > |
| 87 | <DialogPrimitive.Popup |
| 88 | ref={popupRef} |
| 89 | initialFocus={popupRef} |
| 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="" |
| 103 | className="relative flex size-full min-h-0 min-w-0 flex-col overflow-hidden bg-background max-sm:pt-(--app-modal-top-inset) max-sm:pb-safe text-foreground outline-none transition-[opacity,scale] duration-150 ease-out [-webkit-app-region:no-drag] data-ending-style:scale-[0.985] data-ending-style:opacity-0 data-starting-style:scale-[0.985] data-starting-style:opacity-0 sm:max-h-[60rem] sm:max-w-[80rem] sm:rounded-2xl sm:border sm:shadow-2xl/40" |
| 104 | > |
| 105 | {/* The emptied strip still moves the window, as the titlebar it replaces does. */} |
| 106 | <div |
| 107 | aria-hidden |
| 108 | className="absolute inset-x-0 top-0 hidden h-(--app-modal-titlebar-height) [-webkit-app-region:drag] max-sm:block" |
| 109 | /> |
| 110 | {children} |
| 111 | <GlobalVoiceInputIndicator /> |
| 112 | </DialogPrimitive.Popup> |
| 113 | </DialogPrimitive.Viewport> |
| 114 | </DialogPrimitive.Portal> |
| 115 | </DialogPrimitive.Root> |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | export function AppRouteModalClose() { |
| 120 | const gt = useGT(); |
| 121 | return ( |
| 122 | <Tooltip> |
| 123 | <TooltipTrigger |
| 124 | render={ |
| 125 | <DialogPrimitive.Close |
| 126 | aria-label={gt("Close")} |
| 127 | render={<Button size="icon-sm" variant="ghost" />} |
| 128 | /> |
| 129 | } |
| 130 | > |
| 131 | <XIcon /> |
| 132 | </TooltipTrigger> |
| 133 | <TooltipPopup side="bottom"> |
| 134 | <T>Close (Esc)</T> |
| 135 | </TooltipPopup> |
| 136 | </Tooltip> |
| 137 | ); |
| 138 | } |