Skip to content

Commit f5d6cf2

Browse files
committed
✨ Added ability to click-to-close toast
1 parent ec32611 commit f5d6cf2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/toaster.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class Toaster {
1717
for (let i = this.toastQueue.length - 1; i >= 0; i--) {
1818
this.toastQueue[i].duration -= deltaTime;
1919
if (this.toastQueue[i].duration <= 0) {
20-
this.toastQueue[i].el.remove();
20+
if (this.toastQueue[i].el.isConnected) {
21+
this.toastQueue[i].el.remove();
22+
}
2123
this.toastQueue.splice(i, 1);
2224
}
2325
}
@@ -50,6 +52,10 @@ class Toaster {
5052
el.classList.add(...toast.classes);
5153
}
5254
toast.el = el;
55+
toast.el.addEventListener("click", () => {
56+
toast.el.remove();
57+
this.toastQueue.splice(this.toastQueue.indexOf(toast), 1);
58+
});
5359
this.toastQueue.push(toast);
5460
this.flip(el);
5561
}

test/toaster.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ toaster-component output{
2525
fade-out 0.3s 4.4s ease;
2626
animation-fill-mode: forwards;
2727
font-size: var(--font-sm);
28+
user-select: none;
29+
cursor: pointer;
30+
pointer-events: all;
31+
}
32+
toaster-component output:active{
33+
transform: scale(0.95);
2834
}
2935
@keyframes fade-in {
3036
from { opacity: 0 }

0 commit comments

Comments
 (0)