1
1
import { parseToHtml } from '../../utils/DOM/DOMHelpers' ;
2
+ import { one } from '../../utils/event/eventHandler' ;
2
3
3
4
let id = 0 ;
4
5
@@ -29,14 +30,18 @@ const useAlertToast = (params = {}) => {
29
30
/**
30
31
* Builds the template for an individual toast.
31
32
* @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').
33
34
* @param {string } toastId - The unique ID for the toast element.
34
35
* @returns {HTMLElement } - The constructed toast element.
35
36
*/
36
37
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>
40
45
</div>
41
46
</div>
42
47
` ) ;
@@ -46,7 +51,7 @@ const useAlertToast = (params = {}) => {
46
51
* @returns {HTMLElement } - The constructed toast stack container element.
47
52
*/
48
53
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 ">
50
55
</div>
51
56
` ) ;
52
57
@@ -64,27 +69,13 @@ const useAlertToast = (params = {}) => {
64
69
return getElement ( ) ;
65
70
} ;
66
71
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
-
81
72
/**
82
73
* Displays a toast with the given text, type, and optional timeout duration.
83
74
* @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').
85
76
* @param {number|boolean } [timeOut=false] - Optional timeout duration for the toast.
86
77
*/
87
- const showToast = ( text , type , timeOut = false ) => {
78
+ const showToast = ( text , type = null , timeOut = false ) => {
88
79
const toastId = getId ( ) ;
89
80
const toast = buildToastTemplate ( text , type , toastId ) ;
90
81
const toastStack = getToastStackTemplate ( ) ;
@@ -94,15 +85,17 @@ const useAlertToast = (params = {}) => {
94
85
95
86
const toastInDOM = document . querySelector ( `#${ toastId } ` ) ;
96
87
97
- toastInDOM . classList . remove ( 'd-none' ) ;
88
+ const instance = window . bootstrap . Toast . getOrCreateInstance ( toastInDOM , {
89
+ autohide : true ,
90
+ animation : true ,
91
+ delay : timeOut ,
92
+ } ) ;
98
93
99
- setTimeout ( ( ) => {
100
- toastInDOM . classList . add ( 'show' ) ;
101
- } , 10 ) ;
94
+ instance . show ( ) ;
102
95
103
- toastInDOM . dataset . timeoutId = setTimeout ( ( ) => {
104
- hideToast ( toastInDOM ) ;
105
- } , timeOut ) ;
96
+ one ( toastInDOM , 'hidden.bs.toast' , ( ) => {
97
+ toastInDOM . remove ( ) ;
98
+ } ) ;
106
99
} ;
107
100
108
101
/**
0 commit comments