This repository was archived by the owner on Feb 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToast.js
More file actions
163 lines (128 loc) · 4.91 KB
/
Toast.js
File metadata and controls
163 lines (128 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const DEFAULT_OPTIONS = {
autoClose: 5000,
position: "top-right",
onClose: () => {},
closeBtn: true,
showProgress: true,
progressColor: "#2d8fff"
}
import './toast.css';
export default class Toast {
element;
autoCloseTimeout;
autoCloseTime;
progressInterval;
visibleSince;
closeBtn;
/**
*
* @param {{content: String, position: String, autoClose: Number, closeBtn: Boolean, progressColor: String, showProgress: Boolean}} options
*/
constructor (options) {
this.closeBtn = options.closeBtn === false ? false : true;
this.autoCloseTimeout = null;
this.autoCloseTime = null;
this.visibleSince = Date.now();
this.element = this._createToast();
this.update({...DEFAULT_OPTIONS, ...options})
requestAnimationFrame(() => {
this.element.classList.add(this.CSS.toastShow);
});
}
/**
*
* @param {{content: String, position: String, autoClose: Number}} options
*/
update (options) {
Object.entries(options).forEach(([key, value]) => {
this[key] = value;
})
}
remove () {
const container = this.element.parentElement;
if(this.autoCloseTimeout !== null) clearTimeout(this.autoCloseTimeout);
if(this.progressInterval !== null) clearInterval(this.progressInterval);
if(this.element !== null) {
this.element.classList.remove(this.CSS.toastShow);
this.element.addEventListener("transitionend", () => {
this.element.remove();
if(container === null || container.hasChildNodes()) return;
container.remove();
})
}
this.onClose();
}
set progressColor (value) {
this.element.querySelector("." + this.CSS.toastProgress).style.setProperty("--progress-color", value);
}
set showProgress (value) {
if(value !== true) return;
let progressbar = this.element.querySelector("." + this.CSS.toastProgress);
progressbar.style.setProperty("--progress", 1);
this.progressInterval = setInterval(() => {
const timeVisible = Date.now() - this.visibleSince
progressbar.style.setProperty("--progress", 1 - timeVisible / this.autoCloseTime);
}, 10);
}
onClose () {
}
_createToast () {
let toast = document.createElement("div");
let toast_body = document.createElement("div");
let progressbar = document.createElement("div");
toast.classList.add(this.CSS.toast);
toast_body.classList.add(this.CSS.toastBody);
progressbar.classList.add(this.CSS.toastProgress);
toast.appendChild(toast_body);
if(this.closeBtn === true) {
let closeBtn = document.createElement("button");
closeBtn.classList.add(this.CSS.toastCloseBtn);
closeBtn.innerHTML = '<svg aria-hidden="true" viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7.71 8.23l3.75 3.75-1.48 1.48-3.75-3.75-3.75 3.75L1 11.98l3.75-3.75L1 4.48 2.48 3l3.75 3.75L9.98 3l1.48 1.48-3.75 3.75z"></path></svg>'
toast.appendChild(closeBtn);
closeBtn.addEventListener("click", () => {
this.onClose();
this.remove();
})
}
toast.appendChild(progressbar);
toast.addEventListener("click", () => {
this.onClose();
this.remove();
})
return toast;
}
set autoClose (value) {
if(!value || value < 0) return;
if(this.autoCloseTimeout !== null) clearTimeout(this.autoCloseTimeout);
this.autoCloseTime = value;
this.autoCloseTimeout = setTimeout(this.remove.bind(this), value);
}
set position (position) {
const currentContainer = this.element.parentElement;
const container = document.querySelector("." + this.CSS.toastContainer + `[data-position="${position}"]`) || this._createContainer(position);
container.appendChild(this.element);
if(currentContainer === null || currentContainer.hasChildNodes()) return;
currentContainer.remove();
}
set content (content) {
this.element.querySelector("." + this.CSS.toastBody).innerText = content;
}
_createContainer (position) {
let container = document.createElement("div");
container.classList.add(this.CSS.toastContainer);
container.setAttribute("data-position", position);
document.body.appendChild(container);
return container;
}
get CSS () {
return {
toast: "toast",
toastContainer: "toast-container",
toastCloseBtn: "toast--closeBtn",
toastBody: "toast-body",
toastProgress: "toast-progressbar",
toastShow: "toast-show",
progressShow: "progressbar-run"
}
}
}