Skip to content

Commit be1448c

Browse files
Merge pull request #2046 from lumi-tip/development-lumi-9227
🐛 closing toast and no more than 3 toast appear at the same time
2 parents bbb5144 + 74dd8f9 commit be1448c

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/hooks/useCustomToast.jsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ const bgColors = {
99
success: { background: '#e0ffe8', color: '#00a33d', borderColor: '#00bb2d' },
1010
};
1111

12-
const useCustomToast = ({
13-
toastId,
14-
}) => {
12+
const useCustomToast = () => {
1513
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);
1819
};
1920

2021
const createToast = ({
@@ -23,14 +24,20 @@ const useCustomToast = ({
2324
position = 'top',
2425
maxWidth = '1200px',
2526
width = '90%',
26-
duration = 5000,
27+
duration = 4000,
2728
description,
2829
actions = null,
2930
isClosable = true,
3031
}) => {
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+
}
3238

33-
toastId = toast({
39+
const toastColors = bgColors[status];
40+
const id = toast({
3441
position,
3542
status,
3643
render: () => (
@@ -66,7 +73,7 @@ const useCustomToast = ({
6673
height="24px"
6774
fontSize="10px"
6875
padding={2}
69-
onClick={closeToast}
76+
onClick={() => closeToast(id)}
7077
top={2}
7178
right={2}
7279
>
@@ -91,7 +98,7 @@ const useCustomToast = ({
9198
fontSize="14px"
9299
textDecoration="underline"
93100
target="_blank"
94-
onClick={closeToast}
101+
onClick={() => closeToast(id)}
95102
>
96103
{action.label}
97104
</Button>
@@ -101,7 +108,13 @@ const useCustomToast = ({
101108
</Box>
102109
),
103110
duration,
111+
onCloseComplete: () => {
112+
activeToasts.delete(id);
113+
},
104114
});
115+
116+
activeToasts.add(id);
117+
return id;
105118
};
106119

107120
return {

0 commit comments

Comments
 (0)