Skip to content

Commit

Permalink
Merge pull request #177 from gunnartorfis/fix/174-exit-animation-last…
Browse files Browse the repository at this point in the history
…-toast

fix: let animation finish before hiding toasts
  • Loading branch information
gunnartorfis authored Dec 10, 2024
2 parents 8e83328 + cf4ed8e commit c975153
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ export const Toaster: React.FC<ToasterProps> = ({
const toastsCounter = React.useRef(1);
const toastRefs = React.useRef<Record<string, React.RefObject<ToastRef>>>({});
const [toasts, setToasts] = React.useState<ToastProps[]>([]);
const [toastsVisible, setToastsVisible] = React.useState(false);

React.useLayoutEffect(() => {
if (toasts.length > 0) {
setToastsVisible(true);
return;
}

// let the animation finish
const timeout = setTimeout(() => {
setToastsVisible(false);
}, 300);

return () => clearTimeout(timeout);
}, [toasts.length]);

const props = React.useMemo(() => {
return {
Expand All @@ -37,7 +52,7 @@ export const Toaster: React.FC<ToasterProps> = ({
};
}, [toasterProps, toasts]);

if (toasts.length === 0) {
if (!toastsVisible) {
return <ToasterUI {...props} />;
}

Expand Down

0 comments on commit c975153

Please sign in to comment.