|
| 1 | +import React, { useCallback, useEffect, useState } from 'react' |
| 2 | +import type { LucideIcon } from 'lucide-react' |
| 3 | +import { X, Info, AlertTriangle, AlertCircle } from 'lucide-react' |
| 4 | +import { Icon } from './Icon' |
| 5 | +import { ToastContext } from './toastContext' |
| 6 | +import type { Toast, ToastOptions, ToastVariant } from './toastContext' |
| 7 | + |
| 8 | +// Computed once at module load -- safe to use during render |
| 9 | +const PREFERS_REDUCED_MOTION = |
| 10 | + typeof window !== 'undefined' && |
| 11 | + typeof window.matchMedia === 'function' && |
| 12 | + window.matchMedia('(prefers-reduced-motion: reduce)').matches |
| 13 | + |
| 14 | +// ── Provider ────────────────────────────────────────────────────────────────── |
| 15 | + |
| 16 | +export function ToastProvider({ children }: { children: React.ReactNode }) { |
| 17 | + const [toasts, setToasts] = useState<Toast[]>([]) |
| 18 | + |
| 19 | + const removeToast = useCallback((id: string) => { |
| 20 | + setToasts(prev => prev.filter(t => t.id !== id)) |
| 21 | + }, []) |
| 22 | + |
| 23 | + const addToast = useCallback((message: string, options: ToastOptions = {}): string => { |
| 24 | + const id = crypto.randomUUID() |
| 25 | + const toast: Toast = { |
| 26 | + id, |
| 27 | + message, |
| 28 | + variant: options.variant ?? 'info', |
| 29 | + durationMs: options.durationMs ?? 5000, |
| 30 | + icon: options.icon, |
| 31 | + } |
| 32 | + setToasts(prev => [...prev, toast]) |
| 33 | + return id |
| 34 | + }, []) |
| 35 | + |
| 36 | + return ( |
| 37 | + <ToastContext.Provider value={{ addToast, removeToast }}> |
| 38 | + {children} |
| 39 | + <ToastStack toasts={toasts} onRemove={removeToast} /> |
| 40 | + </ToastContext.Provider> |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +// ── Stack ───────────────────────────────────────────────────────────────────── |
| 45 | + |
| 46 | +function ToastStack({ toasts, onRemove }: { toasts: Toast[]; onRemove: (id: string) => void }) { |
| 47 | + if (toasts.length === 0) return null |
| 48 | + |
| 49 | + return ( |
| 50 | + <div |
| 51 | + aria-live="polite" |
| 52 | + aria-label="Notifications" |
| 53 | + className="fixed top-4 right-4 z-50 flex flex-col gap-2 pointer-events-none" |
| 54 | + style={{ maxWidth: '360px', width: 'calc(100vw - 2rem)' }} |
| 55 | + > |
| 56 | + {toasts.map(toast => ( |
| 57 | + <ToastItem key={toast.id} toast={toast} onRemove={onRemove} /> |
| 58 | + ))} |
| 59 | + </div> |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +// ── Item ────────────────────────────────────────────────────────────────────── |
| 64 | + |
| 65 | +const VARIANT_STYLES: Record<ToastVariant, { border: string; iconColor: string }> = { |
| 66 | + info: { border: 'var(--color-gold)', iconColor: 'var(--color-gold)' }, |
| 67 | + warning: { border: 'var(--color-amber, #f59e0b)', iconColor: 'var(--color-amber, #f59e0b)' }, |
| 68 | + error: { border: 'var(--color-red)', iconColor: 'var(--color-red)' }, |
| 69 | +} |
| 70 | + |
| 71 | +const VARIANT_ICONS: Record<ToastVariant, LucideIcon> = { |
| 72 | + info: Info, |
| 73 | + warning: AlertTriangle, |
| 74 | + error: AlertCircle, |
| 75 | +} |
| 76 | + |
| 77 | +function ToastItem({ toast, onRemove }: { toast: Toast; onRemove: (id: string) => void }) { |
| 78 | + const [visible, setVisible] = useState(false) |
| 79 | + const [leaving, setLeaving] = useState(false) |
| 80 | + const reduced = PREFERS_REDUCED_MOTION |
| 81 | + |
| 82 | + // One-frame delay so the initial hidden state paints before the enter transition |
| 83 | + useEffect(() => { |
| 84 | + const frame = requestAnimationFrame(() => setVisible(true)) |
| 85 | + return () => cancelAnimationFrame(frame) |
| 86 | + }, []) |
| 87 | + |
| 88 | + const dismiss = useCallback(() => { |
| 89 | + if (reduced) { |
| 90 | + onRemove(toast.id) |
| 91 | + return |
| 92 | + } |
| 93 | + setLeaving(true) |
| 94 | + setTimeout(() => onRemove(toast.id), 200) |
| 95 | + }, [onRemove, toast.id, reduced]) |
| 96 | + |
| 97 | + useEffect(() => { |
| 98 | + if (toast.durationMs === 0) return |
| 99 | + const timer = setTimeout(dismiss, toast.durationMs) |
| 100 | + return () => clearTimeout(timer) |
| 101 | + }, [toast.durationMs, dismiss]) |
| 102 | + |
| 103 | + const styles = VARIANT_STYLES[toast.variant] |
| 104 | + const DefaultIcon = VARIANT_ICONS[toast.variant] |
| 105 | + const opacity = reduced ? 1 : visible && !leaving ? 1 : 0 |
| 106 | + const translateY = reduced ? '0px' : visible && !leaving ? '0px' : '-8px' |
| 107 | + |
| 108 | + return ( |
| 109 | + <div |
| 110 | + role={toast.variant === 'error' ? 'alert' : 'status'} |
| 111 | + aria-live={toast.variant === 'error' ? 'assertive' : 'polite'} |
| 112 | + className="pointer-events-auto flex items-start gap-3 rounded-lg px-3 py-3 shadow-lg border" |
| 113 | + style={{ |
| 114 | + background: 'var(--color-surface)', |
| 115 | + borderColor: styles.border, |
| 116 | + borderLeftWidth: '3px', |
| 117 | + opacity, |
| 118 | + transform: `translateY(${translateY})`, |
| 119 | + transition: reduced ? 'none' : 'opacity 0.18s ease, transform 0.18s ease', |
| 120 | + boxShadow: '0 4px 16px rgba(0,0,0,0.14)', |
| 121 | + }} |
| 122 | + > |
| 123 | + <span className="mt-0.5 shrink-0" style={{ color: styles.iconColor }}> |
| 124 | + {toast.icon ?? <Icon icon={DefaultIcon} size="sm" />} |
| 125 | + </span> |
| 126 | + <span className="flex-1 text-sm leading-snug" style={{ color: 'var(--color-ink)' }}> |
| 127 | + {toast.message} |
| 128 | + </span> |
| 129 | + <button |
| 130 | + onClick={dismiss} |
| 131 | + aria-label="Dismiss notification" |
| 132 | + className="shrink-0 rounded transition-colors hover:bg-black/10 p-0.5 -mt-0.5 -mr-0.5" |
| 133 | + style={{ color: 'var(--color-muted)' }} |
| 134 | + > |
| 135 | + <Icon icon={X} size="sm" /> |
| 136 | + </button> |
| 137 | + </div> |
| 138 | + ) |
| 139 | +} |
0 commit comments