export enum ToastType {
Default = 'default',
Info = 'info',
Success = 'success',
Error = 'error',
}
export class Toast {
type: ToastType
message: string
title: string
duration: number | undefined
creationTime = new Date()
constructor(message = '', title = '', type = ToastType.Default) {
this.type = type
this.message = message
this.title = title
this.duration = 3000
}
show() {
container.cards.splice(0, 0, this)
if (this.duration !== undefined) {
setTimeout(() => this.dismiss(), this.duration)
}
}
dismiss() {
container.cards.splice(container.cards.indexOf(this), 1)
}
get element() {
return dq(`.toast-card[key='${this.key}']`)
}
get key() {
return this.creationTime.toISOString()
}
static get container() {
return document.querySelector('.toast-card-container')
}
static createToastContainer() {
if (!document.querySelector('.toast-card-container')) {
document.body.insertAdjacentHTML('beforeend', /* html */`