Skip to content

Alert toast changes - rewrite to bootstrap Toast component #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion _dev/css/theme/components/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
@import "search/index";
@import "page-loader/index";
@import "links-list/index";
@import "alert-toast/index";
11 changes: 0 additions & 11 deletions _dev/css/theme/components/alert-toast/_alert-toast-stack.scss

This file was deleted.

43 changes: 0 additions & 43 deletions _dev/css/theme/components/alert-toast/_alert-toast.scss

This file was deleted.

2 changes: 0 additions & 2 deletions _dev/css/theme/components/alert-toast/_index.scss

This file was deleted.

4 changes: 4 additions & 0 deletions _dev/css/theme/override/bootstrap/_toast.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.toast-container {
width: rem-calc(250px);
}
49 changes: 21 additions & 28 deletions _dev/js/theme/components/useAlertToast.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseToHtml } from '../../utils/DOM/DOMHelpers';
import { one } from '../../utils/event/eventHandler';

let id = 0;

Expand Down Expand Up @@ -29,14 +30,18 @@ const useAlertToast = (params = {}) => {
/**
* Builds the template for an individual toast.
* @param {string} text - The text content of the toast.
* @param {string} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {string|null} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {string} toastId - The unique ID for the toast element.
* @returns {HTMLElement} - The constructed toast element.
*/
const buildToastTemplate = (text, type, toastId) => parseToHtml(`
<div class="alert-toast alert-toast--${type} d-none" id=${toastId}>
<div class="alert-toast__content">
${text}
<div class="toast ${type ? `text-bg-${type}` : ''}" id=${toastId}>
<div class="d-flex">
<div class="toast-body">
${text}
</div>
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close">
</button>
</div>
</div>
`);
Expand All @@ -46,7 +51,7 @@ const useAlertToast = (params = {}) => {
* @returns {HTMLElement} - The constructed toast stack container element.
*/
const buildToastStackTemplate = () => parseToHtml(`
<div id="${stackTemplateId}" class="alert-toast-stack">
<div id="${stackTemplateId}" class="toast-container p-3 top-0 end-0 position-fixed">
</div>
`);

Expand All @@ -64,27 +69,13 @@ const useAlertToast = (params = {}) => {
return getElement();
};

/**
* Hides a toast element.
* @param {HTMLElement} toast - The toast element to hide.
*/
const hideToast = (toast) => {
toast.classList.remove('show');

const hideDuration = (parseFloat(window.getComputedStyle(toast).transitionDuration)) * 1000;

setTimeout(() => {
toast.remove();
}, hideDuration);
};

/**
* Displays a toast with the given text, type, and optional timeout duration.
* @param {string} text - The text content of the toast.
* @param {string} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {string|null} type - The type of toast ('info', 'success', 'danger', 'warning').
* @param {number|boolean} [timeOut=false] - Optional timeout duration for the toast.
*/
const showToast = (text, type, timeOut = false) => {
const showToast = (text, type = null, timeOut = false) => {
const toastId = getId();
const toast = buildToastTemplate(text, type, toastId);
const toastStack = getToastStackTemplate();
Expand All @@ -94,15 +85,17 @@ const useAlertToast = (params = {}) => {

const toastInDOM = document.querySelector(`#${toastId}`);

toastInDOM.classList.remove('d-none');
const instance = window.bootstrap.Toast.getOrCreateInstance(toastInDOM, {
autohide: true,
animation: true,
delay: timeOut,
});

setTimeout(() => {
toastInDOM.classList.add('show');
}, 10);
instance.show();

toastInDOM.dataset.timeoutId = setTimeout(() => {
hideToast(toastInDOM);
}, timeOut);
one(toastInDOM, 'hidden.bs.toast', () => {
toastInDOM.remove();
});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion _dev/js/theme/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import EventEmitter from 'events';

import './windowExpose';
import './components/dynamic-bootstrap-components';
import './core/index';
import './vendors/index';
import './components/dynamic-bootstrap-components';
import bsCustomFileInput from 'bs-custom-file-input';
import './components/header/index';
import './components/customer/index';
Expand Down
2 changes: 0 additions & 2 deletions _dev/js/utils/dynamicImports/useFunctionCallstackMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ const useFunctionCallstackMap = (key) => {
callbackMap.set(key, new Map());
}

callbackMap.set(key, new Map());

const functionsCallMap = callbackMap.get(key);
const currentCallbacks = functionsCallMap.get(elementKey) || [];
const callbacks = [...currentCallbacks, callback];
Expand Down