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

AppRouteModal.tsx

115 lines5.1 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, eventDetails) => {
35 if (nextOpen) return;
36 // Esc from a layer outside Base UI (Clerk's modals) is that layer's to handle.
37 const target = eventDetails.event.target;
38 if (
39 eventDetails.reason === "escape-key" &&
40 target !== document.body &&
41 target instanceof Node &&
42 !popupRef.current?.contains(target)
43 ) {
44 return eventDetails.cancel();
45 }
46 // Closing unmounts a focused field without a blur, so commit-on-blur rows would
47 // drop what was typed. Blur first so they commit.
48 const focused = document.activeElement;
49 if (focused instanceof HTMLElement && popupRef.current?.contains(focused)) focused.blur();
50 setOpen(false);
51 }}
52 onOpenChangeComplete={(nextOpen) => {
53 if (!nextOpen) void closeAppModal();
54 }}
55 >
56 <DialogPrimitive.Portal>
57 <DialogBackdrop />
58 <DialogPrimitive.Viewport
59 style={
60 {
61 "--app-modal-titlebar-height": `${nativeTitlebarHeight}px`,
62 "--app-modal-top-inset": `max(env(safe-area-inset-top), ${nativeTitlebarHeight}px)`,
63 "--app-modal-sm-top": `max(1.5rem, ${nativeTitlebarHeight}px)`,
64 } as CSSProperties
65 }
66 // Electron hit-tests drag regions under the backdrop regardless of z-index; without
67 // this, presses above the modal move the window instead of dismissing it.
68 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"
69 onPointerDown={(event) => {
70 backdropPressRef.current = event.target === event.currentTarget;
71 }}
72 onClick={(event) => {
73 if (backdropPressRef.current && event.target === event.currentTarget) setOpen(false);
74 backdropPressRef.current = false;
75 }}
76 >
77 <DialogPrimitive.Popup
78 ref={popupRef}
79 initialFocus={popupRef}
80 finalFocus={false}
81 aria-label={label}
82 data-slot="dialog-popup"
83 data-app-modal=""
84 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"
85 >
86 {/* The emptied strip still moves the window, as the titlebar it replaces does. */}
87 <div
88 aria-hidden
89 className="absolute inset-x-0 top-0 hidden h-(--app-modal-titlebar-height) [-webkit-app-region:drag] max-sm:block"
90 />
91 {children}
92 </DialogPrimitive.Popup>
93 </DialogPrimitive.Viewport>
94 </DialogPrimitive.Portal>
95 </DialogPrimitive.Root>
96 );
98​
99export function AppRouteModalClose() {
100 return (
101 <Tooltip>
102 <TooltipTrigger
103 render={
104 <DialogPrimitive.Close
105 aria-label="Close"
106 render={<Button size="icon-sm" variant="ghost" />}
107 />
108 }
109 >
110 <XIcon />
111 </TooltipTrigger>
112 <TooltipPopup side="bottom">Close (Esc)</TooltipPopup>
113 </Tooltip>
114 );