-
Notifications
You must be signed in to change notification settings - Fork 2
/
toast.js
64 lines (58 loc) · 1.19 KB
/
toast.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
/* https://codepen.io/Coding-in-Public/pen/ZEaKENX */
let toastContainer;
const generateToast = ({
message,
background = "#00214d",
color = "#fffffe",
length = "3000ms",
}) => {
toastContainer.insertAdjacentHTML(
"beforeend",
`<p class="toast"
style="background-color: ${background};
color: ${color};
animation-duration: ${length}">
${message}
</p>`
);
const toast = toastContainer.lastElementChild;
toast.addEventListener("animationend", () => toast.remove());
};
const initToast = () => {
document.body.insertAdjacentHTML(
"afterbegin",
`<div class="toast-container"></div>
<style>
.toast-container {
position: fixed;
top: 1rem;
right: 1.5rem;
display: grid;
justify-items: end;
gap: 1.5rem;
}
.toast {
font-size: 1.5rem;
font-weight: bold;
line-height: 1;
padding: 0.5em 1em;
background-color: lightblue;
animation: toastIt 3000ms cubic-bezier(0.785, 0.135, 0.15, 0.86) forwards;
}
@keyframes toastIt {
0%,
100% {
transform: translateY(-150%);
opacity: 0;
}
10%,
90% {
transform: translateY(0);
opacity: 1;
}
}
</style>
`
);
toastContainer = document.querySelector(".toast-container");
};