import * as React from "react" import { View } from "@tarojs/components" import { type VariantProps } from "class-variance-authority" import { cn } from "@/lib/utils" import { buttonVariants } from "@/components/ui/button" import { Portal } from "@/components/ui/portal" import { useKeyboardOffset } from "@/lib/hooks/use-keyboard-offset" const AlertDialogContext = React.createContext<{ open?: boolean onOpenChange?: (open: boolean) => void } | null>(null) const usePresence = (open: boolean | undefined, durationMs: number) => { const [present, setPresent] = React.useState(!!open) const timeoutRef = React.useRef | null>(null) React.useEffect(() => { if (open) { if (timeoutRef.current) clearTimeout(timeoutRef.current) timeoutRef.current = null setPresent(true) return } timeoutRef.current = setTimeout(() => setPresent(false), durationMs) return () => { if (timeoutRef.current) clearTimeout(timeoutRef.current) timeoutRef.current = null } }, [open, durationMs]) return present } const AlertDialog = ({ children, open: openProp, defaultOpen = false, onOpenChange }: { children: React.ReactNode, open?: boolean, defaultOpen?: boolean, onOpenChange?: (open: boolean) => void }) => { const [openState, setOpenState] = React.useState(defaultOpen || false) const open = openProp !== undefined ? openProp : openState const handleOpenChange = (newOpen: boolean) => { if (openProp === undefined) { setOpenState(newOpen) } onOpenChange?.(newOpen) } return ( {children} ) } const AlertDialogTrigger = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, ...props }, ref) => { const context = React.useContext(AlertDialogContext) return ( { e.stopPropagation() context?.onOpenChange?.(true) }} {...props} > {children} ) }) AlertDialogTrigger.displayName = "AlertDialogTrigger" const AlertDialogPortal = ({ children }) => { const context = React.useContext(AlertDialogContext) const present = usePresence(context?.open, 200) if (!present) return null return {children} } const AlertDialogOverlay = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => { const context = React.useContext(AlertDialogContext) const state = context?.open ? "open" : "closed" return ( { e.stopPropagation() // Unlike Dialog, AlertDialog typically forces explicit action/cancel. // But user might want it to close on overlay click. // Standard shadcn/radix alert dialog usually does NOT close on overlay click? // Radix Alert Dialog does NOT close on overlay click by default. // We will leave it as is (no close on click) or optional? // For now, let's follow standard pattern: it blocks interaction. }} {...props} /> ) }) AlertDialogOverlay.displayName = "AlertDialogOverlay" const AlertDialogContent = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, children, style, ...props }, ref) => { const context = React.useContext(AlertDialogContext) const offset = useKeyboardOffset() const state = context?.open ? "open" : "closed" return ( 0 ? `calc(50% - ${offset / 2}px)` : undefined }} onClick={(e) => e.stopPropagation()} {...props} > {children} ) }) AlertDialogContent.displayName = "AlertDialogContent" const AlertDialogHeader = ({ className, ...props }: React.ComponentPropsWithoutRef) => ( ) AlertDialogHeader.displayName = "AlertDialogHeader" const AlertDialogFooter = ({ className, ...props }: React.ComponentPropsWithoutRef) => ( ) AlertDialogFooter.displayName = "AlertDialogFooter" const AlertDialogTitle = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) AlertDialogTitle.displayName = "AlertDialogTitle" const AlertDialogDescription = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )) AlertDialogDescription.displayName = "AlertDialogDescription" const AlertDialogAction = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & VariantProps >(({ className, variant, size, onClick, ...props }, ref) => { const context = React.useContext(AlertDialogContext) return ( { context?.onOpenChange?.(false) onClick?.(e) }} {...props} /> ) }) AlertDialogAction.displayName = "AlertDialogAction" const AlertDialogCancel = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & VariantProps >(({ className, variant = "outline", size, onClick, ...props }, ref) => { const context = React.useContext(AlertDialogContext) return ( { context?.onOpenChange?.(false) onClick?.(e) }} {...props} /> ) }) AlertDialogCancel.displayName = "AlertDialogCancel" export { AlertDialog, AlertDialogPortal, AlertDialogOverlay, AlertDialogTrigger, AlertDialogContent, AlertDialogHeader, AlertDialogFooter, AlertDialogTitle, AlertDialogDescription, AlertDialogAction, AlertDialogCancel, }