/** * @fileoverview Toast Notification Component for Rantii * @author retoor * @description Non-intrusive notification messages for user feedback * @keywords toast, notification, alert, message, feedback */ import { BaseComponent } from './base-component.js'; class ToastNotification extends BaseComponent { static get observedAttributes() { return ['type', 'message', 'duration']; } init() { this.autoHideTimer = null; this.render(); } render() { const type = this.getAttr('type') || 'info'; const message = this.getAttr('message') || ''; this.addClass('toast', `toast-${type}`); this.setHtml(`
${this.getIcon(type)} ${message}
`); this.bindEvents(); this.startAutoHide(); } getIcon(type) { const icons = { success: ``, error: ``, warning: ``, info: `` }; return icons[type] || icons.info; } bindEvents() { const closeBtn = this.$('.toast-close'); if (closeBtn) { this.on(closeBtn, 'click', () => this.dismiss()); } } startAutoHide() { const duration = parseInt(this.getAttr('duration') || '4000', 10); if (duration > 0) { this.autoHideTimer = setTimeout(() => this.dismiss(), duration); } } dismiss() { if (this.autoHideTimer) { clearTimeout(this.autoHideTimer); } this.addClass('toast-hiding'); setTimeout(() => { this.emit('toast-dismissed'); this.remove(); }, 300); } show(message, type = 'info', duration = 4000) { this.setAttr('message', message); this.setAttr('type', type); this.setAttr('duration', duration.toString()); this.render(); } } customElements.define('toast-notification', ToastNotification); class ToastContainer extends BaseComponent { init() { this.addClass('toast-container'); } show(message, type = 'info', duration = 4000) { const toast = document.createElement('toast-notification'); toast.setAttribute('message', message); toast.setAttribute('type', type); toast.setAttribute('duration', duration.toString()); this.appendChild(toast); return toast; } success(message, duration = 4000) { return this.show(message, 'success', duration); } error(message, duration = 5000) { return this.show(message, 'error', duration); } warning(message, duration = 4000) { return this.show(message, 'warning', duration); } info(message, duration = 4000) { return this.show(message, 'info', duration); } clearAll() { this.innerHTML = ''; } } customElements.define('toast-container', ToastContainer); export { ToastNotification, ToastContainer };