From 46285c4769e01488bff8b061031814bc8c14c8a5 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 20 Sep 2024 18:16:33 +0800 Subject: [PATCH] Refactor `onpop()` details --- src/lib/ToastItem.svelte | 28 ++++++++++------------------ src/lib/stores.js | 1 + 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/lib/ToastItem.svelte b/src/lib/ToastItem.svelte index 13845e9..248f5e7 100644 --- a/src/lib/ToastItem.svelte +++ b/src/lib/ToastItem.svelte @@ -14,20 +14,19 @@ let paused = false let cprops = {} /** @type {any} */ let unlisten +/** @type {MouseEvent | KeyboardEvent} */ +let event const progress = tweened(item.initial, { duration: item.duration, easing: linear }) -function close(details) { - item.details = details +/** @param {MouseEvent|KeyboardEvent|undefined} [ev] */ +function close(ev) { + if (ev) event = ev toast.pop(item.id) } function autoclose() { - if ($progress === 1 || $progress === 0) - close({ - autoClose: true, - originalEvent: null - }) + if ($progress === 1 || $progress === 0) close() } function pause() { @@ -80,10 +79,7 @@ $: if (!check(item.progress)) { onMount(listen) onDestroy(() => { - if (check(item.onpop, 'function')) { - // @ts-ignore - item.onpop(item.id, item.details) - } + item.onpop && item.onpop(item.id, { event }) unlisten && unlisten() }) @@ -109,13 +105,9 @@ onDestroy(() => { class="_toastBtn pe" role="button" tabindex="0" - on:click={(ev) => - close({ - autoClose: false, - originalEvent: ev - })} - on:keydown={(e) => { - if (e instanceof KeyboardEvent && ['Enter', ' '].includes(e.key)) close(e) + on:click={(ev) => close(ev)} + on:keydown={(ev) => { + if (ev instanceof KeyboardEvent && ['Enter', ' '].includes(ev.key)) close(ev) }} /> {/if} diff --git a/src/lib/stores.js b/src/lib/stores.js index ba0419e..4ba5471 100644 --- a/src/lib/stores.js +++ b/src/lib/stores.js @@ -18,6 +18,7 @@ import { writable } from 'svelte/store' /** * @callback SvelteToastOnPopCallback * @param {number} [id] - optionally get the toast id if needed + * @param {object} [details] */ /**