(function () {
var host = document.getElementById("toastHost");
if (!host) return;
var ICONS = {
info:
'',
success:
'',
error:
'',
warning:
'',
};
var DEFAULT_DURATION = 2400;
var MAX_VISIBLE = 3;
function normalizeType(type) {
if (type === "success" || type === "error" || type === "warning" || type === "info") {
return type;
}
return "info";
}
function showToast(message, options) {
if (!message) return null;
var opts = typeof options === "string" ? { type: options } : options || {};
var type = normalizeType(opts.type);
var duration = typeof opts.duration === "number" ? opts.duration : DEFAULT_DURATION;
while (host.children.length >= MAX_VISIBLE) {
var oldest = host.firstElementChild;
if (oldest) oldest.remove();
}
var item = document.createElement("div");
item.className = "toast-item toast-item--" + type;
item.setAttribute("role", "status");
var icon = document.createElement("span");
icon.className = "toast-icon";
icon.setAttribute("aria-hidden", "true");
icon.innerHTML = ICONS[type] || ICONS.info;
var text = document.createElement("span");
text.className = "toast-text";
text.textContent = String(message);
item.appendChild(icon);
item.appendChild(text);
host.appendChild(item);
requestAnimationFrame(function () {
item.classList.add("is-visible");
});
var hideTimer = setTimeout(function () {
item.classList.add("is-leaving");
item.classList.remove("is-visible");
setTimeout(function () {
if (item.parentNode) item.parentNode.removeChild(item);
}, 260);
}, Math.max(800, duration));
return {
close: function () {
clearTimeout(hideTimer);
if (item.parentNode) item.parentNode.removeChild(item);
},
};
}
window.showToast = showToast;
})();