Skip to content

Commit 7c3610f

Browse files
authored
Merge pull request #355 from Oksydan/alert-toast-changes
Alert toast changes - rewrite to bootstrap Toast component
2 parents 89042ee + 3f75080 commit 7c3610f

File tree

8 files changed

+26
-88
lines changed

8 files changed

+26
-88
lines changed

_dev/css/theme/components/_index.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@
88
@import "search/index";
99
@import "page-loader/index";
1010
@import "links-list/index";
11-
@import "alert-toast/index";

_dev/css/theme/components/alert-toast/_alert-toast-stack.scss

Lines changed: 0 additions & 11 deletions
This file was deleted.

_dev/css/theme/components/alert-toast/_alert-toast.scss

Lines changed: 0 additions & 43 deletions
This file was deleted.

_dev/css/theme/components/alert-toast/_index.scss

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
.toast-container {
3+
width: rem-calc(250px);
4+
}

_dev/js/theme/components/useAlertToast.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parseToHtml } from '../../utils/DOM/DOMHelpers';
2+
import { one } from '../../utils/event/eventHandler';
23

34
let id = 0;
45

@@ -29,14 +30,18 @@ const useAlertToast = (params = {}) => {
2930
/**
3031
* Builds the template for an individual toast.
3132
* @param {string} text - The text content of the toast.
32-
* @param {string} type - The type of toast ('info', 'success', 'danger', 'warning').
33+
* @param {string|null} type - The type of toast ('info', 'success', 'danger', 'warning').
3334
* @param {string} toastId - The unique ID for the toast element.
3435
* @returns {HTMLElement} - The constructed toast element.
3536
*/
3637
const buildToastTemplate = (text, type, toastId) => parseToHtml(`
37-
<div class="alert-toast alert-toast--${type} d-none" id=${toastId}>
38-
<div class="alert-toast__content">
39-
${text}
38+
<div class="toast ${type ? `text-bg-${type}` : ''}" id=${toastId}>
39+
<div class="d-flex">
40+
<div class="toast-body">
41+
${text}
42+
</div>
43+
<button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close">
44+
</button>
4045
</div>
4146
</div>
4247
`);
@@ -46,7 +51,7 @@ const useAlertToast = (params = {}) => {
4651
* @returns {HTMLElement} - The constructed toast stack container element.
4752
*/
4853
const buildToastStackTemplate = () => parseToHtml(`
49-
<div id="${stackTemplateId}" class="alert-toast-stack">
54+
<div id="${stackTemplateId}" class="toast-container p-3 top-0 end-0 position-fixed">
5055
</div>
5156
`);
5257

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

67-
/**
68-
* Hides a toast element.
69-
* @param {HTMLElement} toast - The toast element to hide.
70-
*/
71-
const hideToast = (toast) => {
72-
toast.classList.remove('show');
73-
74-
const hideDuration = (parseFloat(window.getComputedStyle(toast).transitionDuration)) * 1000;
75-
76-
setTimeout(() => {
77-
toast.remove();
78-
}, hideDuration);
79-
};
80-
8172
/**
8273
* Displays a toast with the given text, type, and optional timeout duration.
8374
* @param {string} text - The text content of the toast.
84-
* @param {string} type - The type of toast ('info', 'success', 'danger', 'warning').
75+
* @param {string|null} type - The type of toast ('info', 'success', 'danger', 'warning').
8576
* @param {number|boolean} [timeOut=false] - Optional timeout duration for the toast.
8677
*/
87-
const showToast = (text, type, timeOut = false) => {
78+
const showToast = (text, type = null, timeOut = false) => {
8879
const toastId = getId();
8980
const toast = buildToastTemplate(text, type, toastId);
9081
const toastStack = getToastStackTemplate();
@@ -94,15 +85,17 @@ const useAlertToast = (params = {}) => {
9485

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

97-
toastInDOM.classList.remove('d-none');
88+
const instance = window.bootstrap.Toast.getOrCreateInstance(toastInDOM, {
89+
autohide: true,
90+
animation: true,
91+
delay: timeOut,
92+
});
9893

99-
setTimeout(() => {
100-
toastInDOM.classList.add('show');
101-
}, 10);
94+
instance.show();
10295

103-
toastInDOM.dataset.timeoutId = setTimeout(() => {
104-
hideToast(toastInDOM);
105-
}, timeOut);
96+
one(toastInDOM, 'hidden.bs.toast', () => {
97+
toastInDOM.remove();
98+
});
10699
};
107100

108101
/**

_dev/js/theme/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import EventEmitter from 'events';
22

33
import './windowExpose';
4+
import './components/dynamic-bootstrap-components';
45
import './core/index';
56
import './vendors/index';
6-
import './components/dynamic-bootstrap-components';
77
import bsCustomFileInput from 'bs-custom-file-input';
88
import './components/header/index';
99
import './components/customer/index';

_dev/js/utils/dynamicImports/useFunctionCallstackMap.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ const useFunctionCallstackMap = (key) => {
7575
callbackMap.set(key, new Map());
7676
}
7777

78-
callbackMap.set(key, new Map());
79-
8078
const functionsCallMap = callbackMap.get(key);
8179
const currentCallbacks = functionsCallMap.get(elementKey) || [];
8280
const callbacks = [...currentCallbacks, callback];

0 commit comments

Comments
 (0)