Skip to content

Commit

Permalink
Merge pull request #436 from danice/toast-content
Browse files Browse the repository at this point in the history
feat(toast): property to specify an element as toast content instead …
  • Loading branch information
wuda-io authored Dec 14, 2023
2 parents dbe499e + 128e349 commit d053c91
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/toasts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export interface ToastOptions extends BaseOptions {
* @default ""
*/
text: string;
/**
* Element Id for the tooltip.
* @default ""
*/
toastId?: string;
/**
* Length in ms the Toast stays before dismissal.
* @default 4000
Expand Down Expand Up @@ -53,7 +58,7 @@ let _defaults: ToastOptions = {

export class Toast {
/** The toast element. */
el: HTMLDivElement;
el: HTMLElement;
/**
* The remaining amount of time in ms that the toast
* will stay before dismissal.
Expand Down Expand Up @@ -199,7 +204,10 @@ export class Toast {
}

_createToast() {
const toast = document.createElement('div');
const toast = this.options.toastId
? document.getElementById(this.options.toastId)
: document.createElement('div');
//const toast = document.createElement('div');
toast.classList.add('toast');
toast.setAttribute('role', 'alert');
toast.setAttribute('aria-live', 'assertive');
Expand All @@ -211,7 +219,8 @@ export class Toast {
}

// Set text content
toast.innerText = this.message;
if (this.message)
toast.innerText = this.message;

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

_animateIn() {
// Animate toast in
this.el.style.display = ""
anim({
targets: this.el,
top: 0,
Expand Down Expand Up @@ -273,10 +283,12 @@ export class Toast {
this.options.completeCallback();
}
// Remove toast from DOM
this.el.remove();
Toast._toasts.splice(Toast._toasts.indexOf(this), 1);
if (Toast._toasts.length === 0) {
Toast._removeContainer();
if (!this.options.toastId) {
this.el.remove();
Toast._toasts.splice(Toast._toasts.indexOf(this), 1);
if (Toast._toasts.length === 0) {
Toast._removeContainer();
}
}
}
});
Expand Down

0 comments on commit d053c91

Please sign in to comment.