diff --git a/src/Notification/InlineNotification.svelte b/src/Notification/InlineNotification.svelte index 59e375a5..c20bda45 100644 --- a/src/Notification/InlineNotification.svelte +++ b/src/Notification/InlineNotification.svelte @@ -17,6 +17,12 @@ */ export let lowContrast = false; + /** + * Set the timeout duration (ms) to hide the notification after opening it + * @type {number} [timeout=0] + */ + export let timeout = 0; + /** * Set the `role` attribute * @type {string} [role="alert"] @@ -54,7 +60,23 @@ const dispatch = createEventDispatcher(); - $: open = true; + let open = true; + let timeoutId = undefined; + + function close() { + open = false; + dispatch('close'); + } + + onMount(() => { + if (timeout) { + timeoutId = setTimeout(() => close(), timeout); + } + + return () => { + clearTimeout(timeoutId); + }; + }); {#if open} @@ -81,10 +103,7 @@ { - open = false; - dispatch('close'); - }} /> + on:click={close()} /> {/if} {/if} diff --git a/src/Notification/ToastNotification.svelte b/src/Notification/ToastNotification.svelte index 7bd38016..1673ebed 100644 --- a/src/Notification/ToastNotification.svelte +++ b/src/Notification/ToastNotification.svelte @@ -18,7 +18,7 @@ export let lowContrast = false; /** - * Set the timeout duration (ms) to hide the notification after closing it + * Set the timeout duration (ms) to hide the notification after opening it * @type {number} [timeout=0] */ export let timeout = 0; @@ -69,11 +69,14 @@ let open = true; let timeoutId = undefined; + function close() { + open = false; + dispatch('close'); + } + onMount(() => { if (timeout) { - timeoutId = setTimeout(() => { - open = false; - }, timeout); + timeoutId = setTimeout(() => close(), timeout); } return () => { @@ -102,10 +105,7 @@ { - open = false; - dispatch('close'); - }} /> + on:click={close()} /> {/if} {/if}