Skip to content

Commit eb93af9

Browse files
committed
Added "closeType" to callback
Why did I not use a wrapper object? I hate this style of function
1 parent db017d8 commit eb93af9

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

simpletoast.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
(() => {
55
if (window !== window.top) return;
6-
const version = buildVersion(1, 8, 0);
6+
const version = buildVersion(1, 9, 0);
77
if (window.SimpleToast) {
88
if (SimpleToast.version) {
99
if (SimpleToast.version >= version.number) return;
@@ -114,7 +114,7 @@
114114
pending += 1;
115115
return;
116116
}
117-
toast.close();
117+
toast.timedout();
118118
});
119119
if (pending) {
120120
startTimeout();
@@ -154,6 +154,7 @@
154154
const body = document.createElement('span');
155155
body.textContent = text;
156156
el.appendChild(body);
157+
let closeType = 'unknown';
157158
const toast = {
158159
setText: (newText) => {
159160
if (!newText || !toast.exists()) return;
@@ -165,9 +166,17 @@
165166
root.removeChild(el);
166167
toasts.delete(id);
167168
if (typeof onClose === 'function') {
168-
onClose(toast);
169+
onClose(toast, closeType);
169170
}
170171
},
172+
timedout: () => {
173+
closeType = 'timeout';
174+
toast.close();
175+
},
176+
closed: () => {
177+
closeType = 'dismissed';
178+
toast.close();
179+
},
171180
};
172181
if (timeout) {
173182
toast.timeout = Date.now() + timeout;
@@ -208,7 +217,7 @@
208217
el.appendChild(elb);
209218
});
210219
}
211-
el.addEventListener('click', toast.close);
220+
el.addEventListener('click', toast.closed);
212221

213222
root.appendChild(el);
214223
toasts.set(id, toast);

0 commit comments

Comments
 (0)