From 5e22a381a21378aaab670288828ffa0eb534b2c9 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Sun, 18 Feb 2024 19:37:35 -0800 Subject: [PATCH] fix(toast-notification): clear autoclose `timeout` correctly Fixes #1914 --- src/Notification/ToastNotification.svelte | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Notification/ToastNotification.svelte b/src/Notification/ToastNotification.svelte index 63da68c060..bcec850b10 100644 --- a/src/Notification/ToastNotification.svelte +++ b/src/Notification/ToastNotification.svelte @@ -55,6 +55,9 @@ let timeoutId = undefined; function close(closeFromTimeout) { + // Clear the timer if the close button was clicked. + clearTimeout(timeoutId); + const shouldContinue = dispatch( "close", { timeout: closeFromTimeout === true }, @@ -66,14 +69,17 @@ } onMount(() => { - if (timeout) { - timeoutId = setTimeout(() => close(true), timeout); - } - return () => { clearTimeout(timeoutId); }; }); + + $: if (typeof window !== "undefined") { + /** Only start the timer of {@link open} has not been set to `false`. */ + if (open && timeout) { + timeoutId = setTimeout(() => close(true), timeout); + } + }