@@ -9,12 +9,13 @@ const bgColors = {
9
9
success : { background : '#e0ffe8' , color : '#00a33d' , borderColor : '#00bb2d' } ,
10
10
} ;
11
11
12
- const useCustomToast = ( {
13
- toastId,
14
- } ) => {
12
+ const useCustomToast = ( ) => {
15
13
const toast = useToast ( ) ;
16
- const closeToast = ( ) => {
17
- toast . close ( toastId ) ;
14
+ const activeToasts = new Set ( ) ;
15
+
16
+ const closeToast = ( id ) => {
17
+ toast . close ( id ) ;
18
+ activeToasts . delete ( id ) ;
18
19
} ;
19
20
20
21
const createToast = ( {
@@ -23,14 +24,20 @@ const useCustomToast = ({
23
24
position = 'top' ,
24
25
maxWidth = '1200px' ,
25
26
width = '90%' ,
26
- duration = 5000 ,
27
+ duration = 4000 ,
27
28
description,
28
29
actions = null ,
29
30
isClosable = true ,
30
31
} ) => {
31
- const toastColors = bgColors [ status ] ;
32
+ // Limit to 3 the number of active toast (to avoid overwellming the ui)
33
+ if ( activeToasts . size >= 3 ) {
34
+ // Close oldest toast
35
+ const oldestToast = Array . from ( activeToasts ) [ 0 ] ;
36
+ closeToast ( oldestToast ) ;
37
+ }
32
38
33
- toastId = toast ( {
39
+ const toastColors = bgColors [ status ] ;
40
+ const id = toast ( {
34
41
position,
35
42
status,
36
43
render : ( ) => (
@@ -66,7 +73,7 @@ const useCustomToast = ({
66
73
height = "24px"
67
74
fontSize = "10px"
68
75
padding = { 2 }
69
- onClick = { closeToast }
76
+ onClick = { ( ) => closeToast ( id ) }
70
77
top = { 2 }
71
78
right = { 2 }
72
79
>
@@ -91,7 +98,7 @@ const useCustomToast = ({
91
98
fontSize = "14px"
92
99
textDecoration = "underline"
93
100
target = "_blank"
94
- onClick = { closeToast }
101
+ onClick = { ( ) => closeToast ( id ) }
95
102
>
96
103
{ action . label }
97
104
</ Button >
@@ -101,7 +108,13 @@ const useCustomToast = ({
101
108
</ Box >
102
109
) ,
103
110
duration,
111
+ onCloseComplete : ( ) => {
112
+ activeToasts . delete ( id ) ;
113
+ } ,
104
114
} ) ;
115
+
116
+ activeToasts . add ( id ) ;
117
+ return id ;
105
118
} ;
106
119
107
120
return {
0 commit comments