Skip to content

Commit

Permalink
fix: starting the toast timer when the promise is resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
macci001 committed Feb 21, 2025
1 parent a66aabe commit 59c5938
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions .changeset/slow-dogs-travel.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
---

fixing maxVisibleToast functionality in toast (#4870)
For promises, starting the timer only after the promise is resolved
31 changes: 20 additions & 11 deletions packages/components/toast/src/use-toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,18 @@ export function useToast<T extends ToastProps>(originalProps: UseToastProps<T>)
}
}, []);

const [isLoading, setIsLoading] = useState<boolean>(!!promiseProp);

useEffect(() => {
if (!promiseProp) return;
promiseProp.finally(() => {
setIsLoading(false);
});
}, [promiseProp]);

useEffect(() => {
const updateProgress = (timestamp: number) => {
if (!timeout) {
if (!timeout || isLoading) {
return;
}

Expand Down Expand Up @@ -240,16 +249,16 @@ export function useToast<T extends ToastProps>(originalProps: UseToastProps<T>)
cancelAnimationFrame(animationRef.current);
}
};
}, [timeout, shouldShowTimeoutProgess, state, isToastHovered, index, total, isRegionExpanded]);

const [isLoading, setIsLoading] = useState<boolean>(!!promiseProp);

useEffect(() => {
if (!promiseProp) return;
promiseProp.finally(() => {
setIsLoading(false);
});
}, [promiseProp]);
}, [
timeout,
shouldShowTimeoutProgess,
state,
isToastHovered,
index,
total,
isRegionExpanded,
isLoading,
]);

const Component = as || "div";
const loadingIcon: ReactNode = icon;
Expand Down
4 changes: 3 additions & 1 deletion packages/components/toast/stories/toast.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ const PromiseToastTemplate = (args: ToastProps) => {
addToast({
title: "Toast Title",
description: "Toast Displayed Successfully",
promise: new Promise((resolve) => setTimeout(resolve, 5000)),
promise: new Promise((resolve) => setTimeout(resolve, 3000)),
timeout: 3000,
shouldShowTimeoutProgess: false,
...args,
});
}}
Expand Down

0 comments on commit 59c5938

Please sign in to comment.