import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
import { XIcon } from "lucide-react";
import { useRef, useState, type CSSProperties, type ReactNode } from "react";
import { useNativeTitlebarHeight } from "../lib/windowControlsOverlay";
import { useCloseAppModal } from "./sidebar/mainAppLocation";
import { Button } from "./ui/button";
import { DialogBackdrop } from "./ui/dialog";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
/**
* Large modal for the Settings and Usage routes. Esc, the close button, or a press on the
* backdrop returns to the page that opened it. Keyboard guards that skip dialogs exempt
* `[data-app-modal]`, because these pages own their shortcuts.
*/
export function AppRouteModal({ label, children }: { label: string; children: ReactNode }) {
const closeAppModal = useCloseAppModal();
const [open, setOpen] = useState(true);
// Text selection that ends on the backdrop is not a dismissal.
const backdropPressRef = useRef(false);
// Focus the surface, not its first control: no stray focus ring, no phone keyboard.
// On close, leave focus alone; returning it to the opener paints a focus ring.
const popupRef = useRef<HTMLDivElement>(null);
// Clear the native window controls (macOS traffic lights, the Windows and Linux caption
// buttons). Below sm the modal fills the window and leaves that strip empty; up to lg its
// 1.5rem inset is shorter than the strip.
const nativeTitlebarHeight = useNativeTitlebarHeight();
return (
<DialogPrimitive.Root
open={open}
// Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses.
disablePointerDismissal
onOpenChange={(nextOpen) => {
if (nextOpen) return;
// Closing unmounts a focused field without a blur, so commit-on-blur rows would
// drop what was typed. Blur first so they commit.
const focused = document.activeElement;
if (focused instanceof HTMLElement && popupRef.current?.contains(focused)) focused.blur();
setOpen(false);
}}
onOpenChangeComplete={(nextOpen) => {
if (!nextOpen) void closeAppModal();
}}
>
<DialogPrimitive.Portal>
<DialogBackdrop />
<DialogPrimitive.Viewport
style={
{
"--app-modal-titlebar-height": `${nativeTitlebarHeight}px`,
"--app-modal-top-inset": `max(env(safe-area-inset-top), ${nativeTitlebarHeight}px)`,
"--app-modal-sm-top": `max(1.5rem, ${nativeTitlebarHeight}px)`,
} as CSSProperties
}
className="fixed inset-0 z-50 flex items-center justify-center sm:p-6 sm:pt-(--app-modal-sm-top) lg:p-10"
onPointerDown={(event) => {
backdropPressRef.current = event.target === event.currentTarget;
}}
onClick={(event) => {
if (backdropPressRef.current && event.target === event.currentTarget) setOpen(false);
backdropPressRef.current = false;
}}
>
<DialogPrimitive.Popup
ref={popupRef}
initialFocus={popupRef}
finalFocus={false}
aria-label={label}
data-slot="dialog-popup"
data-app-modal=""
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"
>
{/* The emptied strip still moves the window, as the titlebar it replaces does. */}
<div
aria-hidden
className="absolute inset-x-0 top-0 hidden h-(--app-modal-titlebar-height) [-webkit-app-region:drag] max-sm:block"
/>
{children}
</DialogPrimitive.Popup>
</DialogPrimitive.Viewport>
</DialogPrimitive.Portal>
</DialogPrimitive.Root>
);
}
export function AppRouteModalClose() {
return (
<Tooltip>
<TooltipTrigger
render={
<DialogPrimitive.Close
aria-label="Close"
render={<Button size="icon-sm" variant="ghost" />}
/>
}
>
<XIcon />
</TooltipTrigger>
<TooltipPopup side="bottom">Close (Esc)</TooltipPopup>
</Tooltip>
);
}