Skip to content

Commit 10c86ef

Browse files
authored
Initial fix logic (#1085)
1 parent d3e6ee7 commit 10c86ef

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

packages/ui-elements/src/components/ToastBar/ToastBar.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import useTheme from '../../hooks/useTheme';
1111
const ToastBar = ({ toast, onClose }) => {
1212
const { type, message, time = 2000 } = toast;
1313
const toastRef = useRef();
14+
const latestOnClose = useRef(onClose);
1415
const { theme } = useTheme();
1516

1617
const { classNames, styleOverrides } = useComponentOverrides('ToastBar');
@@ -42,8 +43,15 @@ const ToastBar = ({ toast, onClose }) => {
4243
}, [theme.colors, type]);
4344

4445
useEffect(() => {
45-
setTimeout(onClose, time);
46-
}, [onClose, time]);
46+
latestOnClose.current = onClose;
47+
}, [onClose]);
48+
49+
useEffect(() => {
50+
const timer = setTimeout(() => {
51+
latestOnClose.current?.();
52+
}, time);
53+
return () => clearTimeout(timer);
54+
}, [time]);
4755

4856
return (
4957
<Box

packages/ui-elements/src/components/ToastBar/ToastBar.styles.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export const getToastBarContainerStyles = (theme) => {
4343
position: absolute;
4444
z-index: ${theme.zIndex?.toastbar || 1600};
4545
border-radius: ${theme.radius};
46-
animation: ${animation} ${2000}ms ease-in-out forwards;
46+
display: flex;
47+
flex-direction: column;
48+
gap: 0.75rem;
4749
`,
4850
};
4951
return styles;

packages/ui-elements/src/components/ToastBar/ToastBarProvider.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const ToastBarProvider = ({ position = 'bottom right', children }) => {
66
const [toasts, setToasts] = useState([]);
77
const dispatchToast = useCallback(
88
(toast) => {
9-
setToasts((prevToasts) => [toast, ...prevToasts]);
9+
const withId = {
10+
id: toast.id || `${Date.now()}-${Math.random().toString(36).slice(2)}`,
11+
...toast,
12+
};
13+
setToasts((prevToasts) => [withId, ...prevToasts]);
1014
},
1115
[setToasts]
1216
);

packages/ui-elements/src/components/ToastBar/ToastContainer.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const ToastContainer = () => {
99
const { theme } = useTheme();
1010
const styles = getToastBarContainerStyles(theme);
1111
const { position, toasts, setToasts } = useContext(ToastContext);
12+
1213
const positionStyle = useMemo(() => {
1314
const positions = position.split(/\s+/);
1415
const styleAnchor = {};
@@ -17,21 +18,29 @@ const ToastContainer = () => {
1718
});
1819
return styleAnchor;
1920
}, [position]);
20-
const currentToast = useMemo(() => {
21-
const toast = toasts[toasts.length - 1];
22-
return toast;
23-
}, [toasts]);
2421

25-
const onClose = useCallback(() => {
26-
setToasts(toasts.slice(0, toasts.length - 1));
27-
}, [setToasts, toasts]);
28-
if (!currentToast) {
22+
const stackedToasts = useMemo(() => [...toasts].reverse(), [toasts]);
23+
24+
const handleClose = useCallback(
25+
(id) => {
26+
setToasts((prevItems) => prevItems.filter((toast) => toast.id !== id));
27+
},
28+
[setToasts]
29+
);
30+
31+
if (!stackedToasts.length) {
2932
return null;
3033
}
3134

3235
return (
3336
<Box css={styles.container} style={positionStyle}>
34-
<ToastBar toast={currentToast} onClose={onClose} />
37+
{stackedToasts.map((toast) => (
38+
<ToastBar
39+
key={toast.id}
40+
toast={toast}
41+
onClose={() => handleClose(toast.id)}
42+
/>
43+
))}
3544
</Box>
3645
);
3746
};

0 commit comments

Comments
 (0)