From f43b728a9569b1316c759e32be2ef867cd755a1a Mon Sep 17 00:00:00 2001 From: Ily1606 <40027165+Ily-1606@users.noreply.github.com> Date: Sun, 25 Jun 2023 20:08:16 +0700 Subject: [PATCH] feat: prevent dismiss toast on hover (#22) --- src/wc-toast-item.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/wc-toast-item.js b/src/wc-toast-item.js index d780fcf..3fb4ca8 100644 --- a/src/wc-toast-item.js +++ b/src/wc-toast-item.js @@ -37,13 +37,27 @@ export default class WCToastItem extends HTMLElement { this.style.setProperty('--wc-toast-color', '#f9f9fa'); } - setTimeout(() => { - this.shadowRoot.querySelector('.wc-toast-bar').classList.add('dismiss'); - + const onClose = () => { setTimeout(() => { this.remove(); }, this.EXIT_ANIMATION_DURATION); - }, this.duration); + this.shadowRoot.querySelector('.wc-toast-bar').classList.add('dismiss'); + } + let isHover = false + this.addEventListener("mouseenter", () => { + isHover = true + }) + this.addEventListener("mouseleave", () => { + isHover = false + }) + const timer = setInterval(() => { + if(this.duration <= 0) { + clearInterval(timer) + onClose() + return + } + if(!isHover) this.duration -= 100 + }, 100) } static get observedAttributes() {