1"use client"
2
3import { ReactNode, RefObject, useEffect, useRef } from "react"
4import { ChevronUp, Loader } from "lucide-react"
5import { AnimatePresence, motion } from "motion/react"
6
7type PopoverFormProps = {
8 open: boolean
9 setOpen: (open: boolean) => void
10 openChild?: ReactNode
11 successChild?: ReactNode
12 showSuccess: boolean
13 width?: string
14 height?: string
15 showCloseButton?: boolean
16 title: string
17}
18
19export function PopoverForm({
20 open,
21 setOpen,
22 openChild,
23 showSuccess,
24 successChild,
25 width = "364px",
26 height = "192px",
27 title = "Feedback",
28 showCloseButton = false,
29}: PopoverFormProps) {
30 const ref = useRef<HTMLDivElement>(null)
31 useClickOutside(ref, () => setOpen(false))
32
33 return (
34 <div
35 key={title}
36 className="flex min-h-[300px] w-full items-center justify-center"
37 >
38 <motion.button
39 layoutId={`${title}-wrapper`}
40 onClick={() => setOpen(true)}
41 style={{ borderRadius: 8 }}
42 className="flex h-9 items-center border bg-white dark:bg-[#121212] px-3 text-sm font-medium outline-none"
43 >
44 <motion.span layoutId={`${title}-title`}>{title}</motion.span>
45 </motion.button>
46 <AnimatePresence>
47 {open && (
48 <motion.div
49 layoutId={`${title}-wrapper`}
50 className="absolute p-1 overflow-hidden bg-muted shadow-[0_0_0_1px_rgba(0,0,0,0.08),0px_1px_2px_rgba(0,0,0,0.04)] outline-none"
51 ref={ref}
52 style={{ borderRadius: 10, width, height }}
53 >
54 <motion.span
55 aria-hidden
56 className="absolute left-4 top-[17px] text-sm text-muted-foreground data-[success]:text-transparent"
57 layoutId={`${title}-title`}
58 data-success={showSuccess}
59 >
60 {title}
61 </motion.span>
62
63 {showCloseButton && (
64 <div className="absolute -top-[5px] left-1/2 transform -translate-x-1/2 w-[12px] h-[26px] flex items-center justify-center z-20">
65 <button
66 onClick={() => setOpen(false)}
67 className="absolute z-10 -mt-1 flex items-center justify-center w-[10px] h-[6px] text-muted-foreground hover:text-foreground focus:outline-none rounded-full "
68 aria-label="Close"
69 >
70 <ChevronUp className="text-muted-foreground/80" />
71 </button>
72
73 <PopoverFormCutOutTopIcon />
74 </div>
75 )}
76
77 <AnimatePresence mode="popLayout">
78 {showSuccess ? (
79 <motion.div
80 key="success"
81 initial={{ y: -32, opacity: 0, filter: "blur(4px)" }}
82 animate={{ y: 0, opacity: 1, filter: "blur(0px)" }}
83 transition={{
84 type: "spring" as const,
85 duration: 0.4,
86 bounce: 0,
87 }}
88 className="flex h-full flex-col items-center justify-center"
89 >
90 {successChild || <PopoverFormSuccess />}
91 </motion.div>
92 ) : (
93 <motion.div
94 exit={{ y: 8, opacity: 0, filter: "blur(4px)" }}
95 transition={{
96 type: "spring" as const,
97 duration: 0.4,
98 bounce: 0,
99 }}
100 key="open-child"
101 style={{ borderRadius: 10 }}
102 className="h-full border bg-white dark:bg-[#121212] z-20 "
103 >
104 {openChild}
105 </motion.div>
106 )}
107 </AnimatePresence>
108 </motion.div>
109 )}
110 </AnimatePresence>
111 </div>
112 )
113}
114
115export function PopoverFormButton({
116 loading,
117 text = "submit",
118}: {
119 loading: boolean
120 text: string
121}) {
122 return (
123 <button
124 type="submit"
125 className="ml-auto flex h-6 w-26 items-center justify-center overflow-hidden rounded-md bg-gradient-to-b from-primary/90 to-primary px-3 text-xs font-semibold text-primary-foreground shadow-[0_0_1px_1px_rgba(255,255,255,0.08)_inset,0_1px_1.5px_0_rgba(0,0,0,0.32),0_0_0_0.5px_#1a94ff]"
126 >
127 <AnimatePresence mode="popLayout" initial={false}>
128 <motion.span
129 key={`${loading}`}
130 initial={{ opacity: 0, y: -25 }}
131 animate={{ opacity: 1, y: 0 }}
132 exit={{ opacity: 0, y: 25 }}
133 transition={{
134 type: "spring",
135 duration: 0.3,
136 bounce: 0,
137 }}
138 className="flex w-full items-center justify-center"
139 >
140 {loading ? (
141 <Loader className="animate-spin size-3" />
142 ) : (
143 <span>{text}</span>
144 )}
145 </motion.span>
146 </AnimatePresence>
147 </button>
148 )
149}
150
151const useClickOutside = (
152 ref: RefObject<HTMLElement | null>,
153 handleOnClickOutside: (event: MouseEvent | TouchEvent) => void
154) => {
155 useEffect(() => {
156 const listener = (event: MouseEvent | TouchEvent) => {
157 if (!ref.current || ref.current.contains(event.target as Node)) {
158 return
159 }
160 handleOnClickOutside(event)
161 }
162 document.addEventListener("mousedown", listener)
163 document.addEventListener("touchstart", listener)
164 return () => {
165 document.removeEventListener("mousedown", listener)
166 document.removeEventListener("touchstart", listener)
167 }
168 }, [ref, handleOnClickOutside])
169}
170
171export function PopoverFormSuccess({
172 title = "Success",
173 description = "Thank you for your submission",
174}) {
175 return (
176 <>
177 <svg
178 width="32"
179 height="32"
180 viewBox="0 0 32 32"
181 fill="none"
182 xmlns="http://www.w3.org/2000/svg"
183 className="-mt-1"
184 >
185 <path
186 d="M27.6 16C27.6 17.5234 27.3 19.0318 26.717 20.4392C26.1341 21.8465 25.2796 23.1253 24.2025 24.2025C23.1253 25.2796 21.8465 26.1341 20.4392 26.717C19.0318 27.3 17.5234 27.6 16 27.6C14.4767 27.6 12.9683 27.3 11.5609 26.717C10.1535 26.1341 8.87475 25.2796 7.79759 24.2025C6.72043 23.1253 5.86598 21.8465 5.28302 20.4392C4.70007 19.0318 4.40002 17.5234 4.40002 16C4.40002 12.9235 5.62216 9.97301 7.79759 7.79759C9.97301 5.62216 12.9235 4.40002 16 4.40002C19.0765 4.40002 22.027 5.62216 24.2025 7.79759C26.3779 9.97301 27.6 12.9235 27.6 16Z"
187 fill="#2090FF"
188 fillOpacity="0.16"
189 />
190 <path
191 d="M12.1334 16.9667L15.0334 19.8667L19.8667 13.1M27.6 16C27.6 17.5234 27.3 19.0318 26.717 20.4392C26.1341 21.8465 25.2796 23.1253 24.2025 24.2025C23.1253 25.2796 21.8465 26.1341 20.4392 26.717C19.0318 27.3 17.5234 27.6 16 27.6C14.4767 27.6 12.9683 27.3 11.5609 26.717C10.1535 26.1341 8.87475 25.2796 7.79759 24.2025C6.72043 23.1253 5.86598 21.8465 5.28302 20.4392C4.70007 19.0318 4.40002 17.5234 4.40002 16C4.40002 12.9235 5.62216 9.97301 7.79759 7.79759C9.97301 5.62216 12.9235 4.40002 16 4.40002C19.0765 4.40002 22.027 5.62216 24.2025 7.79759C26.3779 9.97301 27.6 12.9235 27.6 16Z"
192 stroke="#2090FF"
193 strokeWidth="2.4"
194 strokeLinecap="round"
195 strokeLinejoin="round"
196 />
197 </svg>
198 <h3 className="mb-1 mt-2 text-sm font-medium text-primary">{title}</h3>
199 <p className="text-sm text-muted-foreground max-w-xs text-pretty mx-auto text-center">
200 {description}
201 </p>
202 </>
203 )
204}
205
206export function PopoverFormSeparator({
207 width = 352,
208 height = 2,
209}: {
210 width?: number | string
211 height?: number
212}) {
213 return (
214 <svg
215 className="absolute left-0 right-0 top-[-1px]"
216 width={width}
217 height={height}
218 viewBox="0 0 352 2"
219 fill="none"
220 xmlns="http://www.w3.org/2000/svg"
221 >
222 <path d="M0 1H352" className="stroke-border" strokeDasharray="4 4" />
223 </svg>
224 )
225}
226
227function PopoverFormCutOutTopIcon({
228 width = 44,
229 height = 30,
230}: {
231 width?: number
232 height?: number
233}) {
234 const aspectRatio = 6 / 12
235 const calculatedHeight = width * aspectRatio
236 const calculatedWidth = height / aspectRatio
237
238 const finalWidth = Math.min(width, calculatedWidth)
239 const finalHeight = Math.min(height, calculatedHeight)
240
241 return (
242 <svg
243 width={finalWidth}
244 height={finalHeight}
245 viewBox="0 0 6 12"
246 fill="none"
247 xmlns="http://www.w3.org/2000/svg"
248 className="rotate-90 mt-[1px]"
249 preserveAspectRatio="none"
250 >
251 <g clipPath="url(#clip0_2029_22)">
252 <path
253 d="M0 2C0.656613 2 1.30679 2.10346 1.91341 2.30448C2.52005 2.5055 3.07124 2.80014 3.53554 3.17157C3.99982 3.54301 4.36812 3.98396 4.6194 4.46927C4.87067 4.95457 5 5.47471 5 6C5 6.52529 4.87067 7.04543 4.6194 7.53073C4.36812 8.01604 3.99982 8.45699 3.53554 8.82843C3.07124 9.19986 2.52005 9.4945 1.91341 9.69552C1.30679 9.89654 0.656613 10 0 10V6V2Z"
254 className="fill-muted"
255 />
256 <path
257 d="M1 12V10C2.06087 10 3.07828 9.57857 3.82843 8.82843C4.57857 8.07828 5 7.06087 5 6C5 4.93913 4.57857 3.92172 3.82843 3.17157C3.07828 2.42143 2.06087 2 1 2V0"
258 className="stroke-border"
259 strokeWidth={0.6}
260 strokeLinejoin="round"
261 />
262 </g>
263 <defs>
264 <clipPath id="clip0_2029_22">
265 <rect width={finalWidth} height={finalHeight} fill="white" />
266 </clipPath>
267 </defs>
268 </svg>
269 )
270}
271
272export function PopoverFormCutOutLeftIcon() {
273 return (
274 <svg
275 width="6"
276 height="12"
277 viewBox="0 0 6 12"
278 fill="none"
279 xmlns="http://www.w3.org/2000/svg"
280 >
281 <g clipPath="url(#clip0_2029_22)">
282 <path
283 d="M0 2C0.656613 2 1.30679 2.10346 1.91341 2.30448C2.52005 2.5055 3.07124 2.80014 3.53554 3.17157C3.99982 3.54301 4.36812 3.98396 4.6194 4.46927C4.87067 4.95457 5 5.47471 5 6C5 6.52529 4.87067 7.04543 4.6194 7.53073C4.36812 8.01604 3.99982 8.45699 3.53554 8.82843C3.07124 9.19986 2.52005 9.4945 1.91341 9.69552C1.30679 9.89654 0.656613 10 0 10V6V2Z"
284 className="fill-muted"
285 />
286 <path
287 d="M1 12V10C2.06087 10 3.07828 9.57857 3.82843 8.82843C4.57857 8.07828 5 7.06087 5 6C5 4.93913 4.57857 3.92172 3.82843 3.17157C3.07828 2.42143 2.06087 2 1 2V0"
288 className="stroke-border"
289 strokeWidth="1"
290 strokeLinejoin="round"
291 />
292 </g>
293 <defs>
294 <clipPath id="clip0_2029_22">
295 <rect width="6" height="12" fill="white" />
296 </clipPath>
297 </defs>
298 </svg>
299 )
300}
301
302export function PopoverFormCutOutRightIcon() {
303 return (
304 <svg
305 width="6"
306 height="12"
307 viewBox="0 0 6 12"
308 fill="none"
309 xmlns="http://www.w3.org/2000/svg"
310 >
311 <g clipPath="url(#clip0_2029_22)">
312 <path
313 d="M0 2C0.656613 2 1.30679 2.10346 1.91341 2.30448C2.52005 2.5055 3.07124 2.80014 3.53554 3.17157C3.99982 3.54301 4.36812 3.98396 4.6194 4.46927C4.87067 4.95457 5 5.47471 5 6C5 6.52529 4.87067 7.04543 4.6194 7.53073C4.36812 8.01604 3.99982 8.45699 3.53554 8.82843C3.07124 9.19986 2.52005 9.4945 1.91341 9.69552C1.30679 9.89654 0.656613 10 0 10V6V2Z"
314 className="fill-muted"
315 />
316 <path
317 d="M1 12V10C2.06087 10 3.07828 9.57857 3.82843 8.82843C4.57857 8.07828 5 7.06087 5 6C5 4.93913 4.57857 3.92172 3.82843 3.17157C3.07828 2.42143 2.06087 2 1 2V0"
318 className="stroke-border"
319 strokeWidth="1"
320 strokeLinejoin="round"
321 />
322 </g>
323 <defs>
324 <clipPath id="clip0_2029_22">
325 <rect width="6" height="12" fill="white" />
326 </clipPath>
327 </defs>
328 </svg>
329 )
330}
331
332export default PopoverForm
333