Skip to content

Commit

Permalink
toast animation refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush013 committed Jul 10, 2022
1 parent bcc4899 commit a20ea99
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions toast/src/app/Toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Toast {
constructor() {
this.template = document.getElementById("toast");
this.parentContainer = document.querySelector(".toast-container");
this.timeout = 5000;
this.timeout = 3000;

this.create();
}
Expand All @@ -17,26 +17,22 @@ class Toast {
setTimeout(this.dismissToast, this.timeout);
}

animateFadeOutToast = () => {
return new Promise((resolve) => {
this.toast.animate(
[
{
opacity: 0,
transform: "translateY(-50px)",
},
],
{ duration: 300, easing: "ease", fill: "forwards" }
);
setTimeout(resolve, 300);
});
animateToastDismiss = () => {
this.toast.animate(
[
{
opacity: 0,
transform: "translateY(-50px)",
},
],
{ duration: 300, easing: "ease", fill: "forwards" }
);
};

dismissToast = () => {
this.animateFadeOutToast().then(() => {
this.listenToastAnimationEnd(toast).then(() => {
this.toast.remove();
});
this.animateToastDismiss();
this.listenToastAnimationEnd().then(() => {
this.toast.remove();
});
};

Expand All @@ -54,8 +50,8 @@ class Toast {
return text.length > 10 && text.length < 50 ? text : this.getRandomText();
}

async listenToastAnimationEnd(toast) {
const animationPromises = toast.getAnimations().map((a) => a.finished);
async listenToastAnimationEnd() {
const animationPromises = this.toast.getAnimations().map((a) => a.finished);

return await Promise.allSettled(animationPromises);
}
Expand Down

0 comments on commit a20ea99

Please sign in to comment.