Skip to content

Commit 172c1f7

Browse files
committed
Add feature pause on mouse hover
1 parent 8111f2c commit 172c1f7

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed

cypress/integration/test.spec.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,42 @@ describe('Integration Tests', () => {
172172
.get('[data-btn=default]').click()
173173
.get('[data-btn=dummyAccept').click()
174174
.get('._toastItem h1').should('not.exist')
175-
.get('._toastBtn').click()
176-
.get('._toastItem').should('not.exist')
175+
.window().invoke('toast.pop', 0)
176+
})
177+
178+
it('Pauses on hover', () => {
179+
cy.get('[data-btn=pauseOnMouseHover]').click()
180+
.get('._toastItem').trigger('mouseenter')
181+
.get('._toastBar').then($bar => {
182+
const old = parseFloat($bar.val())
183+
cy.wait(50).then(() => {
184+
expect(parseFloat($bar.val())).to.equal(old)
185+
})
186+
})
187+
.get('._toastItem').trigger('mouseleave')
188+
.get('._toastBar').then($bar => {
189+
const old = parseFloat($bar.val())
190+
cy.wait(50).then(() => {
191+
expect(parseFloat($bar.val())).to.be.below(old)
192+
}).get('._toastBtn').click()
193+
})
194+
})
195+
196+
it('Does not pause on hover if `pausable` is false', () => {
197+
cy.get('[data-btn=default]').click()
198+
.get('._toastItem').trigger('mouseenter', { force: true })
199+
.get('._toastBar').then($bar => {
200+
const old = parseFloat($bar.val())
201+
cy.wait(50).then(() => {
202+
expect(parseFloat($bar.val())).to.be.below(old)
203+
})
204+
})
205+
.get('._toastItem').trigger('mouseleave', { force: true })
206+
.get('._toastBar').then($bar => {
207+
const old = parseFloat($bar.val())
208+
cy.wait(50).then(() => {
209+
expect(parseFloat($bar.val())).to.be.below(old)
210+
}).get('._toastBtn').click()
211+
})
177212
})
178213
})

src/SvelteToast.svelte

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ ul {
3232

3333
<ul>
3434
{#each items as item (item.id)}
35-
<li
36-
in:fly={item.intro}
37-
out:fade
38-
animate:flip={{ duration: 200 }}
39-
style={getCss(item.theme)}
40-
>
35+
<li in:fly={item.intro} out:fade animate:flip={{ duration: 200 }} style={getCss(item.theme)}>
4136
<ToastItem {item} />
4237
</li>
4338
{/each}

src/ToastItem.svelte

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,34 @@ import { toast } from './stores.js'
66
export let item
77
88
const progress = tweened(item.initial, { duration: item.duration, easing: linear })
9+
const autoclose = () => {
10+
if ($progress === 1 || $progress === 0) {
11+
toast.pop(item.id)
12+
}
13+
}
914
let prev = item.initial
1015
1116
$: if (prev !== item.next) {
12-
if (item.next === 1 || item.next === 0) {
13-
progress.set(item.next).then(() => toast.pop(item.id))
14-
} else {
15-
progress.set(item.next)
16-
}
17+
progress.set(item.next).then(autoclose)
1718
prev = item.next
1819
}
1920
21+
const pause = () => {
22+
if (item.pausable) {
23+
progress.set($progress, { duration: 0 })
24+
}
25+
}
26+
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+
}
33+
}
34+
2035
const getProps = () => {
21-
const { props, sendIdTo } = item.component
36+
const { props = {}, sendIdTo } = item.component
2237
if (sendIdTo) {
2338
props[sendIdTo] = item.id
2439
}
@@ -88,7 +103,7 @@ $: if (typeof item.progress !== 'undefined') {
88103
}
89104
</style>
90105

91-
<div class="_toastItem">
106+
<div class="_toastItem" class:pe={item.pausable} on:mouseenter={pause} on:mouseleave={play}>
92107
<div class="_toastMsg" class:pe={item.component}>
93108
{#if item.component}
94109
<svelte:component this={item.component.src} {...getProps()} />

src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ declare module '@zerodevx/svelte-toast'
1212
* initial: 1, // initial progress bar value
1313
* next: 0, // next progress value
1414
* dismissable: true, // allow dismiss with close button
15+
* pausable: false, // allow pause on mouse hover
1516
* reversed: false, // insert new toast to bottom of stack
1617
* intro: { x: 256 }, // toast intro fly animation settings
1718
* theme: {} // css var overrides
@@ -27,6 +28,7 @@ export interface SvelteToastOptions {
2728
next?: number
2829
progress?: number
2930
dismissable?: boolean
31+
pausable?: boolean
3032
reversed?: boolean
3133
intro?: FlyParams
3234
theme?: { [key: string]: string }

src/stores.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const defaults = {
55
initial: 1,
66
next: 0,
77
dismissable: true,
8+
pausable: false,
89
reversed: false,
910
intro: { x: 256 },
1011
theme: {}

0 commit comments

Comments
 (0)