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

dialog.tsx

259 lines7.3 KB

1import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
2import { useRender } from "@base-ui/react/use-render";
3import { XIcon } from "lucide-react";
4import { type ComponentProps, createContext, type ReactNode, use, useRef, useState } from "react";
5import { cn } from "~/lib/utils";
6import { Button, type ButtonVariant } from "~/components/ui/button";
7import {
8 DIALOG_BACKDROP_CLASS,
9 DIALOG_MEDIA_BACKDROP_CLASS,
10 DIALOG_MEDIA_POPUP_CLASS,
11 DIALOG_MOBILE_SHEET_CLASS,
12 DIALOG_POPUP_CLASS,
13} from "~/components/ui/dialog-styles";
14import { ScrollArea } from "~/components/ui/scroll-area";
15​
16const ControlledOpenContext = createContext<boolean | undefined>(undefined);
17​
18function RetainedWhileClosed({ open, children }: { open: boolean; children: ReactNode }) {
19 const controlledOpen = use(ControlledOpenContext);
20 const isOpen = open && controlledOpen !== false;
21 const [retained, setRetained] = useState(children);
22 if (isOpen && retained !== children) {
23 setRetained(children);
24 }
25 return isOpen ? children : retained;
27​
28function renderRetainedPopup(props: ComponentProps<"div">, state: { open: boolean }) {
29 return (
30 <div {...props}>
31 <RetainedWhileClosed open={state.open}>{props.children}</RetainedWhileClosed>
32 </div>
33 );
35​
36function Dialog<Payload>(props: DialogPrimitive.Root.Props<Payload>) {
37 return (
38 <ControlledOpenContext value={props.open}>
39 <DialogPrimitive.Root {...props} />
40 </ControlledOpenContext>
41 );
43​
44function useDialogActionInitialFocus() {
45 const popupRef = useRef<HTMLDivElement>(null);
46 const initialFocus = (openType: string) => {
47 // Match Base UI: touch opens focus the popup to avoid opening the virtual keyboard.
48 if (openType === "touch") return popupRef.current;
49 return (
50 popupRef.current?.querySelector<HTMLElement>(
51 '[data-slot="dialog-action"]:not(:disabled):not([aria-disabled="true"])',
52 ) ?? true
53 );
54 };
55 return { popupRef, initialFocus };
57​
58const DialogPortal = DialogPrimitive.Portal;
59​
60function DialogTrigger(props: DialogPrimitive.Trigger.Props) {
61 return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
63​
64function DialogClose(props: DialogPrimitive.Close.Props) {
65 return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
67​
68function DialogBackdrop({
69 className,
70 variant = "default",
71 ...props
72}: DialogPrimitive.Backdrop.Props & { variant?: "default" | "media" }) {
73 return (
74 <DialogPrimitive.Backdrop
75 forceRender
76 className={cn(
77 variant === "media" ? DIALOG_MEDIA_BACKDROP_CLASS : DIALOG_BACKDROP_CLASS,
78 className,
79 )}
80 data-slot="dialog-backdrop"
81 {...props}
82 />
83 );
85​
86function DialogViewport({ className, ...props }: DialogPrimitive.Viewport.Props) {
87 return (
88 <DialogPrimitive.Viewport
89 // Electron drag regions ignore z-index, so without no-drag a click over a header drag strip never reaches the page.
90 className={cn(
91 "fixed inset-0 z-50 grid grid-rows-[1fr_auto_1fr] justify-items-center p-4 [-webkit-app-region:no-drag]",
92 className,
93 )}
94 data-slot="dialog-viewport"
95 {...props}
96 />
97 );
99​
100function DialogPopup({
101 className,
102 children,
103 showCloseButton = true,
104 bottomStickOnMobile = true,
105 variant = "default",
106 ...props
107}: DialogPrimitive.Popup.Props & {
108 showCloseButton?: boolean;
109 bottomStickOnMobile?: boolean;
110 variant?: "default" | "media";
111}) {
112 const { popupRef, initialFocus } = useDialogActionInitialFocus();
113 const popup = useRender({
114 ref: popupRef,
115 render: (
116 <DialogPrimitive.Popup
117 className={cn(
118 variant === "media" ? DIALOG_MEDIA_POPUP_CLASS : DIALOG_POPUP_CLASS,
119 "row-start-2 text-popover-foreground",
120 variant === "default" && "max-h-full max-w-lg",
121 bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
122 className,
123 )}
124 data-slot="dialog-popup"
125 initialFocus={initialFocus}
126 render={renderRetainedPopup}
127 {...props}
128 >
129 {children}
130 {showCloseButton && (
131 <DialogPrimitive.Close
132 aria-label="Close"
133 className="absolute end-2 top-2"
134 render={<Button size="icon" variant="ghost" />}
135 >
136 <XIcon />
137 </DialogPrimitive.Close>
138 )}
139 </DialogPrimitive.Popup>
140 ),
141 });
142 return (
143 <DialogPortal>
144 {/* Media opens from inside other overlays (a composer chip, a popover), so it sits above them. */}
145 <DialogBackdrop className={variant === "media" ? "z-[60]" : undefined} variant={variant} />
146 <DialogViewport
147 className={cn(
148 bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12",
149 variant === "media" &&
150 "z-[60] grid-rows-1 place-items-center px-4 pb-6 pt-[calc(var(--native-titlebar-height,0px)+1.5rem)]",
151 )}
152 >
153 {popup}
154 </DialogViewport>
155 </DialogPortal>
156 );
158​
159type DialogActionProps = Omit<React.ComponentProps<typeof Button>, "variant"> & {
160 variant?: Exclude<ButtonVariant, `${string}destructive${string}`>;
161};
162​
163function DialogAction(props: DialogActionProps) {
164 return <Button data-slot="dialog-action" {...props} />;
166​
167function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
168 return (
169 <div
170 className={cn(
171 "flex flex-col gap-2 p-6 in-[[data-slot=dialog-popup]:has([data-slot=dialog-panel])]:pb-3 max-sm:pb-4",
172 className,
173 )}
174 data-slot="dialog-header"
175 {...props}
176 />
177 );
179​
180function DialogFooter({
181 className,
182 variant = "default",
183 ...props
184}: React.ComponentProps<"div"> & {
185 variant?: "default" | "bare";
186}) {
187 return (
188 <div
189 className={cn(
190 "flex flex-col-reverse gap-2 px-6 sm:flex-row sm:justify-end sm:rounded-b-[calc(var(--radius-2xl)-1px)]",
191 variant === "default" && "border-t bg-muted/72 py-4",
192 variant === "bare" && "py-4",
193 className,
194 )}
195 data-slot="dialog-footer"
196 {...props}
197 />
198 );
200​
201function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
202 return (
203 <DialogPrimitive.Title
204 className={cn("wrap-anywhere font-semibold text-xl leading-snug", className)}
205 data-slot="dialog-title"
206 {...props}
207 />
208 );
210​
211function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) {
212 return (
213 <DialogPrimitive.Description
214 className={cn("text-muted-foreground text-sm", className)}
215 data-slot="dialog-description"
216 {...props}
217 />
218 );
220​
221function DialogPanel({
222 className,
223 scrollFade = true,
224 ...props
225}: React.ComponentProps<"div"> & { scrollFade?: boolean }) {
226 return (
227 <ScrollArea scrollFade={scrollFade}>
228 <div
229 className={cn(
230 "space-y-4 p-6 in-[[data-slot=dialog-popup]:has([data-slot=dialog-header])]:pt-1 in-[[data-slot=dialog-popup]:has([data-slot=dialog-footer]:not(.border-t))]:pb-1",
231 className,
232 )}
233 data-slot="dialog-panel"
234 {...props}
235 />
236 </ScrollArea>
237 );
239​
240export {
241 ControlledOpenContext,
242 renderRetainedPopup,
243 Dialog,
244 DialogTrigger,
245 DialogPortal,
246 DialogClose,
247 DialogAction,
248 useDialogActionInitialFocus,
249 DialogBackdrop,
250 DialogBackdrop as DialogOverlay,
251 DialogPopup,
252 DialogPopup as DialogContent,
253 DialogHeader,
254 DialogFooter,
255 DialogTitle,
256 DialogDescription,
257 DialogPanel,
258 DialogViewport,
259};