Skip to content

Commit cbf9900

Browse files
authored
fix: binding of toast on button click
1 parent fba1fb3 commit cbf9900

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

simpletoast.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
console.log(`SimpleToast(v${localToast.versionString}): Publicized`);
1515
}
1616
})(this, () => {
17-
const version = buildVersion(2, 0, 1);
17+
const version = buildVersion(2, 0, 2);
1818
const style = {
1919
root: {
2020
display: 'flex',
@@ -76,6 +76,16 @@
7676
return old;
7777
}
7878

79+
function getClassName(clazz) {
80+
if (Array.isArray(clazz)) {
81+
return clazz.join(' ');
82+
}
83+
if (typeof clazz === 'string') {
84+
return clazz;
85+
}
86+
return undefined;
87+
}
88+
7989
const toasts = new Map();
8090
let root = (() => {
8191
function create() {
@@ -146,8 +156,7 @@
146156
const body = el.appendChild(document.createElement('span'));
147157
const fel = el.appendChild(document.createElement('span'));
148158
if (className) {
149-
const clazz = className.toast || className;
150-
el.className = Array.isArray(clazz) ? clazz.join(' ') : (typeof clazz === 'string' ? clazz : undefined);
159+
el.className = getClassName(className.toast || className);
151160
}
152161
applyCSS(el, style.toast);
153162
applyCSS(el, css.toast || css);
@@ -172,12 +181,12 @@
172181
body.innerHTML = newText;
173182
},
174183
exists: () => toasts.has(id),
175-
close: (closeType) => {
184+
close: (closeType = 'unknown') => {
176185
if (!toast.exists()) return;
177186
root.removeChild(el);
178187
toasts.delete(id);
179188
if (typeof onClose === 'function') {
180-
onClose.call(safeToast, closeType || 'unknown', safeToast);
189+
onClose.call(safeToast, closeType, safeToast);
181190
}
182191
},
183192
};
@@ -192,24 +201,23 @@
192201
buttons.forEach((button) => {
193202
if (!button.text) return;
194203
const elb = document.createElement('button');
195-
if (button.className || className && className.button) {
196-
const clazz = button.className || className.button;
197-
elb.className = Array.isArray(clazz) ? clazz.join(' ') : clazz;
204+
if (button.className || className?.button) {
205+
elb.className = getClassName(button.className || className.button);
198206
}
199207
elb.innerHTML = button.text;
200208
applyCSS(elb, style.button);
201209
applyCSS(elb, css.button);
202210
applyCSS(elb, button.css);
203211
if (typeof button.onclick === 'function') {
204-
elb.onclick = button.onclick;
212+
elb.onclick = (e) => button.onclick.call(safeToast, e, safeToast);
205213
}
206214
let prev = {};
207215
elb.onmouseover = () => {
208216
const hoverStyle = Object.assign(
209217
{},
210218
style.button.mouseOver,
211-
css.button && css.button.mouseOver,
212-
button.css && button.css.mouseOver
219+
css.button?.mouseOver,
220+
button.css?.mouseOver
213221
);
214222
prev = applyCSS(hoverStyle);
215223
};

0 commit comments

Comments
 (0)