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

AppRouteModal.tsx

81 lines3.2 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) setOpen(false);
31 }}
32 onOpenChangeComplete={(nextOpen) => {
33 if (!nextOpen) void closeAppModal();
34 }}
35 >
36 <DialogPrimitive.Portal>
37 <DialogBackdrop />
38 <DialogPrimitive.Viewport
39 className="fixed inset-0 z-50 flex items-center justify-center sm:p-6 lg:p-10"
40 onPointerDown={(event) => {
41 backdropPressRef.current = event.target === event.currentTarget;
42 }}
43 onClick={(event) => {
44 if (backdropPressRef.current && event.target === event.currentTarget) setOpen(false);
45 backdropPressRef.current = false;
46 }}
47 >
48 <DialogPrimitive.Popup
49 ref={popupRef}
50 initialFocus={popupRef}
51 finalFocus={false}
52 aria-label={label}
53 data-slot="dialog-popup"
54 data-app-modal=""
55 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"
56 >
57 {children}
58 </DialogPrimitive.Popup>
59 </DialogPrimitive.Viewport>
60 </DialogPrimitive.Portal>
61 </DialogPrimitive.Root>
62 );
64​
65export function AppRouteModalClose() {
66 return (
67 <Tooltip>
68 <TooltipTrigger
69 render={
70 <DialogPrimitive.Close
71 aria-label="Close"
72 render={<Button size="icon-sm" variant="ghost" />}
73 />
74 }
75 >
76 <XIcon />
77 </TooltipTrigger>
78 <TooltipPopup side="bottom">Close (Esc)</TooltipPopup>
79 </Tooltip>
80 );