1"use client"
2
3import React, {
4 createContext,
5 useContext,
6 useEffect,
7 useId,
8 useRef,
9 useState,
10} from "react"
11import { X } from "lucide-react"
12import { AnimatePresence, motion, MotionConfig } from "motion/react"
13
14import { cn } from "@/lib/utils"
15
16const TRANSITION = {
17 type: "spring" as const,
18 bounce: 0.05,
19 duration: 0.3,
20}
21
22function useClickOutside(
23 ref: React.RefObject<HTMLElement | null>,
24 handler: () => void
25) {
26 useEffect(() => {
27 const handleClickOutside = (event: MouseEvent) => {
28 if (ref.current && !ref.current.contains(event.target as Node)) {
29 handler()
30 }
31 }
32
33 document.addEventListener("mousedown", handleClickOutside)
34 return () => {
35 document.removeEventListener("mousedown", handleClickOutside)
36 }
37 }, [ref, handler])
38}
39
40interface PopoverContextType {
41 isOpen: boolean
42 openPopover: () => void
43 closePopover: () => void
44 uniqueId: string
45 note: string
46 setNote: (note: string) => void
47}
48
49const PopoverContext = createContext<PopoverContextType | undefined>(undefined)
50
51function usePopover() {
52 const context = useContext(PopoverContext)
53 if (!context) {
54 throw new Error("usePopover must be used within a PopoverProvider")
55 }
56 return context
57}
58
59function usePopoverLogic() {
60 const uniqueId = useId()
61 const [isOpen, setIsOpen] = useState(false)
62 const [note, setNote] = useState("")
63
64 const openPopover = () => setIsOpen(true)
65 const closePopover = () => {
66 setIsOpen(false)
67 setNote("")
68 }
69
70 return { isOpen, openPopover, closePopover, uniqueId, note, setNote }
71}
72
73interface PopoverRootProps {
74 children: React.ReactNode
75 className?: string
76}
77
78export function PopoverRoot({ children, className }: PopoverRootProps) {
79 const popoverLogic = usePopoverLogic()
80
81 return (
82 <PopoverContext.Provider value={popoverLogic}>
83 <MotionConfig transition={TRANSITION}>
84 <div
85 className={cn(
86 "relative flex items-center justify-center isolate",
87 className
88 )}
89 >
90 {children}
91 </div>
92 </MotionConfig>
93 </PopoverContext.Provider>
94 )
95}
96
97interface PopoverTriggerProps {
98 children: React.ReactNode
99 className?: string
100}
101
102export function PopoverTrigger({ children, className }: PopoverTriggerProps) {
103 const { openPopover, uniqueId } = usePopover()
104
105 return (
106 <motion.button
107 key="button"
108 layoutId={`popover-${uniqueId}`}
109 className={cn(
110 "flex h-9 items-center border border-zinc-950/10 bg-white px-3 text-zinc-950 dark:border-zinc-50/10 dark:bg-zinc-700 dark:text-zinc-50",
111 className
112 )}
113 style={{
114 borderRadius: 8,
115 }}
116 onClick={openPopover}
117 >
118 <motion.span layoutId={`popover-label-${uniqueId}`} className="text-sm">
119 {children}
120 </motion.span>
121 </motion.button>
122 )
123}
124
125interface PopoverContentProps {
126 children: React.ReactNode
127 className?: string
128}
129
130export function PopoverContent({ children, className }: PopoverContentProps) {
131 const { isOpen, closePopover, uniqueId } = usePopover()
132 const formContainerRef = useRef<HTMLDivElement>(null)
133
134 useClickOutside(formContainerRef, closePopover)
135
136 useEffect(() => {
137 const handleKeyDown = (event: KeyboardEvent) => {
138 if (event.key === "Escape") {
139 closePopover()
140 }
141 }
142
143 document.addEventListener("keydown", handleKeyDown)
144
145 return () => {
146 document.removeEventListener("keydown", handleKeyDown)
147 }
148 }, [closePopover])
149
150 return (
151 <AnimatePresence>
152 {isOpen && (
153 <motion.div
154 ref={formContainerRef}
155 layoutId={`popover-${uniqueId}`}
156 className={cn(
157 "absolute h-[200px] w-[364px] overflow-hidden border border-zinc-950/10 bg-white outline-none dark:bg-zinc-700 z-50", // Changed z-90 to z-50
158 className
159 )}
160 style={{
161 borderRadius: 12,
162 top: "auto", // Remove any top positioning
163 left: "auto", // Remove any left positioning
164 transform: "none", // Remove any transform
165 }}
166 >
167 {children}
168 </motion.div>
169 )}
170 </AnimatePresence>
171 )
172}
173
174interface PopoverFormProps {
175 children: React.ReactNode
176 onSubmit?: (note: string) => void
177 className?: string
178}
179
180export function PopoverForm({
181 children,
182 onSubmit,
183 className,
184}: PopoverFormProps) {
185 const { note, closePopover } = usePopover()
186
187 const handleSubmit = (e: React.FormEvent) => {
188 e.preventDefault()
189 onSubmit?.(note)
190 closePopover()
191 }
192
193 return (
194 <form
195 className={cn("flex h-full flex-col", className)}
196 onSubmit={handleSubmit}
197 >
198 {children}
199 </form>
200 )
201}
202
203interface PopoverLabelProps {
204 children: React.ReactNode
205 className?: string
206}
207
208export function PopoverLabel({ children, className }: PopoverLabelProps) {
209 const { uniqueId, note } = usePopover()
210
211 return (
212 <motion.span
213 layoutId={`popover-label-${uniqueId}`}
214 aria-hidden="true"
215 style={{
216 opacity: note ? 0 : 1,
217 }}
218 className={cn(
219 "absolute left-4 top-3 select-none text-sm text-zinc-500 dark:text-zinc-400",
220 className
221 )}
222 >
223 {children}
224 </motion.span>
225 )
226}
227
228interface PopoverTextareaProps {
229 className?: string
230}
231
232export function PopoverTextarea({ className }: PopoverTextareaProps) {
233 const { note, setNote } = usePopover()
234
235 return (
236 <textarea
237 className={cn(
238 "h-full w-full resize-none rounded-md bg-transparent px-4 py-3 text-sm outline-none",
239 className
240 )}
241 autoFocus
242 value={note}
243 onChange={(e) => setNote(e.target.value)}
244 />
245 )
246}
247
248interface PopoverFooterProps {
249 children: React.ReactNode
250 className?: string
251}
252
253export function PopoverFooter({ children, className }: PopoverFooterProps) {
254 return (
255 <div
256 key="close"
257 className={cn("flex justify-between px-4 py-3", className)}
258 >
259 {children}
260 </div>
261 )
262}
263
264interface PopoverCloseButtonProps {
265 className?: string
266}
267
268export function PopoverCloseButton({ className }: PopoverCloseButtonProps) {
269 const { closePopover } = usePopover()
270
271 return (
272 <button
273 type="button"
274 className={cn("flex items-center", className)}
275 onClick={closePopover}
276 aria-label="Close popover"
277 >
278 <X size={16} className="text-zinc-900 dark:text-zinc-100" />
279 </button>
280 )
281}
282
283interface PopoverSubmitButtonProps {
284 className?: string
285}
286
287export function PopoverSubmitButton({ className }: PopoverSubmitButtonProps) {
288 return (
289 <button
290 className={cn(
291 "relative ml-1 flex h-8 shrink-0 scale-100 select-none appearance-none items-center justify-center rounded-lg border border-zinc-950/10 bg-transparent px-2 text-sm text-zinc-500 transition-colors hover:bg-zinc-100 hover:text-zinc-800 focus-visible:ring-2 active:scale-[0.98] dark:border-zinc-50/10 dark:text-zinc-50 dark:hover:bg-zinc-800",
292 className
293 )}
294 type="submit"
295 aria-label="Submit note"
296 >
297 Submit
298 </button>
299 )
300}
301
302export function PopoverHeader({
303 children,
304 className,
305}: {
306 children: React.ReactNode
307 className?: string
308}) {
309 return (
310 <div
311 className={cn(
312 "px-4 py-2 font-semibold text-zinc-900 dark:text-zinc-100",
313 className
314 )}
315 >
316 {children}
317 </div>
318 )
319}
320
321export function PopoverBody({
322 children,
323 className,
324}: {
325 children: React.ReactNode
326 className?: string
327}) {
328 return <div className={cn("p-4", className)}>{children}</div>
329}
330
331// New component: PopoverButton
332export function PopoverButton({
333 children,
334 onClick,
335 className,
336}: {
337 children: React.ReactNode
338 onClick?: () => void
339 className?: string
340}) {
341 return (
342 <button
343 className={cn(
344 "flex w-full items-center gap-2 rounded-md px-4 py-2 text-left text-sm hover:bg-zinc-100 dark:hover:bg-zinc-700",
345 className
346 )}
347 onClick={onClick}
348 >
349 {children}
350 </button>
351 )
352}
353