Skip to content

Commit f603823

Browse files
committed
Refactor and add annotations
1 parent ab9f1a4 commit f603823

File tree

3 files changed

+44
-90
lines changed

3 files changed

+44
-90
lines changed

src/index.d.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/lib/SvelteToast.svelte

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<script>
22
import { fade, fly } from 'svelte/transition'
33
import { flip } from 'svelte/animate'
4-
import { toast } from './stores.js'
4+
import { toast } from './stores'
55
import ToastItem from './ToastItem.svelte'
66
7+
/** @type {import('./stores').SvelteToastOptions} */
78
export let options = {}
89
export let target = 'default'
910
11+
let items = []
12+
13+
function getCss(theme) {
14+
return Object.keys(theme).reduce((a, c) => `${a}${c}:${theme[c]};`, '')
15+
}
16+
1017
$: toast._init(target, options)
1118
12-
let items
1319
$: items = $toast.filter((i) => i.target === target)
14-
15-
const getCss = (theme) => Object.keys(theme).reduce((a, c) => `${a}${c}:${theme[c]};`, '')
1620
</script>
1721

1822
<ul class="_toastContainer">

src/lib/ToastItem.svelte

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,35 @@
22
import { onMount, onDestroy } from 'svelte'
33
import { tweened } from 'svelte/motion'
44
import { linear } from 'svelte/easing'
5-
import { toast } from './stores.js'
5+
import { toast } from './stores'
66
7+
/** @type {import('./stores').SvelteToastOptions} */
78
export let item
89
9-
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
10-
const close = () => toast.pop(item.id)
11-
const autoclose = () => {
12-
if ($progress === 1 || $progress === 0) {
13-
close()
14-
}
15-
}
1610
let next = item.initial
1711
let prev = next
1812
let paused = false
13+
let cprops = {}
14+
let unlisten
1915
20-
$: if (next !== item.next) {
21-
next = item.next
22-
prev = $progress
23-
paused = false
24-
progress.set(next).then(autoclose)
16+
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
17+
18+
function close() {
19+
toast.pop(item.id)
20+
}
21+
22+
function autoclose() {
23+
if ($progress === 1 || $progress === 0) close()
2524
}
2625
27-
const pause = () => {
26+
function pause() {
2827
if (!paused && $progress !== next) {
2928
progress.set($progress, { duration: 0 })
3029
paused = true
3130
}
3231
}
3332
34-
const resume = () => {
33+
function resume() {
3534
if (paused) {
3635
const d = item.duration
3736
const duration = d - d * (($progress - prev) / (next - prev))
@@ -40,20 +39,11 @@ const resume = () => {
4039
}
4140
}
4241
43-
let componentProps = {}
44-
$: if (item.component) {
45-
const { props = {}, sendIdTo } = item.component
46-
componentProps = { ...props, ...(sendIdTo && { [sendIdTo]: item.id }) }
47-
}
48-
49-
const check = (prop, kind = 'undefined') => typeof prop === kind
50-
// `progress` has been renamed to `next`; shim included for backward compatibility, to remove in next major
51-
$: if (!check(item.progress)) {
52-
item.next = item.progress
42+
function check(prop, kind = 'undefined') {
43+
return typeof prop === kind
5344
}
5445
55-
let unlisten
56-
const listen = (d = document) => {
46+
function listen(d = document) {
5747
if (check(d.hidden)) return
5848
const handler = () => (d.hidden ? pause() : resume())
5949
const name = 'visibilitychange'
@@ -62,7 +52,25 @@ const listen = (d = document) => {
6252
handler()
6353
}
6454
55+
$: if (next !== item.next) {
56+
next = item.next
57+
prev = $progress
58+
paused = false
59+
progress.set(next).then(autoclose)
60+
}
61+
62+
$: if (item.component) {
63+
const { props = {}, sendIdTo } = item.component
64+
cprops = { ...props, ...(sendIdTo && { [sendIdTo]: item.id }) }
65+
}
66+
67+
// `progress` has been renamed to `next`; shim included for backward compatibility, to remove in next major
68+
$: if (!check(item.progress)) {
69+
item.next = item.progress
70+
}
71+
6572
onMount(listen)
73+
6674
onDestroy(() => {
6775
if (check(item.onpop, 'function')) {
6876
item.onpop(item.id)
@@ -81,7 +89,7 @@ onDestroy(() => {
8189
>
8290
<div role="status" class="_toastMsg" class:pe={item.component}>
8391
{#if item.component}
84-
<svelte:component this={item.component.src} {...componentProps} />
92+
<svelte:component this={item.component.src} {...cprops} />
8593
{:else}
8694
{@html item.msg}
8795
{/if}

0 commit comments

Comments
 (0)