Skip to content

Commit 2d1ccf1

Browse files
committed
Pesky jsdoc type coercions are pesky
1 parent 76a52f4 commit 2d1ccf1

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

src/lib/SvelteToast.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ export let options = {}
99
/** @type {(string|'default')} */
1010
export let target = 'default'
1111
12+
/** @type {import('./stores').SvelteToastOptions[]} */
1213
let items = []
1314
15+
/** @param {Object<string,string|number>} [theme] */
1416
function getCss(theme) {
15-
return Object.keys(theme).reduce((a, c) => `${a}${c}:${theme[c]};`, '')
17+
return theme ? Object.keys(theme).reduce((a, c) => `${a}${c}:${theme[c]};`, '') : undefined
1618
}
1719
1820
$: toast._init(target, options)
@@ -23,7 +25,7 @@ $: items = $toast.filter((i) => i.target === target)
2325
<ul class="_toastContainer">
2426
{#each items as item (item.id)}
2527
<li
26-
class={item.classes.join(' ')}
28+
class={item.classes?.join(' ')}
2729
in:fly={item.intro}
2830
out:fade
2931
animate:flip={{ duration: 200 }}

src/lib/ToastItem.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { toast } from './stores'
77
/** @type {import('./stores').SvelteToastOptions} */
88
export let item
99
10+
/** @type {any} */
1011
let next = item.initial
1112
let prev = next
1213
let paused = false
1314
let cprops = {}
15+
/** @type {any} */
1416
let unlisten
1517
1618
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
@@ -32,13 +34,14 @@ function pause() {
3234
3335
function resume() {
3436
if (paused) {
35-
const d = item.duration
37+
const d = /** @type {any} */ (item.duration)
3638
const duration = d - d * (($progress - prev) / (next - prev))
3739
progress.set(next, { duration }).then(autoclose)
3840
paused = false
3941
}
4042
}
4143
44+
/** @param {any} prop */
4245
function check(prop, kind = 'undefined') {
4346
return typeof prop === kind
4447
}
@@ -73,6 +76,7 @@ onMount(listen)
7376
7477
onDestroy(() => {
7578
if (check(item.onpop, 'function')) {
79+
// @ts-ignore
7680
item.onpop(item.id)
7781
}
7882
unlisten && unlisten()

src/lib/stores.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { writable } from 'svelte/store'
1111
/**
1212
* @typedef {Object} SvelteToastCustomComponent
1313
* @property {SvelteComponent} src - custom Svelte Component
14-
* @property {Object.<string, *>} [props] - props to pass into custom component
14+
* @property {Object<string,any>} [props] - props to pass into custom component
1515
* @property {string} [sendIdTo] - forward toast id to prop name
1616
*/
1717

@@ -32,7 +32,7 @@ import { writable } from 'svelte/store'
3232
* @property {boolean} [dismissable] - allow dissmiss with close button
3333
* @property {boolean} [reversed] - display toasts in reverse order
3434
* @property {FlyParams} [intro] - toast intro fly animation settings
35-
* @property {Object.<string, string|number>} [theme] - css var overrides
35+
* @property {Object<string,string|number>} [theme] - css var overrides
3636
* @property {string[]} [classes] - user-defined classes
3737
* @property {SvelteToastOnPopCallback} [onpop] - callback that runs on toast dismiss
3838
* @property {SvelteToastCustomComponent} [component] - send custom Svelte Component as a message
@@ -50,13 +50,13 @@ const defaults = {
5050
intro: { x: 256 }
5151
}
5252

53-
const createToast = () => {
54-
const { subscribe, update } = writable([])
55-
/** @type {Object.<string, SvelteToastOptions>} */
53+
function createToast() {
54+
const { subscribe, update } = writable(new Array())
55+
/** @type {Object<string,SvelteToastOptions>} */
5656
const options = {}
5757
let count = 0
5858

59-
/** @param {*} obj */
59+
/** @param {any} obj */
6060
function _obj(obj) {
6161
return obj instanceof Object
6262
}
@@ -96,14 +96,15 @@ const createToast = () => {
9696
* - toast.pop(0) // remove all toasts
9797
* - toast.pop(id) // removes the toast with specified `id`
9898
* - toast.pop({ target: 'foo' }) // remove all toasts from target `foo`
99-
* @param {(number|Object.<'target', string>)} [id]
99+
* @param {(number|Object<'target',string>)} [id]
100100
*/
101101
function pop(id) {
102102
update((n) => {
103103
if (!n.length || id === 0) return []
104104
// Filter function is deprecated; shim added for backward compatibility
105105
if (typeof id === 'function') return n.filter((i) => id(i))
106-
if (_obj(id)) return n.filter((i) => i.target !== id.target)
106+
if (_obj(id))
107+
return n.filter(/** @type {SvelteToastOptions[]} i */ (i) => i.target !== id.target)
107108
const found = id || Math.max(...n.map((i) => i.id))
108109
return n.filter((i) => i.id !== found)
109110
})
@@ -115,7 +116,7 @@ const createToast = () => {
115116
* @param {SvelteToastOptions} [opts]
116117
*/
117118
function set(id, opts) {
118-
/** @type {object} */
119+
/** @type {any} */
119120
const param = _obj(id) ? id : { ...opts, id }
120121
update((n) => {
121122
const idx = n.findIndex((i) => i.id === param.id)

src/routes/+page.svelte

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ if (browser) window.toast = toast
1414
1515
const version = PUBLIC_VERSION
1616
17-
let selected
17+
let selected = ''
1818
let code = '// Tap a button below'
1919
let colors = false
2020
let bottom = false
2121
let options = {}
22-
let formatted
22+
let formatted = ''
2323
2424
const buttons = [
2525
{
@@ -133,7 +133,8 @@ toast.set(id, { msg: 'Just a bit more', next: 0.8 })
133133
await sleep(2000)
134134
toast.set(id, { next: 1 })`,
135135
run: async () => {
136-
const sleep = (t) => new Promise((resolve) => setTimeout(resolve, t))
136+
const sleep = (/** @type {number|undefined} */ t) =>
137+
new Promise((resolve) => setTimeout(resolve, t))
137138
const id = toast.push('Loading, please wait...', {
138139
duration: 300,
139140
initial: 0,
@@ -354,13 +355,15 @@ toast.pop(0)`,
354355
}
355356
]
356357
358+
/** @param {{ name: any; code: any; run: any; }} btn */
357359
function clicked(btn) {
358360
selected = btn.name
359361
code = btn.code
360362
btn.run()
361363
if (browser && !dev) window.gtag('event', 'toast', { event_label: btn.name })
362364
}
363365
366+
// @ts-ignore
364367
$: formatted = Prism.highlight(code, Prism.languages.javascript, 'javascript')
365368
</script>
366369
@@ -385,9 +388,10 @@ $: formatted = Prism.highlight(code, Prism.languages.javascript, 'javascript')
385388
</div>
386389
<p class="max-w-2xl mx-auto text-center mb-6">
387390
Simple elegant toast notifications for modern web frontends in very little lines of code.
388-
Because a demo helps better than a thousand API docs, so here it is. Use in Vanilla JS <span
389-
class="font-mono text-sm">(8kB gzipped)</span
390-
> or as a Svelte component.
391+
Because a demo helps better than a thousand API docs, so here it is. Use in Vanilla JS (<strong
392+
>8kB</strong
393+
>
394+
gzipped) or as a Svelte component.
391395
</p>
392396
<div class="mockup-code h-80 mb-4 text-sm overflow-auto">
393397
<pre><code class="language-javascript">{@html formatted}</code></pre>

src/routes/Dummy.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<script>
22
import { toast } from '$lib/index.js'
33
4-
export let toastId
5-
export let title
4+
export let toastId = 0
5+
export let title = ''
66
7-
const clicked = (ok) => {
7+
const clicked = (/** @type {boolean} */ ok) => {
88
toast.pop(toastId)
99
toast.push({
1010
msg: ok ? 'Accepted!' : 'Declined!',

0 commit comments

Comments
 (0)