Skip to content

Commit d053c91

Browse files
authored
Merge pull request #436 from danice/toast-content
feat(toast): property to specify an element as toast content instead …
2 parents dbe499e + 128e349 commit d053c91

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/toasts.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export interface ToastOptions extends BaseOptions {
88
* @default ""
99
*/
1010
text: string;
11+
/**
12+
* Element Id for the tooltip.
13+
* @default ""
14+
*/
15+
toastId?: string;
1116
/**
1217
* Length in ms the Toast stays before dismissal.
1318
* @default 4000
@@ -53,7 +58,7 @@ let _defaults: ToastOptions = {
5358

5459
export class Toast {
5560
/** The toast element. */
56-
el: HTMLDivElement;
61+
el: HTMLElement;
5762
/**
5863
* The remaining amount of time in ms that the toast
5964
* will stay before dismissal.
@@ -199,7 +204,10 @@ export class Toast {
199204
}
200205

201206
_createToast() {
202-
const toast = document.createElement('div');
207+
const toast = this.options.toastId
208+
? document.getElementById(this.options.toastId)
209+
: document.createElement('div');
210+
//const toast = document.createElement('div');
203211
toast.classList.add('toast');
204212
toast.setAttribute('role', 'alert');
205213
toast.setAttribute('aria-live', 'assertive');
@@ -211,7 +219,8 @@ export class Toast {
211219
}
212220

213221
// Set text content
214-
toast.innerText = this.message;
222+
if (this.message)
223+
toast.innerText = this.message;
215224

216225
// Append toast
217226
Toast._container.appendChild(toast);
@@ -220,6 +229,7 @@ export class Toast {
220229

221230
_animateIn() {
222231
// Animate toast in
232+
this.el.style.display = ""
223233
anim({
224234
targets: this.el,
225235
top: 0,
@@ -273,10 +283,12 @@ export class Toast {
273283
this.options.completeCallback();
274284
}
275285
// Remove toast from DOM
276-
this.el.remove();
277-
Toast._toasts.splice(Toast._toasts.indexOf(this), 1);
278-
if (Toast._toasts.length === 0) {
279-
Toast._removeContainer();
286+
if (!this.options.toastId) {
287+
this.el.remove();
288+
Toast._toasts.splice(Toast._toasts.indexOf(this), 1);
289+
if (Toast._toasts.length === 0) {
290+
Toast._removeContainer();
291+
}
280292
}
281293
}
282294
});

0 commit comments

Comments
 (0)