Skip to content

Commit 3ce85c0

Browse files
committed
Handle some edge cases for pausable toasts
1 parent 00f88fa commit 3ce85c0

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

cypress/integration/test.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,17 @@ describe('Integration Tests', () => {
210210
}).get('._toastBtn').click()
211211
})
212212
})
213+
214+
it('Passes pausable edge case when `next` is changed on hover', () => {
215+
cy.window().invoke('toast.push', 'test', { pausable: true, duration: 50 })
216+
.then($id => {
217+
cy.get('._toastItem').trigger('mouseenter', { force: true })
218+
.window().invoke('toast.set', $id, { next: 0.1 })
219+
.wait(100)
220+
.get('._toastBar').then($bar => {
221+
expect(parseFloat($bar.val())).to.equal(0.1)
222+
cy.get('._toastBtn').click()
223+
})
224+
})
225+
})
213226
})

src/ToastItem.svelte

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,29 @@ const autoclose = () => {
1111
toast.pop(item.id)
1212
}
1313
}
14-
let prev = item.initial
14+
let next = item.initial
15+
let prev = next
16+
let paused = false
1517
16-
$: if (prev !== item.next) {
17-
progress.set(item.next).then(autoclose)
18-
prev = item.next
18+
$: if (next !== item.next) {
19+
next = item.next
20+
prev = $progress
21+
paused = false
22+
progress.set(next).then(autoclose)
1923
}
2024
2125
const pause = () => {
22-
if (item.pausable) {
26+
if (item.pausable && !paused && $progress !== next) {
2327
progress.set($progress, { duration: 0 })
28+
paused = true
2429
}
2530
}
2631
27-
const play = () => {
28-
if (item.pausable) {
29-
const pct = ($progress - item.initial) / (item.next - item.initial)
30-
const remaining = item.duration - (item.duration * pct)
31-
progress.set(item.next, { duration: remaining }).then(autoclose)
32+
const resume = () => {
33+
if (item.pausable && paused) {
34+
const remaining = item.duration - (item.duration * (($progress - prev) / (next - prev)))
35+
progress.set(next, { duration: remaining }).then(autoclose)
36+
paused = false
3237
}
3338
}
3439
@@ -103,7 +108,7 @@ $: if (typeof item.progress !== 'undefined') {
103108
}
104109
</style>
105110

106-
<div class="_toastItem" class:pe={item.pausable} on:mouseenter={pause} on:mouseleave={play}>
111+
<div class="_toastItem" class:pe={item.pausable} on:mouseenter={pause} on:mouseleave={resume}>
107112
<div class="_toastMsg" class:pe={item.component}>
108113
{#if item.component}
109114
<svelte:component this={item.component.src} {...getProps()} />

0 commit comments

Comments
 (0)