From 4df3801f423c0c4e24fa8cdc0cbc391726cde023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Sat, 12 Jun 2021 22:07:21 +0200 Subject: [PATCH] fixed toast timer --- .../toast-portal/ToastPortalElement.ts | 27 ++++++++++--- src/core/utils/timer.ts | 39 ++++++++++++------- src/styles/toast-portal/toast-portal.scss | 15 ++++--- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/components/toast-portal/ToastPortalElement.ts b/src/components/toast-portal/ToastPortalElement.ts index a0e56b5..b648156 100644 --- a/src/components/toast-portal/ToastPortalElement.ts +++ b/src/components/toast-portal/ToastPortalElement.ts @@ -1,11 +1,12 @@ import { controller, targets } from '@github/catalyst'; -import { delay, html, until } from 'core/utils'; +import { delay, html, Timer } from 'core/utils'; import { BaseComponentElement } from 'common/'; @controller class ToastPortalElement extends BaseComponentElement { @targets toastElement: HTMLElement; toasts: Array = []; + timer: Timer; constructor() { super(); } @@ -15,11 +16,20 @@ class ToastPortalElement extends BaseComponentElement { }; pushToast = (type: string, message: string): void => { + const toastLen = this.toasts.length; this.toasts = [{ type, message }, ...this.toasts]; - const interval = setInterval(() => { - this.popToast(); - clearInterval(interval); - }, 5000); + if (toastLen < 1) { + this.timer = new Timer(() => { + this.popToast(); + if (this.toasts.length > 0) { + this.timer.reset(); + } + }, 5000); + } + // const interval = setInterval(() => { + // this.popToast(); + // clearInterval(interval); + // }, 5000); this.update(); }; @@ -33,7 +43,12 @@ class ToastPortalElement extends BaseComponentElement { render = () => { const renderToast = (note: string, type: string) => { - const message = () => html`
${note}
`; + const message = () => + html` +
+ ${note} +
+ `; return html`${message()}`; }; diff --git a/src/core/utils/timer.ts b/src/core/utils/timer.ts index fbd3fec..6c32a6c 100644 --- a/src/core/utils/timer.ts +++ b/src/core/utils/timer.ts @@ -1,20 +1,33 @@ -const Timer = function (callback, delay, ...args) { - var timerId, - start, - remaining = delay; +class Timer { + private timerId: number; + private start: number; + private remaining: number; + private args: any; + constructor(private callback: () => any, private delay: number, ...args) { + this.remaining = delay; + this.args = args; + this.resume(); + } - this.pause = function () { - window.clearTimeout(timerId); - remaining -= Date.now() - start; + pause = () => { + window.clearTimeout(this.timerId); + this.remaining -= Date.now() - this.start; }; - this.resume = function () { - start = Date.now(); - window.clearTimeout(timerId); - timerId = window.setTimeout(callback, remaining, ...args); + resume = () => { + this.start = Date.now(); + window.clearTimeout(this.timerId); + console.log(this.remaining); + this.timerId = window.setTimeout(this.callback, this.remaining, ...this.args); }; - this.resume(); -}; + reset = (pause: boolean = false) => { + window.clearTimeout(this.timerId); + this.remaining = this.delay; + if (!pause) { + this.resume(); + } + }; +} export default Timer; diff --git a/src/styles/toast-portal/toast-portal.scss b/src/styles/toast-portal/toast-portal.scss index 5884cd2..9860839 100644 --- a/src/styles/toast-portal/toast-portal.scss +++ b/src/styles/toast-portal/toast-portal.scss @@ -14,14 +14,19 @@ toast-portal { top: 5px; right: 7px; .toast { + position: relative; + width: 200px; + height: 40px; + border-radius: 4px; + margin: 4px 3px; + padding: 2px 3px; + display: flex; + align-items: center; &.--default { - position: relative; - width: 200px; - height: 40px; background-color: $gray-07; border: 1px solid $gray-08; - border-radius: 4px; - + } + .toast-text { } } }