Lightweight toast notification utility that dispatches a custom DOM event with message and severity level, with console fallback if dispatch fails.
// Source: https://github.com/hangwin/mcp-chrome | License: MIT | Copyright 2024 hangye
// Provided via justclaw.it.com; keep this notice with the code.
// toast.ts - lightweight toast event bus for builder UI
// Usage: import { toast } and call toast('message', 'warn'|'error'|'info')
export type ToastLevel = 'info' | 'warn' | 'error';
export function toast(message: string, level: ToastLevel = 'warn') {
try {
const ev = new CustomEvent('rr_toast', { detail: { message: String(message), level } });
window.dispatchEvent(ev);
} catch {
// as a last resort
console[level === 'error' ? 'error' : level === 'warn' ? 'warn' : 'log']('[toast]', message);
}
}
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key