Skip to content

Commit 28caa0e

Browse files
committed
Add feature pause on visibilitychange (#50)
1 parent 8cdc007 commit 28caa0e

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

cypress/e2e/test.cy.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,37 @@ describe('Integration Tests', () => {
292292
.get('._toastBtn')
293293
.click()
294294
})
295+
296+
it('Toggles pause and resume on visibilitychange', () => {
297+
cy.get('[data-btn=default]')
298+
.click()
299+
.document()
300+
.then((doc) => {
301+
cy.stub(doc, 'hidden').value(true)
302+
})
303+
.document()
304+
.trigger('visibilitychange')
305+
.get('._toastBar')
306+
.then(($bar) => {
307+
const old = parseFloat($bar.val())
308+
cy.wait(500).then(() => {
309+
expect(parseFloat($bar.val())).to.be.equal(old)
310+
})
311+
})
312+
.document()
313+
.then((doc) => {
314+
cy.stub(doc, 'hidden').value(false)
315+
})
316+
.document()
317+
.trigger('visibilitychange')
318+
.get('._toastBar')
319+
.then(($bar) => {
320+
const old = parseFloat($bar.val())
321+
cy.wait(500).then(() => {
322+
expect(parseFloat($bar.val())).to.be.below(old)
323+
})
324+
})
325+
.get('._toastBtn')
326+
.click()
327+
})
295328
})

src/ToastItem.svelte

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script>
2-
import { onDestroy } from 'svelte'
2+
import { onMount, onDestroy } from 'svelte'
33
import { tweened } from 'svelte/motion'
44
import { linear } from 'svelte/easing'
55
import { toast } from './stores.js'
@@ -25,7 +25,7 @@ $: if (next !== item.next) {
2525
}
2626
2727
const pause = () => {
28-
if (item.pausable && !paused && $progress !== next) {
28+
if (!paused && $progress !== next) {
2929
progress.set($progress, { duration: 0 })
3030
paused = true
3131
}
@@ -51,14 +51,32 @@ $: if (typeof item.progress !== 'undefined') {
5151
item.next = item.progress
5252
}
5353
54+
const handler = () => (document.hidden ? pause() : resume())
55+
const listener = (add) => {
56+
const { hidden, addEventListener, removeEventListener } = document
57+
if (typeof hidden === 'undefined') return
58+
const name = 'visibilitychange'
59+
add ? addEventListener(name, handler) : removeEventListener(name, handler)
60+
return true
61+
}
62+
onMount(() => listener(true) && handler())
63+
5464
onDestroy(() => {
5565
if (typeof item.onpop === 'function') {
5666
item.onpop(item.id)
5767
}
68+
listener(false)
5869
})
5970
</script>
6071

61-
<div class="_toastItem" class:pe={item.pausable} on:mouseenter={pause} on:mouseleave={resume}>
72+
<div
73+
class="_toastItem"
74+
class:pe={item.pausable}
75+
on:mouseenter={() => {
76+
if (item.pausable) pause()
77+
}}
78+
on:mouseleave={resume}
79+
>
6280
<div role="status" class="_toastMsg" class:pe={item.component}>
6381
{#if item.component}
6482
<svelte:component this={item.component.src} {...componentProps} />

0 commit comments

Comments
 (0)