diff --git a/.changeset/perfect-turtles-eat.md b/.changeset/perfect-turtles-eat.md new file mode 100644 index 0000000000..7d821f902c --- /dev/null +++ b/.changeset/perfect-turtles-eat.md @@ -0,0 +1,5 @@ +--- +"@heroui/toast": patch +--- + +Fix toast items closing in reverse order. Toasts now close in proper FIFO instead of LIFO (#5096) diff --git a/packages/components/toast/src/use-toast.ts b/packages/components/toast/src/use-toast.ts index 6f6f01b9b7..5e7798b684 100644 --- a/packages/components/toast/src/use-toast.ts +++ b/packages/components/toast/src/use-toast.ts @@ -202,6 +202,7 @@ export function useToast(originalProps: UseToastProps) }, []); const [isLoading, setIsLoading] = useState(!!promiseProp); + const [isToastExiting, setIsToastExiting] = useState(false); useEffect(() => { if (!promiseProp) return; @@ -220,7 +221,7 @@ export function useToast(originalProps: UseToastProps) startTime.current = timestamp; } - if (isToastHovered || isRegionExpanded || index != total - 1) { + if (isToastHovered || isRegionExpanded) { pausedTime.current += timestamp - startTime.current; startTime.current = null; animationRef.current = requestAnimationFrame(updateProgress); @@ -232,7 +233,7 @@ export function useToast(originalProps: UseToastProps) timeElapsed.current = elapsed; if (timeElapsed.current >= timeout) { - state.close(toast.key); + setIsToastExiting(true); } progressRef.current = Math.min((elapsed / timeout) * 100, 100); @@ -264,6 +265,7 @@ export function useToast(originalProps: UseToastProps) total, isRegionExpanded, isLoading, + setIsToastExiting, ]); const Component = as || "div"; @@ -283,7 +285,6 @@ export function useToast(originalProps: UseToastProps) }, []); const [initialHeight, setInitialHeight] = useState(0); - const [isToastExiting, setIsToastExiting] = useState(false); // Following was inspired from sonner ❤️ useLayoutEffect(() => {