const smNotifications = document.createElement('template') smNotifications.innerHTML = `
` customElements.define('sm-notifications', class extends HTMLElement { constructor() { super() this.shadow = this.attachShadow({ mode: 'open' }).append(smNotifications.content.cloneNode(true)) this.notificationPanel = this.shadowRoot.querySelector('.notification-panel') this.animationOptions = { duration: 300, fill: "forwards", easing: "cubic-bezier(0.175, 0.885, 0.32, 1.275)" } this.push = this.push.bind(this) this.createNotification = this.createNotification.bind(this) this.removeNotification = this.removeNotification.bind(this) this.clearAll = this.clearAll.bind(this) } randString(length) { let result = ''; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for (let i = 0; i < length; i++) result += characters.charAt(Math.floor(Math.random() * characters.length)); return result; } createNotification(message, options) { const { pinned = false, icon = '' } = options const notification = document.createElement('div') notification.id = this.randString(8) notification.classList.add('notification') let composition = `` composition += `${message}
` if (pinned) { notification.classList.add('pinned') composition += ` ` } notification.innerHTML = composition return notification } push(message, options = {}) { const notification = this.createNotification(message, options) this.notificationPanel.append(notification) notification.animate([ { transform: `translateY(1rem)`, opacity: '0' }, { transform: `none`, opacity: '1' }, ], this.animationOptions) return notification.id } removeNotification(notification) { notification.animate([ { transform: `none`, opacity: '1' }, { transform: `translateY(0.5rem)`, opacity: '0' } ], this.animationOptions).onfinish = () => { notification.remove() } } clearAll() { Array.from(this.notificationPanel.children).forEach(child => { this.removeNotification(child) }) } connectedCallback() { this.notificationPanel.addEventListener('click', e => { if (e.target.closest('.close')) ( this.removeNotification(e.target.closest('.notification')) ) }) const observer = new MutationObserver(mutationList => { mutationList.forEach(mutation => { if (mutation.type === 'childList') { if (mutation.addedNodes.length && !mutation.addedNodes[0].classList.contains('pinned')) { setTimeout(() => { this.removeNotification(mutation.addedNodes[0]) }, 5000); } } }) }) observer.observe(this.notificationPanel, { childList: true, }) } })