Skip to content

Commit c052526

Browse files
committed
Add feature onpop callback
1 parent 4453355 commit c052526

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/ToastItem.svelte

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import { toast } from './stores.js'
66
export let item
77
88
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
9+
const close = () => {
10+
const { id, onpop } = item
11+
toast.pop(id)
12+
if (typeof onpop === 'function') {
13+
onpop(id)
14+
}
15+
}
916
const autoclose = () => {
1017
if ($progress === 1 || $progress === 0) {
11-
toast.pop(item.id)
18+
close()
1219
}
1320
}
1421
let next = item.initial
@@ -31,8 +38,9 @@ const pause = () => {
3138
3239
const resume = () => {
3340
if (paused) {
34-
const remaining = item.duration - item.duration * (($progress - prev) / (next - prev))
35-
progress.set(next, { duration: remaining }).then(autoclose)
41+
const d = item.duration
42+
const duration = d - d * (($progress - prev) / (next - prev))
43+
progress.set(next, { duration }).then(autoclose)
3644
paused = false
3745
}
3846
}
@@ -60,11 +68,7 @@ $: if (typeof item.progress !== 'undefined') {
6068
padding: var(--toastPadding, 0);
6169
background: var(--toastBackground, rgba(66, 66, 66, 0.9));
6270
color: var(--toastColor, #fff);
63-
box-shadow: var(
64-
--toastBoxShadow,
65-
0 4px 6px -1px rgba(0, 0, 0, 0.1),
66-
0 2px 4px -1px rgba(0, 0, 0, 0.06)
67-
);
71+
box-shadow: var(--toastBoxShadow, 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06));
6872
border: var(--toastBorder, none);
6973
border-radius: var(--toastBorderRadius, 0.125rem);
7074
position: relative;
@@ -112,7 +116,7 @@ $: if (typeof item.progress !== 'undefined') {
112116
._toastBar::-webkit-progress-bar {
113117
background: transparent;
114118
}
115-
/* `--toastProgressBackground` has been renamed to `--toastBarBackground`; override included for backward compatibility */
119+
/* `--toastProgressBackground` renamed to `--toastBarBackground`; override included for backward compatibility */
116120
._toastBar::-webkit-progress-value {
117121
background: var(--toastProgressBackground, var(--toastBarBackground, rgba(33, 150, 243, 0.75)));
118122
}
@@ -130,9 +134,7 @@ $: if (typeof item.progress !== 'undefined') {
130134
{/if}
131135
</div>
132136
{#if item.dismissable}
133-
<div class="_toastBtn pe" role="button" tabindex="-1" on:click={() => toast.pop(item.id)}>
134-
135-
</div>
137+
<div class="_toastBtn pe" role="button" tabindex="-1" on:click={close}>✕</div>
136138
{/if}
137139
<progress class="_toastBar" value={$progress} />
138140
</div>

src/index.d.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ declare module '@zerodevx/svelte-toast'
88
* Default Toast Options
99
* ```typescript
1010
* {
11-
* duration: 4000, // duration of progress bar tween to the `next` value
12-
* initial: 1, // initial progress bar value
13-
* next: 0, // next progress value
14-
* pausable: false, // pause progress bar tween on mouse hover
15-
* dismissable: true, // allow dismiss with close button
16-
* reversed: false, // insert new toast to bottom of stack
17-
* intro: { x: 256 }, // toast intro fly animation settings
18-
* theme: {} // css var overrides
11+
* duration: 4000, // duration of progress bar tween to the `next` value
12+
* initial: 1, // initial progress bar value
13+
* next: 0, // next progress value
14+
* pausable: false, // pause progress bar tween on mouse hover
15+
* dismissable: true, // allow dismiss with close button
16+
* reversed: false, // insert new toast to bottom of stack
17+
* intro: { x: 256 }, // toast intro fly animation settings
18+
* theme: {} // css var overrides
1919
* }
2020
* ```
2121
*/
@@ -37,6 +37,7 @@ export interface SvelteToastOptions {
3737
props?: { [key: string]: any }
3838
sendIdTo?: string
3939
}
40+
onpop?(id?: number): any
4041
}
4142

4243
export class SvelteToast extends SvelteComponent {

0 commit comments

Comments
 (0)