Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(toast): property to specify an element as toast content instead … #436

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading