@zenithui/toast
Version:
A modern, lightweight toast notification system for React applications. ZenithUI Toast provides a simple and customizable way to display notifications, alerts, and messages to users with smooth animations and flexible styling options.
37 lines (36 loc) • 1.34 kB
JavaScript
class ToastSingleton {
addToast = null;
pendingToasts = [];
register(addToast) {
this.addToast = addToast;
this.pendingToasts.forEach(({ message, type, options })=>this.addToast?.(message, type, options));
this.pendingToasts = [];
}
showToast = (message, type, options)=>{
if (this.addToast) this.addToast(message, type, options);
else this.pendingToasts.push({
message,
type,
options
});
};
success = (message, options)=>this.showToast(message, "success", options);
info = (message, options)=>this.showToast(message, "info", options);
error = (message, options)=>this.showToast(message, "error", options);
warning = (message, options)=>this.showToast(message, "warning", options);
loading = (message, options)=>this.showToast(message, "loading", options);
promise = (message, options)=>this.showToast(message, "promise", options);
}
const toastInstance = new ToastSingleton();
const toast = {
success: toastInstance.success,
info: toastInstance.info,
error: toastInstance.error,
warning: toastInstance.warning,
loading: toastInstance.loading,
promise: toastInstance.promise
};
const registerToast = (addToast)=>{
toastInstance.register(addToast);
};
export { registerToast, toast };