Skip to content

Commit

Permalink
Feat(web): Add autoclose feature to Toast component
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelklibani committed May 21, 2024
1 parent a87c94f commit 4de4151
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/web/src/js/Toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 => {
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/js/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; // milliseconds
4 changes: 3 additions & 1 deletion packages/web/src/scss/components/Toast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,13 @@ Then configure and create a new Toast instance and call the `show` method on it,
import Toast from '@lmc-eu/spirit-web/dist/js/Toast';

const toast = new Toast(null, {
autoCloseInterval: 3000 // Set interval after ToastBar will be closed in ms, default: 3000
color: 'informative', // One of ['inverted' (default), 'success', 'warning, 'danger', 'informative']
containerId: 'toast-example', // Must match the ID of the Toast container in HTML
content: 'Hello, this is my toast message!', // Can be plain text or HTML
enableAutoClose: true, // If true, ToastBar will close after `autoCloseInterval`, default: true
hasIcon: true,
// iconName: 'info', // Optional icon name used as the #fragment in the SVG sprite URL
iconName: 'info', // Optional icon name used as the #fragment in the SVG sprite URL
id: 'my-toast', // An ID is required for dismissible ToastBar
isDismissible: true,
});
Expand Down
6 changes: 4 additions & 2 deletions packages/web/src/scss/components/Toast/dynamic-toast.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import Toast from '../../../js/Toast';
export const addDynamicToast = (event, containerId) => {
const formElement = event.target.closest('form');
const config = {
containerId,
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,
Expand All @@ -29,7 +31,7 @@ export const clearQueue = (event, containerId) => {

if (instance instanceof Toast && instance?.isShown) {
instance?.hide();
cleared++;
cleared += 1;
}
});

Expand Down
11 changes: 11 additions & 0 deletions packages/web/src/scss/components/Toast/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ <h2 class="docs-Heading">Dynamic Toast Queue</h2>
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;
</script>

<form>
Expand Down

0 comments on commit 4de4151

Please sign in to comment.