-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToastCG.js
86 lines (76 loc) · 1.86 KB
/
ToastCG.js
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
/**
* @class Toast
* @description Create a new Toast
*
* @param {any} config Add your configuration
*
* parent -> class or id of your target element, when null its document.body
*
* icon -> add your icon
*
* text -> add your message text
*
* style -> primary, accent, information, success, warning, error
*
* position -> left-top, left-bottom, right-top, right-bottom
*
* time -> time until unvisible
*
* @example
* const myToast = new Toast({
* icon: "YOUR ICON",
* text: "Test Toast",
* style: "primary",
* position: "top-right",
* time: 5000,
* });
*/
class Toast {
constructor(config) {
// Create Toast
const newToast = document.createElement("div");
newToast.classList.add("toast");
// Add Icon
if (config.icon != null) {
// Create Icon
const newIcon = document.createElement("span");
newIcon.classList.add("toast-icon");
newIcon.innerHTML = config.icon;
// Append icon
newToast.appendChild(newIcon);
}
// Add Message
if (config.text != null) {
// Create Message
const newMessage = document.createElement("p");
newMessage.classList.add("toast-text", "text-l");
newMessage.innerText = config.text;
// Append Message
newToast.appendChild(newMessage);
} else {
console.error("Error! Please ad a Message to your Toast!");
}
// Setup Style
if (config.style != null) {
newToast.classList.add("toast-" + config.style);
}
// Setup Position
if (config.position != null) {
newToast.classList.add("toast-" + config.position);
}
// Append to parent element
if (config.parent != null) {
document.querySelector(config.parent).appendChild(newToast);
} else {
document.body.appendChild(newToast);
}
// Setup Timer
if (config.time != null) {
setTimeout(destroy, config.time);
}
async function destroy() {
document.body.removeChild(newToast);
}
}
}
export default Toast;