Skip to content

Commit 46285c4

Browse files
committed
Refactor onpop() details
1 parent 8a715bd commit 46285c4

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

src/lib/ToastItem.svelte

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@ let paused = false
1414
let cprops = {}
1515
/** @type {any} */
1616
let unlisten
17+
/** @type {MouseEvent | KeyboardEvent} */
18+
let event
1719
1820
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
1921
20-
function close(details) {
21-
item.details = details
22+
/** @param {MouseEvent|KeyboardEvent|undefined} [ev] */
23+
function close(ev) {
24+
if (ev) event = ev
2225
toast.pop(item.id)
2326
}
2427
2528
function autoclose() {
26-
if ($progress === 1 || $progress === 0)
27-
close({
28-
autoClose: true,
29-
originalEvent: null
30-
})
29+
if ($progress === 1 || $progress === 0) close()
3130
}
3231
3332
function pause() {
@@ -80,10 +79,7 @@ $: if (!check(item.progress)) {
8079
onMount(listen)
8180
8281
onDestroy(() => {
83-
if (check(item.onpop, 'function')) {
84-
// @ts-ignore
85-
item.onpop(item.id, item.details)
86-
}
82+
item.onpop && item.onpop(item.id, { event })
8783
unlisten && unlisten()
8884
})
8985
</script>
@@ -109,13 +105,9 @@ onDestroy(() => {
109105
class="_toastBtn pe"
110106
role="button"
111107
tabindex="0"
112-
on:click={(ev) =>
113-
close({
114-
autoClose: false,
115-
originalEvent: ev
116-
})}
117-
on:keydown={(e) => {
118-
if (e instanceof KeyboardEvent && ['Enter', ' '].includes(e.key)) close(e)
108+
on:click={(ev) => close(ev)}
109+
on:keydown={(ev) => {
110+
if (ev instanceof KeyboardEvent && ['Enter', ' '].includes(ev.key)) close(ev)
119111
}}
120112
/>
121113
{/if}

src/lib/stores.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { writable } from 'svelte/store'
1818
/**
1919
* @callback SvelteToastOnPopCallback
2020
* @param {number} [id] - optionally get the toast id if needed
21+
* @param {object} [details]
2122
*/
2223

2324
/**

0 commit comments

Comments
 (0)