Skip to content

Commit

Permalink
Refactor and add annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodevx committed Feb 24, 2023
1 parent ab9f1a4 commit f603823
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 90 deletions.
58 changes: 0 additions & 58 deletions src/index.d.ts

This file was deleted.

12 changes: 8 additions & 4 deletions src/lib/SvelteToast.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<script>
import { fade, fly } from 'svelte/transition'
import { flip } from 'svelte/animate'
import { toast } from './stores.js'
import { toast } from './stores'
import ToastItem from './ToastItem.svelte'
/** @type {import('./stores').SvelteToastOptions} */
export let options = {}
export let target = 'default'
let items = []
function getCss(theme) {
return Object.keys(theme).reduce((a, c) => `${a}${c}:${theme[c]};`, '')
}
$: toast._init(target, options)
let items
$: items = $toast.filter((i) => i.target === target)
const getCss = (theme) => Object.keys(theme).reduce((a, c) => `${a}${c}:${theme[c]};`, '')
</script>

<ul class="_toastContainer">
Expand Down
64 changes: 36 additions & 28 deletions src/lib/ToastItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@
import { onMount, onDestroy } from 'svelte'
import { tweened } from 'svelte/motion'
import { linear } from 'svelte/easing'
import { toast } from './stores.js'
import { toast } from './stores'
/** @type {import('./stores').SvelteToastOptions} */
export let item
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
const close = () => toast.pop(item.id)
const autoclose = () => {
if ($progress === 1 || $progress === 0) {
close()
}
}
let next = item.initial
let prev = next
let paused = false
let cprops = {}
let unlisten
$: if (next !== item.next) {
next = item.next
prev = $progress
paused = false
progress.set(next).then(autoclose)
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
function close() {
toast.pop(item.id)
}
function autoclose() {
if ($progress === 1 || $progress === 0) close()
}
const pause = () => {
function pause() {
if (!paused && $progress !== next) {
progress.set($progress, { duration: 0 })
paused = true
}
}
const resume = () => {
function resume() {
if (paused) {
const d = item.duration
const duration = d - d * (($progress - prev) / (next - prev))
Expand All @@ -40,20 +39,11 @@ const resume = () => {
}
}
let componentProps = {}
$: if (item.component) {
const { props = {}, sendIdTo } = item.component
componentProps = { ...props, ...(sendIdTo && { [sendIdTo]: item.id }) }
}
const check = (prop, kind = 'undefined') => typeof prop === kind
// `progress` has been renamed to `next`; shim included for backward compatibility, to remove in next major
$: if (!check(item.progress)) {
item.next = item.progress
function check(prop, kind = 'undefined') {
return typeof prop === kind
}
let unlisten
const listen = (d = document) => {
function listen(d = document) {
if (check(d.hidden)) return
const handler = () => (d.hidden ? pause() : resume())
const name = 'visibilitychange'
Expand All @@ -62,7 +52,25 @@ const listen = (d = document) => {
handler()
}
$: if (next !== item.next) {
next = item.next
prev = $progress
paused = false
progress.set(next).then(autoclose)
}
$: if (item.component) {
const { props = {}, sendIdTo } = item.component
cprops = { ...props, ...(sendIdTo && { [sendIdTo]: item.id }) }
}
// `progress` has been renamed to `next`; shim included for backward compatibility, to remove in next major
$: if (!check(item.progress)) {
item.next = item.progress
}
onMount(listen)
onDestroy(() => {
if (check(item.onpop, 'function')) {
item.onpop(item.id)
Expand All @@ -81,7 +89,7 @@ onDestroy(() => {
>
<div role="status" class="_toastMsg" class:pe={item.component}>
{#if item.component}
<svelte:component this={item.component.src} {...componentProps} />
<svelte:component this={item.component.src} {...cprops} />
{:else}
{@html item.msg}
{/if}
Expand Down

0 comments on commit f603823

Please sign in to comment.