Skip to content

Commit

Permalink
Refactor onpop() details
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodevx committed Sep 21, 2024
1 parent 8a715bd commit 46285c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
28 changes: 10 additions & 18 deletions src/lib/ToastItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
})
</script>
Expand All @@ -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}
Expand Down
1 change: 1 addition & 0 deletions src/lib/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { writable } from 'svelte/store'
/**
* @callback SvelteToastOnPopCallback
* @param {number} [id] - optionally get the toast id if needed
* @param {object} [details]
*/

/**
Expand Down

0 comments on commit 46285c4

Please sign in to comment.