From c61ebcf5e7e10d474a160477b8793750223797b1 Mon Sep 17 00:00:00 2001 From: Pavel Klibani Date: Thu, 16 May 2024 13:44:20 +0200 Subject: [PATCH] Feat(web): Add autoclose feature to Toast component --- packages/web/src/js/Toast.ts | 10 +++++++++ packages/web/src/js/constants.ts | 2 ++ .../scss/components/Toast/dynamic-toast.mjs | 2 ++ .../web/src/scss/components/Toast/index.html | 22 +++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/packages/web/src/js/Toast.ts b/packages/web/src/js/Toast.ts index 59df7cc23c..8dd42968d2 100644 --- a/packages/web/src/js/Toast.ts +++ b/packages/web/src/js/Toast.ts @@ -10,6 +10,7 @@ import { CLASS_NAME_HIDDEN, CLASS_NAME_TRANSITIONING, CLASS_NAME_VISIBLE, + TOAST_AUTO_CLOSE_INTERVAL, } from './constants'; import { enableDismissTrigger, enableToggleTrigger, executeAfterTransition, SpiritConfig } from './utils'; import { EventHandler, SelectorEngine } from './dom'; @@ -50,9 +51,11 @@ const PROPERTY_NAME_FALLBACK_TRANSITION = 'opacity'; type Color = keyof typeof COLOR_ICON_MAP; type Config = { + autoCloseInterval: number; color: Color; containerId: string; content: HTMLElement | string; + enableAutoClose: boolean; hasIcon: boolean; iconName: string; id: string; @@ -254,6 +257,13 @@ class Toast extends BaseComponent { ); this.isShown = true; + + if (config.enableAutoClose) { + const timeoutId = setTimeout(() => { + this.hide(); + clearInterval(timeoutId); + }, config.autoCloseInterval || TOAST_AUTO_CLOSE_INTERVAL); + } } finishHiding = (): void => { diff --git a/packages/web/src/js/constants.ts b/packages/web/src/js/constants.ts index 45068da71b..4084b02184 100644 --- a/packages/web/src/js/constants.ts +++ b/packages/web/src/js/constants.ts @@ -12,3 +12,5 @@ export const CLASS_NAME_HIDDEN = 'is-hidden'; export const CLASS_NAME_OPEN = 'is-open'; export const CLASS_NAME_TRANSITIONING = 'is-transitioning'; export const CLASS_NAME_VISIBLE = 'is-visible'; + +export const TOAST_AUTO_CLOSE_INTERVAL = 3000; diff --git a/packages/web/src/scss/components/Toast/dynamic-toast.mjs b/packages/web/src/scss/components/Toast/dynamic-toast.mjs index 7995c990c3..334dc80c29 100644 --- a/packages/web/src/scss/components/Toast/dynamic-toast.mjs +++ b/packages/web/src/scss/components/Toast/dynamic-toast.mjs @@ -3,9 +3,11 @@ import Toast from '../../../js/Toast'; export const addDynamicToast = (event, containerId) => { const formElement = event.target.closest('form'); const config = { + autoCloseInterval: formElement.querySelector('#toast-auto-close-interval').value, color: formElement.querySelector('#toast-color').value, containerId: containerId, content: formElement.querySelector('#toast-content').value, + enableAutoClose: formElement.querySelector('#toast-enable-auto-close').checked, hasIcon: formElement.querySelector('#toast-has-icon').checked, id: `my-dynamic-toast-${Date.now()}`, isDismissible: formElement.querySelector('#toast-is-dismissible').checked, diff --git a/packages/web/src/scss/components/Toast/index.html b/packages/web/src/scss/components/Toast/index.html index d60f262131..3e80e6f195 100644 --- a/packages/web/src/scss/components/Toast/index.html +++ b/packages/web/src/scss/components/Toast/index.html @@ -138,6 +138,18 @@

Dynamic Toast Queue

import { addDynamicToast, clearQueue } from './dynamic-toast.mjs'; window.addDynamicToast = addDynamicToast; window.clearQueue = clearQueue; + + // Disable interval field if auto close is not checked. + + const autoCloseIntervalField = document.getElementById('toast-auto-close-interval'); + const enableAutoCloseCheckbox = document.getElementById('toast-enable-auto-close'); + + enableAutoCloseCheckbox.addEventListener('change', (event) => { + autoCloseIntervalField.disabled = !event.target.checked; + }); + + // Initialize the field state. + autoCloseIntervalField.disabled = !enableAutoCloseCheckbox.checked;
@@ -173,6 +185,16 @@

Dynamic Toast Queue

Dismissible + +
+ + +