a2host/public/js/toast.js

129 lines
4.4 KiB
JavaScript

/**
* a2host - Toast Notification System (ES Module)
* Provee notificaciones flotantes reactivas, accesibles y con estética Glassmorphism.
*/
class ToastManager {
constructor() {
this.container = null;
this.toasts = [];
}
ensureContainer() {
if (!this.container) {
this.container = document.getElementById('toast-container');
if (!this.container) {
this.container = document.createElement('div');
this.container.id = 'toast-container';
this.container.className = 'toast-container';
this.container.setAttribute('aria-live', 'polite');
this.container.setAttribute('aria-atomic', 'true');
document.body.appendChild(this.container);
}
}
return this.container;
}
/**
* Muestra un toast
* @param {'success'|'error'|'warning'|'info'} type
* @param {string} title
* @param {string} [message]
* @param {number} [duration=5000]
*/
show(type, title, message = '', duration = 5000) {
const container = this.ensureContainer();
const toast = document.createElement('div');
toast.className = `toast toast-${type}`;
toast.setAttribute('role', 'alert');
const icons = {
success: `<svg class="toast-icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M16.7 5.3 7.5 14.5l-4.2-4.2"/></svg>`,
error: `<svg class="toast-icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M6 6l8 8m0-8-8 8"/></svg>`,
warning: `<svg class="toast-icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M10 3.5 2 16.5h16L10 3.5zM10 8.5v4M10 14.5v.5"/></svg>`,
info: `<svg class="toast-icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2"><circle cx="10" cy="10" r="7"/><path stroke-linecap="round" stroke-linejoin="round" d="M10 9v5M10 6.5v.5"/></svg>`
};
toast.innerHTML = `
<div class="toast-icon-wrapper toast-icon-${type}">
${icons[type] || icons.info}
</div>
<div class="toast-body">
<h4 class="toast-title">${escapeHtml(title)}</h4>
${message ? `<p class="toast-message">${escapeHtml(message)}</p>` : ''}
</div>
<button type="button" class="toast-close" aria-label="Cerrar notificación">
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M4 4l8 8m0-8-8 8"/></svg>
</button>
<div class="toast-progress"></div>
`;
const closeBtn = toast.querySelector('.toast-close');
const progressBar = toast.querySelector('.toast-progress');
let isClosing = false;
let timer = null;
const removeToast = () => {
if (isClosing) return;
isClosing = true;
if (timer) clearTimeout(timer);
toast.classList.add('toast-leaving');
toast.addEventListener('animationend', () => {
toast.remove();
}, { once: true });
};
closeBtn.addEventListener('click', removeToast);
// Animación de la barra de progreso si hay duración
if (duration > 0) {
if (progressBar) {
progressBar.style.animationDuration = `${duration}ms`;
}
timer = setTimeout(removeToast, duration);
// Pausar al hacer hover para accesibilidad
toast.addEventListener('mouseenter', () => {
if (timer) clearTimeout(timer);
if (progressBar) progressBar.style.animationPlayState = 'paused';
});
toast.addEventListener('mouseleave', () => {
if (progressBar) progressBar.style.animationPlayState = 'running';
timer = setTimeout(removeToast, 2000);
});
}
container.appendChild(toast);
return toast;
}
success(title, message, duration) {
return this.show('success', title, message, duration);
}
error(title, message, duration) {
return this.show('error', title, message, duration || 7000);
}
warning(title, message, duration) {
return this.show('warning', title, message, duration);
}
info(title, message, duration) {
return this.show('info', title, message, duration);
}
}
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
export const toast = new ToastManager();
export default toast;