|
| 1 | +import React from "react" |
| 2 | +import { CheckCircle2, AlertTriangle, Info, XCircle, X } from "lucide-react" |
| 3 | +import { cn } from "@/lib/utils" |
| 4 | + |
| 5 | +type AlertType = "success" | "warning" | "error" | "info" |
| 6 | + |
| 7 | +type GlobalAlertProps = { |
| 8 | + type: AlertType |
| 9 | + title?: string |
| 10 | + message: string |
| 11 | + onClose?: () => void |
| 12 | +} |
| 13 | + |
| 14 | +const typeStyles: Record<AlertType, string> = { |
| 15 | + success: |
| 16 | + "border-emerald-500/40 bg-emerald-900/70 text-emerald-50 shadow-emerald-500/30", |
| 17 | + warning: |
| 18 | + "border-amber-400/50 bg-amber-900/80 text-amber-50 shadow-amber-400/30", |
| 19 | + error: |
| 20 | + "border-rose-500/50 bg-rose-950/80 text-rose-50 shadow-rose-500/40", |
| 21 | + info: |
| 22 | + "border-sky-500/50 bg-sky-950/80 text-sky-50 shadow-sky-500/40", |
| 23 | +} |
| 24 | + |
| 25 | +const typeIcon: Record<AlertType, React.ComponentType<{ className?: string }>> = |
| 26 | + { |
| 27 | + success: CheckCircle2, |
| 28 | + warning: AlertTriangle, |
| 29 | + error: XCircle, |
| 30 | + info: Info, |
| 31 | + } |
| 32 | + |
| 33 | +export const GlobalAlert = ({ |
| 34 | + type, |
| 35 | + title, |
| 36 | + message, |
| 37 | + onClose, |
| 38 | +}: GlobalAlertProps) => { |
| 39 | + const Icon = typeIcon[type] |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="pointer-events-auto fixed top-4 right-4 z-40 flex max-w-sm flex-col gap-3"> |
| 43 | + <div |
| 44 | + className={cn( |
| 45 | + "relative overflow-hidden rounded-2xl border px-4 py-3 shadow-2xl backdrop-blur-xl", |
| 46 | + "bg-background/80 dark:bg-slate-950/80", |
| 47 | + "transition-transform transition-opacity duration-200 ease-out", |
| 48 | + typeStyles[type] |
| 49 | + )} |
| 50 | + role="status" |
| 51 | + aria-live="polite" |
| 52 | + > |
| 53 | + <div className="pointer-events-none absolute inset-0 bg-gradient-to-br from-white/5 via-white/0 to-white/10" /> |
| 54 | + <div className="pointer-events-none absolute -inset-x-24 -top-24 h-40 bg-gradient-to-br from-white/10 via-transparent to-white/0 blur-2xl" /> |
| 55 | + <div className="relative flex items-start gap-3"> |
| 56 | + <div className="mt-0.5 flex h-6 w-6 items-center justify-center rounded-full bg-black/20"> |
| 57 | + <Icon className="h-4 w-4" /> |
| 58 | + </div> |
| 59 | + <div className="flex-1"> |
| 60 | + {title && ( |
| 61 | + <div className="text-sm font-semibold leading-snug"> |
| 62 | + {title} |
| 63 | + </div> |
| 64 | + )} |
| 65 | + <div className="text-sm leading-snug text-white/90 dark:text-slate-100"> |
| 66 | + {message} |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + {onClose && ( |
| 70 | + <button |
| 71 | + type="button" |
| 72 | + onClick={onClose} |
| 73 | + className="ml-2 inline-flex h-6 w-6 items-center justify-center rounded-full border border-white/20 bg-black/20 text-white/80 transition hover:bg-black/40 focus:outline-none focus:ring-2 focus:ring-white/60 focus:ring-offset-2 focus:ring-offset-slate-950" |
| 74 | + aria-label="Dismiss alert" |
| 75 | + > |
| 76 | + <X className="h-3 w-3" /> |
| 77 | + </button> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + ) |
| 83 | +} |
| 84 | + |
0 commit comments