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

AppRouteModal.tsx

86 lines3.5 KB

1import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
2import { XIcon } from "lucide-react";
3import { useRef, useState, type ReactNode } from "react";
4​
5import { useCloseAppModal } from "./sidebar/mainAppLocation";
6import { Button } from "./ui/button";
7import { DialogBackdrop } from "./ui/dialog";
8import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
9​
10/**
11 * Large modal for the Settings and Usage routes. Esc, the close button, or a press on the
12 * backdrop returns to the page that opened it. Keyboard guards that skip dialogs exempt
13 * `[data-app-modal]`, because these pages own their shortcuts.
14 */
15export function AppRouteModal({ label, children }: { label: string; children: ReactNode }) {
16 const closeAppModal = useCloseAppModal();
17 const [open, setOpen] = useState(true);
18 // Text selection that ends on the backdrop is not a dismissal.
19 const backdropPressRef = useRef(false);
20 // Focus the surface, not its first control: no stray focus ring, no phone keyboard.
21 // On close, leave focus alone; returning it to the opener paints a focus ring.
22 const popupRef = useRef<HTMLDivElement>(null);
23​
24 return (
25 <DialogPrimitive.Root
26 open={open}
27 // Portaled layers outside this tree (toasts, Clerk, confirm dialogs) are not backdrop presses.
28 disablePointerDismissal
29 onOpenChange={(nextOpen) => {
30 if (nextOpen) return;
31 // Closing unmounts a focused field without a blur, so commit-on-blur rows would
32 // drop what was typed. Blur first so they commit.
33 const focused = document.activeElement;
34 if (focused instanceof HTMLElement && popupRef.current?.contains(focused)) focused.blur();
35 setOpen(false);
36 }}
37 onOpenChangeComplete={(nextOpen) => {
38 if (!nextOpen) void closeAppModal();
39 }}
40 >
41 <DialogPrimitive.Portal>
42 <DialogBackdrop />
43 <DialogPrimitive.Viewport
44 className="fixed inset-0 z-50 flex items-center justify-center sm:p-6 lg:p-10"
45 onPointerDown={(event) => {
46 backdropPressRef.current = event.target === event.currentTarget;
47 }}
48 onClick={(event) => {
49 if (backdropPressRef.current && event.target === event.currentTarget) setOpen(false);
50 backdropPressRef.current = false;
51 }}
52 >
53 <DialogPrimitive.Popup
54 ref={popupRef}
55 initialFocus={popupRef}
56 finalFocus={false}
57 aria-label={label}
58 data-slot="dialog-popup"
59 data-app-modal=""
60 className="relative flex size-full min-h-0 min-w-0 flex-col overflow-hidden bg-background max-sm:pt-safe 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"
61 >
62 {children}
63 </DialogPrimitive.Popup>
64 </DialogPrimitive.Viewport>
65 </DialogPrimitive.Portal>
66 </DialogPrimitive.Root>
67 );
69​
70export function AppRouteModalClose() {
71 return (
72 <Tooltip>
73 <TooltipTrigger
74 render={
75 <DialogPrimitive.Close
76 aria-label="Close"
77 render={<Button size="icon-sm" variant="ghost" />}
78 />
79 }
80 >
81 <XIcon />
82 </TooltipTrigger>
83 <TooltipPopup side="bottom">Close (Esc)</TooltipPopup>
84 </Tooltip>
85 );