-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (46 loc) · 1.92 KB
/
Copy pathscript.js
File metadata and controls
46 lines (46 loc) · 1.92 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
const notifications = document.querySelector(".notifications"),
buttons = document.querySelectorAll(".buttons .btn");
// Object containing details for different types of toasts
const toastDetails = {
timer: 5000,
success: {
icon: 'fa-circle-check',
text: 'Success: This is a success toast.',
},
error: {
icon: 'fa-circle-xmark',
text: 'Error: This is an error toast.',
},
warning: {
icon: 'fa-triangle-exclamation',
text: 'Warning: This is a warning toast.',
},
info: {
icon: 'fa-circle-info',
text: 'Info: This is an information toast.',
}
}
const removeToast = (toast) => {
toast.classList.add("hide");
if(toast.timeoutId) clearTimeout(toast.timeoutId); // Clearing the timeout for the toast
setTimeout(() => toast.remove(), 500); // Removing the toast after 500ms
}
const createToast = (id) => {
// Getting the icon and text for the toast based on the id passed
const { icon, text } = toastDetails[id];
const toast = document.createElement("li"); // Creating a new 'li' element for the toast
toast.className = `toast ${id}`; // Setting the classes for the toast
// Setting the inner HTML for the toast
toast.innerHTML = `<div class="column">
<i class="fa-solid ${icon}"></i>
<span>${text}</span>
</div>
<i class="fa-solid fa-xmark" onclick="removeToast(this.parentElement)"></i>`;
notifications.appendChild(toast); // Append the toast to the notification ul
// Setting a timeout to remove the toast after the specified duration
toast.timeoutId = setTimeout(() => removeToast(toast), toastDetails.timer);
}
// Adding a click event listener to each button to create a toast when clicked
buttons.forEach(btn => {
btn.addEventListener("click", () => createToast(btn.id));
});