From 2a8ffe811e7fd7f090bcbbbc5427a0fc7eddacc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ruffert?= Date: Fri, 23 May 2025 19:38:37 +0200 Subject: [PATCH] feat: limit max toasts to render (maximized) Default is set to 5. --- src/style.css | 20 ---------- src/toast-queue.js | 94 ++++++++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 61 deletions(-) diff --git a/src/style.css b/src/style.css index b70459e..acd3898 100644 --- a/src/style.css +++ b/src/style.css @@ -51,15 +51,6 @@ gap: .6rem; } - /* [data-toast-placement^="top"]:has([data-minimized]) - [data-toast="container"]:has(li:nth-child(n + 2)) { - margin-bottom: 12px; - } - [data-toast-placement^="bottom"]:has([data-minimized]) - [data-toast="container"]:has(li:nth-child(n + 2)) { - margin-top: 12px; - } */ - [data-toast="actions"] { pointer-events: auto; width: var(--toast-width); @@ -119,17 +110,6 @@ opacity: 0; block-size: 0; } - [data-toast="container"][data-minimized] [data-toast="root"]:nth-child(n + 5) { - overflow: clip; - } - [data-toast="container"][data-minimized] [data-toast="root"]:nth-child(n + 20) { - display: none; - } - [data-toast="container"][data-minimized] - [data-toast="root"]:nth-child(n + 5) - [data-toast="notification"] { - box-shadow: none; - } [data-toast="notification"] { width: var(--toast-width); diff --git a/src/toast-queue.js b/src/toast-queue.js index 71d1aa5..5d0779b 100644 --- a/src/toast-queue.js +++ b/src/toast-queue.js @@ -13,6 +13,8 @@ TOAST_TEMPLATE.innerHTML = `
  • `; +const MAX_TOASTS = 5; + const getPlacementViewTransitionClass = (placement) => { if (placement === 'top') return 'block-start inline-start'; if (placement === 'top center') return 'block-start'; @@ -25,8 +27,8 @@ const getPlacementViewTransitionClass = (placement) => { export class ToastQueue { #queue = new Set(); #timeout = 8000; - #placement = 'top end'; /** @type ToastPlacement 'top' | 'top center' | 'top end' | 'bottom' | 'bottom center' | 'bottom end' */ + #placement = 'top end'; #popover; #container; @@ -42,7 +44,7 @@ export class ToastQueue { root.appendChild(toastContainer); this.#popover.addEventListener('click', (event) => { - console.log(event.target.dataset); + console.log('click', event.target.dataset); if (event.target.dataset.toastButton === 'minimize') { wrapInViewTransition(() => { @@ -52,9 +54,12 @@ export class ToastQueue { return; } - if (event.target.closest('[data-toast="container"]').dataset.minimized !== '') return; - if (event.target.dataset.toastButton === 'clear') return; - console.log(event.target); + // if (event.target.closest('[data-toast="container"]')?.dataset.minimized !== '') return; + if (event.target.dataset.toastButton === 'clear') { + const toastId = event.target.closest('[data-toast-id]').dataset.toastId; + this.close(toastId); + return; + } wrapInViewTransition(() => { this.#container.removeAttribute('data-minimized', ''); @@ -106,6 +111,29 @@ export class ToastQueue { } } + render(toasts = []) { + wrapInViewTransition(() => { + if (toasts.length === 0) { + this.#popover.hidePopover(); + } + if (toasts.length === 1) { + this.#popover.showPopover(); + } + + this.#container.innerHTML = ''; + + for (const toast of toasts) { + if (this.#container.firstChild) { + this.#container.insertBefore(toast.ref, this.#container.firstChild); + } else { + this.#container.appendChild(toast.ref); + } + } + + // toastNotification.scrollIntoView(); + }); + } + add(content, variant, options) { const timeout = options?.timeout || this.#timeout; const clone = TOAST_TEMPLATE.content.cloneNode(true); @@ -138,38 +166,20 @@ export class ToastQueue { this.#container.style.setProperty('--numtoasts', this.#queue.size); this.#container.setAttribute('aria-label', `${this.#queue.size} notifications`); - wrapInViewTransition(() => { - if (this.#queue.size === 1) { - this.#popover.showPopover(); - } - - if (this.#container.firstChild) { - this.#container.insertBefore(toastRoot, this.#container.firstChild); - } else { - this.#container.appendChild(toastRoot); - } - - toastNotification.scrollIntoView(); - }); - - toastClearButton.addEventListener('click', (event) => { - console.log('clear', event.target); - this.close(toastId); - }); - - toastRoot.addEventListener('toggle', (event) => { - console.log('toggle', event.target); - }); + this.render( + Array.from(this.#queue) + .reverse() + .filter((_, i) => i <= MAX_TOASTS) + .reverse(), + ); - // toastRoot.addEventListener("scrollsnapchanging", event => { - // const toastId = event.target.dataset.toastId; - // const snapTarget = event.snapTargetInline; - // if (!toastId) return; - // if (snapTarget.dataset.toast === 'sentinel') { - // console.log(event); - // this.close(toastId); - // }; + // toastClearButton.addEventListener('click', (event) => { + // console.log('clear', event.target); + // this.close(toastId); + // }); + // toastRoot.addEventListener('toggle', (event) => { + // console.log('toggle', event.target); // }); return toastRoot; @@ -191,12 +201,13 @@ export class ToastQueue { this.#container.style.setProperty('--numtoasts', this.#queue.size); this.#container.setAttribute('aria-label', `${this.#queue.size} notifications`); - wrapInViewTransition(() => { - if (this.#queue.size === 0) { - this.#popover.hidePopover(); - } - toastRef.remove(); - }); + // toastRef.remove(); + this.render( + Array.from(this.#queue) + .reverse() + .filter((_, i) => i <= MAX_TOASTS) + .reverse(), + ); } /** Clear all toasts. */ @@ -205,6 +216,7 @@ export class ToastQueue { toast.ref.remove(); } this.#queue.clear(); + this.render([]); } /** Pause the timer for all toasts. */ -- knotmirror 0.0.1