UNPKG

react-universal-toast

Version:

A lightweight, customizable toast notification system for React applications with TypeScript support.

37 lines (36 loc) 1.02 kB
class ToastStore { constructor() { this.toasts = []; this.listeners = []; } get currentToasts() { return [...this.toasts]; } subscribe(listener) { this.listeners.push(listener); listener([...this.toasts]); return () => { this.listeners = this.listeners.filter((l) => l !== listener); }; } notify() { this.listeners.forEach((l) => l([...this.toasts])); } show(message, opts) { var _a; const id = Math.random().toString(36).slice(2); const newToast = { id, message, type: (_a = opts === null || opts === void 0 ? void 0 : opts.type) !== null && _a !== void 0 ? _a : "default" }; this.toasts.push(newToast); this.notify(); return id; } remove(id) { this.toasts = this.toasts.filter((t) => t.id !== id); this.notify(); } clear() { this.toasts = []; this.notify(); } } export const toastStore = new ToastStore();