Skip to content

Commit bc9215a

Browse files
author
Kyle Andrews
committed
Fixed toaster notification close button bug
1 parent 1a2c70f commit bc9215a

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.2.1] - 2020-04-17
11+
12+
### Fixed
13+
14+
- toaster notification close button bug
15+
1016
## [1.2.0] - 2020-04-04
1117

1218
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codewithkyle/notifyjs",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "A simple JavaScript library for creating and managing toaster & snackbar notifications",
55
"main": "notify.js",
66
"types": "notify.d.ts",

src/notification-manager.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class NotificationManager {
145145
for (let i = 0; i < this.toaster.length; i++) {
146146
this.toaster[i].duration -= deltaTime;
147147
if (this.toaster[i].duration <= 0) {
148-
this.removeToasterNotification(this.toaster[i], i);
148+
this.removeToasterNotification(i);
149149
}
150150
}
151151
}
@@ -335,14 +335,23 @@ export class NotificationManager {
335335

336336
public snackbar = this.notify;
337337

338-
private removeToasterNotification(notification: ToasterNotification, id: number) {
339-
notification.element.remove();
340-
this.toaster.splice(id, 1);
338+
private removeToasterNotification(index: number) {
339+
this.toaster[index].element.remove();
340+
this.toaster.splice(index, 1);
341341
}
342342
private handleToastClose: EventListener = (e: Event) => {
343343
const target = e.currentTarget as HTMLButtonElement;
344-
const id = parseInt(target.parentElement.dataset.id);
345-
this.removeToasterNotification(this.toaster[id], id);
344+
const notificationId = target.parentElement.dataset.id;
345+
let index = null;
346+
for (let i = 0; i < this.toaster.length; i++) {
347+
if (this.toaster[i].element.dataset.id === notificationId) {
348+
index = i;
349+
break;
350+
}
351+
}
352+
if (index) {
353+
this.removeToasterNotification(index);
354+
}
346355
};
347356

348357
private createToast(notification: ToasterNotification) {
@@ -387,7 +396,7 @@ export class NotificationManager {
387396
}
388397

389398
const notificationEl = this.createToast(notification);
390-
notificationEl.dataset.id = `${this.toaster.length}`;
399+
notificationEl.dataset.id = `${performance.now()}`;
391400
notification.element = notificationEl;
392401

393402
if (notification?.duration && !isNaN(notification.duration)) {

0 commit comments

Comments
 (0)