Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AppRouteModal.tsx

105 lines4.7 KB

1import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
2import { XIcon } from "lucide-react";
3import { useRef, useState, type CSSProperties, type ReactNode } from "react";
4​
5import { useNativeTitlebarHeight } from "../lib/windowControlsOverlay";
6import { useCloseAppModal } from "./sidebar/mainAppLocation";
7import { Button } from "./ui/button";
8import { DialogBackdrop } from "./ui/dialog";
9import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
10​
11/**
12 * Large modal for the Settings and Usage routes. Esc, the close button, or a press on the
13 * backdrop returns to the page that opened it. Keyboard guards that skip dialogs exempt
14 * `[data-app-modal]`, because these pages own their shortcuts.
15 */
16export function AppRouteModal({ label, children }: { label: string; children: ReactNode }) {
17 const closeAppModal = useCloseAppModal();
18 const [open, setOpen] = useState(true);
19 // Text selection that ends on the backdrop is not a dismissal.
20 const backdropPressRef = useRef(false);
21 // Focus the surface, not its first control: no stray focus ring, no phone keyboard.
22 // On close, leave focus alone; returning it to the opener paints a focus ring.
23 const popupRef = useRef<HTMLDivElement>(null);
24 // Clear the native window controls (macOS traffic lights, the Windows and Linux caption
25 // buttons). Below sm the modal fills the window and leaves that strip empty; up to lg its
26 // 1.5rem inset is shorter than the strip.
27 const nativeTitlebarHeight = useNativeTitlebarHeight();
28​
29 return (
30 <DialogPrimitive.Root
31 open={open}
32 // Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses.
33 disablePointerDismissal
34 onOpenChange={(nextOpen) => {
35 if (nextOpen) return;
36 // Closing unmounts a focused field without a blur, so commit-on-blur rows would
37 // drop what was typed. Blur first so they commit.
38 const focused = document.activeElement;
39 if (focused instanceof HTMLElement && popupRef.current?.contains(focused)) focused.blur();
40 setOpen(false);
41 }}
42 onOpenChangeComplete={(nextOpen) => {
43 if (!nextOpen) void closeAppModal();
44 }}
45 >
46 <DialogPrimitive.Portal>
47 <DialogBackdrop />
48 <DialogPrimitive.Viewport
49 style={
50 {
51 "--app-modal-titlebar-height": `${nativeTitlebarHeight}px`,
52 "--app-modal-top-inset": `max(env(safe-area-inset-top), ${nativeTitlebarHeight}px)`,
53 "--app-modal-sm-top": `max(1.5rem, ${nativeTitlebarHeight}px)`,
54 } as CSSProperties
55 }
56 // Electron hit-tests drag regions under the backdrop regardless of z-index; without
57 // this, presses above the modal move the window instead of dismissing it.
58 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"
59 onPointerDown={(event) => {
60 backdropPressRef.current = event.target === event.currentTarget;
61 }}
62 onClick={(event) => {
63 if (backdropPressRef.current && event.target === event.currentTarget) setOpen(false);
64 backdropPressRef.current = false;
65 }}
66 >
67 <DialogPrimitive.Popup
68 ref={popupRef}
69 initialFocus={popupRef}
70 finalFocus={false}
71 aria-label={label}
72 data-slot="dialog-popup"
73 data-app-modal=""
74 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"
75 >
76 {/* The emptied strip still moves the window, as the titlebar it replaces does. */}
77 <div
78 aria-hidden
79 className="absolute inset-x-0 top-0 hidden h-(--app-modal-titlebar-height) [-webkit-app-region:drag] max-sm:block"
80 />
81 {children}
82 </DialogPrimitive.Popup>
83 </DialogPrimitive.Viewport>
84 </DialogPrimitive.Portal>
85 </DialogPrimitive.Root>
86 );
88​
89export function AppRouteModalClose() {
90 return (
91 <Tooltip>
92 <TooltipTrigger
93 render={
94 <DialogPrimitive.Close
95 aria-label="Close"
96 render={<Button size="icon-sm" variant="ghost" />}
97 />
98 }
99 >
100 <XIcon />
101 </TooltipTrigger>
102 <TooltipPopup side="bottom">Close (Esc)</TooltipPopup>
103 </Tooltip>
104 );