1 "use client"
2
3 import { AlertDialog as RadixAlertDialog } from "radix-ui"
4 import * as React from "react"
5
6 import { Button } from "@/components/button"
7 import { Heading } from "@/components/heading"
8 import { clx } from "@/utils/clx"
9
10 type PromptVariant = "danger" | "confirmation"
11
12 const PromptContext = React.createContext<{ variant: PromptVariant }>({
13 variant: "danger",
14 })
15
16 const usePromptContext = () => {
17 const context = React.useContext(PromptContext)
18 if (!context) {
19 throw new Error("usePromptContext must be used within a PromptProvider")
20 }
21 return context
22 }
23
24 type PromptProviderProps = React.PropsWithChildren<{
25 variant: PromptVariant
26 }>
27
28 const PromptProvider = ({ variant, children }: PromptProviderProps) => {
29 return (
30 <PromptContext.Provider value={{ variant }}>
31 {children}
32 </PromptContext.Provider>
33 )
34 }
35
36 /**
37 * This component is based on the [Radix UI Alert Dialog](https://www.radix-ui.com/primitives/docs/components/alert-dialog) primitives.
38 */
39 const Root = ({
40 /**
41 * The variant of the prompt.
42 */
43 variant = "danger",
44 ...props
45 }: React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Root> & {
46 variant?: PromptVariant
47 }) => {
48 return (
49 <PromptProvider variant={variant}>
50 <RadixAlertDialog.Root {...props} />
51 </PromptProvider>
52 )
53 }
54 Root.displayName = "Prompt"
55
56 const Trigger = RadixAlertDialog.Trigger
57 Trigger.displayName = "Prompt.Trigger"
58
59 const Portal = (props: RadixAlertDialog.AlertDialogPortalProps) => {
60 return <RadixAlertDialog.AlertDialogPortal {...props} />
61 }
62 Portal.displayName = "Prompt.Portal"
63
64 const Overlay = React.forwardRef<
65 React.ElementRef<typeof RadixAlertDialog.Overlay>,
66 React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Overlay>
67 >(({ className, ...props }, ref) => {
68 return (
69 <RadixAlertDialog.Overlay
70 ref={ref}
71 className={clx(
72 "bg-ui-bg-overlay fixed inset-0",
73 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
74 className
75 )}
76 {...props}
77 />
78 )
79 })
80 Overlay.displayName = "Prompt.Overlay"
81
82 const Title = React.forwardRef<
83 React.ElementRef<typeof RadixAlertDialog.Title>,
84 Omit<React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Title>, "asChild">
85 >(({ className, children, ...props }, ref) => {
86 return (
87 <RadixAlertDialog.Title ref={ref} className={clx(className)} {...props} asChild>
88 <Heading level="h2" className="text-ui-fg-base">
89 {children}
90 </Heading>
91 </RadixAlertDialog.Title>
92 )
93 })
94 Title.displayName = "Prompt.Title"
95
96 const Content = React.forwardRef<
97 React.ElementRef<typeof RadixAlertDialog.Content>,
98 React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Content>
99 >(({ className, ...props }, ref) => {
100 return (
101 <Portal>
102 <Overlay />
103 <RadixAlertDialog.Content
104 ref={ref}
105 className={clx(
106 "bg-ui-bg-base shadow-elevation-flyout fixed left-[50%] top-[50%] flex w-full max-w-[400px] translate-x-[-50%] translate-y-[-50%] flex-col rounded-lg border focus-visible:outline-none",
107 "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] duration-200",
108 className
109 )}
110 {...props}
111 />
112 </Portal>
113 )
114 })
115 Content.displayName = "Prompt.Content"
116
117 const Description = React.forwardRef<
118 React.ElementRef<typeof RadixAlertDialog.Description>,
119 React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Description>
120 >(({ className, ...props }, ref) => {
121 return (
122 <RadixAlertDialog.Description
123 ref={ref}
124 className={clx("text-ui-fg-subtle txt-compact-medium", className)}
125 {...props}
126 />
127 )
128 })
129 Description.displayName = "Prompt.Description"
130
131 const Action = React.forwardRef<
132 React.ElementRef<typeof RadixAlertDialog.Action>,
133 Omit<React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Action>, "asChild">
134 >(({ className, children, type, ...props }, ref) => {
135 const { variant } = usePromptContext()
136
137 return (
138 <RadixAlertDialog.Action ref={ref} className={className} {...props} asChild>
139 <Button
140 size="small"
141 type={type}
142 variant={variant === "danger" ? "danger" : "primary"}
143 >
144 {children}
145 </Button>
146 </RadixAlertDialog.Action>
147 )
148 })
149 Action.displayName = "Prompt.Action"
150
151 const Cancel = React.forwardRef<
152 React.ElementRef<typeof RadixAlertDialog.Cancel>,
153 Omit<React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Cancel>, "asChild">
154 >(({ className, children, ...props }, ref) => {
155 return (
156 <RadixAlertDialog.Cancel ref={ref} className={clx(className)} {...props} asChild>
157 <Button size="small" variant="secondary">
158 {children}
159 </Button>
160 </RadixAlertDialog.Cancel>
161 )
162 })
163 Cancel.displayName = "Prompt.Cancel"
164
165 /**
166 * This component is based on the `div` element and supports all of its props
167 */
168 const Header = ({
169 className,
170 ...props
171 }: React.HTMLAttributes<HTMLDivElement>) => {
172 return (
173 <div
174 className={clx("flex flex-col gap-y-1 px-6 pt-6", className)}
175 {...props}
176 />
177 )
178 }
179 Header.displayName = "Prompt.Header"
180
181 /**
182 * This component is based on the `div` element and supports all of its props
183 */
184 const Footer = ({
185 className,
186 ...props
187 }: React.HTMLAttributes<HTMLDivElement>) => {
188 return (
189 <div
190 className={clx("flex items-center justify-end gap-x-2 p-6", className)}
191 {...props}
192 />
193 )
194 }
195 Footer.displayName = "Prompt.Footer"
196
197 const Prompt = Object.assign(Root, {
198 Trigger,
199 Content,
200 Title,
201 Description,
202 Action,
203 Cancel,
204 Header,
205 Footer,
206 })
207
208 export { Prompt }