Packages
Server-rendered toast notifications for Phoenix LiveView.
Retired package: Deprecated - Experimental release, please use 0.0.2 or later
Current section
Files
Jump to
Current section
Files
assets/js/toast_flash.js
// FlashToasts JavaScript Hook
// Handles converting flash messages to toasts on mount
export default {
mounted() {
const flash = this.el.dataset.flash;
if (!flash) return;
try {
const flashData = JSON.parse(flash);
// Convert flash messages to toasts
Object.entries(flashData).forEach(([type, message]) => {
if (message && message.trim() !== "") {
// Map Phoenix flash types to toast types
const toastType = this.mapFlashType(type);
// Send toast creation event to LiveComponent
this.pushEventTo("#toast-group", "flash-toast", {
type: toastType,
message: message
});
}
});
// Remove the flash container element
this.el.remove();
} catch (e) {
console.error("Failed to parse flash data:", e);
}
},
mapFlashType(flashType) {
const typeMap = {
"info": "info",
"success": "success",
"error": "error",
"warning": "warning",
"alert": "warning", // Map alert to warning
"notice": "info" // Map notice to info
};
return typeMap[flashType] || "default";
}
};