-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-overlay.js
More file actions
57 lines (48 loc) · 1.77 KB
/
Copy pathcontent-overlay.js
File metadata and controls
57 lines (48 loc) · 1.77 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
(function () {
const EXISTING = document.getElementById("wgi-toast-container");
if (EXISTING) EXISTING.remove();
window.addEventListener("wgi-show-toast", (e) => {
const { title, message, type, duration } = e.detail;
render(title, message, type, duration || 8000);
});
function render(title, message, type, duration) {
const old = document.getElementById("wgi-toast-container");
if (old) old.remove();
const container = document.createElement("div");
container.id = "wgi-toast-container";
container.className = "wgi-toast-backdrop";
const icon = type === "success" ? "\u2713" : "\u26A0";
const headerClass = type === "success" ? "wgi-success" : "wgi-error";
const barClass = type === "success" ? "" : "wgi-error";
container.innerHTML = `
<div class="wgi-toast">
<div class="wgi-toast-header ${headerClass}">
<span>${icon}</span>
<span>${escapeHtml(title)}</span>
</div>
<div class="wgi-toast-body">${escapeHtml(message)}</div>
<div class="wgi-toast-progress">
<div class="wgi-toast-progress-bar ${barClass}" style="width:100%"></div>
</div>
</div>
`;
document.body.appendChild(container);
requestAnimationFrame(() => {
const bar = container.querySelector(".wgi-toast-progress-bar");
if (bar) {
bar.style.transitionDuration = `${duration}ms`;
bar.style.width = "0%";
}
});
setTimeout(() => {
const toast = container.querySelector(".wgi-toast");
if (toast) toast.classList.add("wgi-removing");
setTimeout(() => container.remove(), 250);
}, duration);
}
function escapeHtml(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML;
}
})();