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

alert-dialog.tsx

165 lines4.3 KB

1import { AlertDialog as AlertDialogPrimitive } from "@base-ui/react/alert-dialog";
2import { useRender } from "@base-ui/react/use-render";
3​
4import { cn } from "~/lib/utils";
5import {
6 DIALOG_BACKDROP_CLASS,
7 DIALOG_MOBILE_SHEET_CLASS,
8 DIALOG_POPUP_CLASS,
9} from "~/components/ui/dialog-styles";
10import {
11 ControlledOpenContext,
12 DialogAction,
13 renderRetainedPopup,
14 useDialogActionInitialFocus,
15} from "~/components/ui/dialog";
16​
17function AlertDialog<Payload>(props: AlertDialogPrimitive.Root.Props<Payload>) {
18 return (
19 <ControlledOpenContext value={props.open}>
20 <AlertDialogPrimitive.Root {...props} />
21 </ControlledOpenContext>
22 );
24​
25const AlertDialogPortal = AlertDialogPrimitive.Portal;
26​
27function AlertDialogBackdrop({ className, ...props }: AlertDialogPrimitive.Backdrop.Props) {
28 return (
29 <AlertDialogPrimitive.Backdrop
30 forceRender
31 className={cn(DIALOG_BACKDROP_CLASS, className)}
32 data-slot="alert-dialog-backdrop"
33 {...props}
34 />
35 );
37​
38function AlertDialogViewport({ className, ...props }: AlertDialogPrimitive.Viewport.Props) {
39 return (
40 <AlertDialogPrimitive.Viewport
41 className={cn(
42 "fixed inset-0 z-50 grid grid-rows-[1fr_auto_1fr] justify-items-center p-4",
43 className,
44 )}
45 data-slot="alert-dialog-viewport"
46 {...props}
47 />
48 );
50​
51function AlertDialogPopup({
52 className,
53 bottomStickOnMobile = true,
54 portalContainer,
55 ...props
56}: AlertDialogPrimitive.Popup.Props & {
57 bottomStickOnMobile?: boolean;
58 portalContainer?: AlertDialogPrimitive.Portal.Props["container"];
59}) {
60 const { popupRef, initialFocus } = useDialogActionInitialFocus();
61 const popup = useRender({
62 ref: popupRef,
63 render: (
64 <AlertDialogPrimitive.Popup
65 className={cn(
66 DIALOG_POPUP_CLASS,
67 "row-start-2 max-h-full max-w-lg text-popover-foreground",
68 bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
69 className,
70 )}
71 data-slot="alert-dialog-popup"
72 initialFocus={initialFocus}
73 render={renderRetainedPopup}
74 {...props}
75 />
76 ),
77 });
78 return (
79 <AlertDialogPortal container={portalContainer}>
80 <AlertDialogBackdrop />
81 <AlertDialogViewport
82 className={cn(bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12")}
83 >
84 {popup}
85 </AlertDialogViewport>
86 </AlertDialogPortal>
87 );
89​
90function AlertDialogHeader({ className, ...props }: React.ComponentProps<"div">) {
91 return (
92 <div
93 className={cn(
94 "flex min-h-0 flex-col gap-2 p-6 text-center max-sm:pb-4 sm:text-left",
95 className,
96 )}
97 data-slot="alert-dialog-header"
98 {...props}
99 />
100 );
102​
103function AlertDialogFooter({
104 className,
105 variant = "default",
106 ...props
107}: React.ComponentProps<"div"> & {
108 variant?: "default" | "bare";
109}) {
110 return (
111 <div
112 className={cn(
113 "flex flex-col-reverse gap-2 px-6 sm:flex-row sm:justify-end sm:rounded-b-[calc(var(--radius-2xl)-1px)]",
114 variant === "default" && "border-t bg-muted/72 py-4",
115 variant === "bare" && "pb-6",
116 className,
117 )}
118 data-slot="alert-dialog-footer"
119 {...props}
120 />
121 );
123​
124function AlertDialogTitle({ className, ...props }: AlertDialogPrimitive.Title.Props) {
125 return (
126 <AlertDialogPrimitive.Title
127 className={cn("wrap-anywhere shrink-0 font-semibold text-xl leading-snug", className)}
128 data-slot="alert-dialog-title"
129 {...props}
130 />
131 );
133​
134function AlertDialogDescription({ className, ...props }: AlertDialogPrimitive.Description.Props) {
135 return (
136 <AlertDialogPrimitive.Description
137 className={cn(
138 "wrap-anywhere max-h-[min(24rem,60dvh)] min-h-0 overflow-y-auto overscroll-contain text-muted-foreground text-sm select-text",
139 className,
140 )}
141 data-slot="alert-dialog-description"
142 {...props}
143 />
144 );
146​
147function AlertDialogClose(props: AlertDialogPrimitive.Close.Props) {
148 return <AlertDialogPrimitive.Close data-slot="alert-dialog-close" {...props} />;
150​
151export {
152 AlertDialog,
153 AlertDialogPortal,
154 AlertDialogBackdrop,
155 AlertDialogBackdrop as AlertDialogOverlay,
156 AlertDialogPopup,
157 AlertDialogPopup as AlertDialogContent,
158 AlertDialogHeader,
159 AlertDialogFooter,
160 AlertDialogTitle,
161 AlertDialogDescription,
162 AlertDialogClose,
163 DialogAction as AlertDialogAction,
164 AlertDialogViewport,
165};