// src/components/ui/Toast.jsx // Toast notification system: provider, hook, and renderer. // // Usage: // 1. Wrap your app (or page) with // 2. In any child component: const { toast } = useToast() // 3. Call: toast.success('Saved!', 'Your changes have been saved.') // toast.danger('Error', 'Something went wrong.') // toast.warning('Warning', 'Disk space is low.') // toast.info('Info', 'A new version is available.') // // Each toast auto-dismisses after `duration` ms (default 4000). // ToastProvider renders the stack itself via a portal — no extra component needed. import { createContext, useContext, useState, useCallback, useRef, useEffect } from 'react' import { createPortal } from 'react-dom' import Spinner from '@/components/ui/Spinner' // --------------------------------------------------------------------------- // Context // --------------------------------------------------------------------------- const ToastContext = createContext(null) // --------------------------------------------------------------------------- // Icons (inline — no external dependency) // --------------------------------------------------------------------------- function ToastIcon({ variant }) { const props = { width: 16, height: 16, viewBox: '0 0 16 16', fill: 'none', stroke: 'currentColor', strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true, } if (variant === 'success') return ( ) if (variant === 'danger') return ( ) if (variant === 'warning') return ( ) // info return ( ) } // --------------------------------------------------------------------------- // Single Toast item // --------------------------------------------------------------------------- function ToastItem({ id, variant = 'info', title, message, duration = 4000, pending = false, onDismiss }) { const [exiting, setExiting] = useState(false) const dismiss = useCallback(() => { setExiting(true) setTimeout(() => onDismiss(id), 300) }, [id, onDismiss]) // Auto-dismiss timer — does not run while `pending` (e.g. waiting on a command reply) const timerRef = useRef(null) const startTimer = () => { if (pending) return timerRef.current = setTimeout(dismiss, duration) } const clearTimer = () => clearTimeout(timerRef.current) // (Re)start the timer whenever `pending` resolves to false, or on mount if already resolved useEffect(() => { clearTimer() startTimer() return clearTimer // eslint-disable-next-line react-hooks/exhaustive-deps }, [pending, duration]) return (
{pending ? : }
{title &&
{title}
} {message &&
{message}
}
{/* Progress bar — shrinks over duration. Hidden while pending (no timeout running). */} {!pending && (
)}
) } // --------------------------------------------------------------------------- // Provider // --------------------------------------------------------------------------- let _nextId = 1 export function ToastProvider({ children }) { const [toasts, setToasts] = useState([]) const addToast = useCallback((variant, title, message, duration, pending = false) => { const id = _nextId++ setToasts((prev) => [...prev, { id, variant, title, message, duration, pending }]) return id }, []) const dismiss = useCallback((id) => { setToasts((prev) => prev.filter((t) => t.id !== id)) }, []) // Update an existing toast in place — e.g. flip a `pending` (blue) toast to // success/danger/warning once a command reply (or timeout) arrives. const update = useCallback((id, patch) => { setToasts((prev) => prev.map((t) => (t.id === id ? { ...t, ...patch } : t))) }, []) const toast = { success: (title, message, duration) => addToast('success', title, message, duration), danger: (title, message, duration) => addToast('danger', title, message, duration), warning: (title, message, duration) => addToast('warning', title, message, duration), info: (title, message, duration) => addToast('info', title, message, duration), // pending: creates a toast with no auto-dismiss timer until update() clears `pending` pending: (title, message) => addToast('info', title, message, 4000, true), update, dismiss, } return ( {children} {createPortal(
{toasts.map((t) => ( ))}
, document.body )}
) } // --------------------------------------------------------------------------- // Hook // --------------------------------------------------------------------------- export function useToast() { const ctx = useContext(ToastContext) if (!ctx) throw new Error('useToast must be used inside ') return { toast: ctx } } // Default export — the provider (hook exported as named) export default ToastProvider