Skip to content

Commit 8111f2c

Browse files
committed
Allow forwarding of toastId to component-message
1 parent 20681b5 commit 8111f2c

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

cypress/integration/test.spec.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,12 @@ describe('Integration Tests', () => {
167167
})
168168

169169
it('Uses component', () => {
170-
cy.get('[data-btn=sendComponentAsAMessage]')
171-
.click()
172-
.get('._toastItem h1')
173-
.should('have.text', 'A Dummy Cookie Component')
174-
.window().invoke('toast.pop')
170+
cy.get('[data-btn=sendComponentAsAMessage]').click()
171+
.get('._toastItem').contains('A Dummy Cookie Component')
172+
.get('[data-btn=default]').click()
173+
.get('[data-btn=dummyAccept').click()
174+
.get('._toastItem h1').should('not.exist')
175+
.get('._toastBtn').click()
176+
.get('._toastItem').should('not.exist')
175177
})
176178
})

docs/App.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ toast.pop(0)`,
234234
src: DummyComponent,
235235
props: {
236236
title: 'A Dummy Cookie Component'
237-
}
237+
},
238+
sendIdTo: 'toastId'
238239
},
239240
dismissable: false,
240241
initial: 0,
@@ -244,7 +245,7 @@ toast.pop(0)`,
244245
})`,
245246
run: () => {
246247
toast.push({
247-
component: { src: DummyComponent, props: { title: 'A Dummy Cookie Component' } },
248+
component: { src: DummyComponent, props: { title: 'A Dummy Cookie Component' }, sendIdTo: 'toastId' },
248249
dismissable: false,
249250
initial: 0,
250251
theme: { '--toastMsgPadding': '0' }

docs/Dummy.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ const clicked = () => toast.pop(toastId)
1212
<p class="text-sm mx-2 mb-1">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
1313
<div class="flex-1 flex flex-row justify-between">
1414
<button class="w-28" on:click={clicked}>DECLINE</button>
15-
<button class="w-28" on:click={clicked}>ACCEPT</button>
15+
<button class="w-28" on:click={clicked} data-btn="dummyAccept">ACCEPT</button>
1616
</div>
1717
</div>

src/ToastItem.svelte

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ $: if (prev !== item.next) {
1717
prev = item.next
1818
}
1919
20+
const getProps = () => {
21+
const { props, sendIdTo } = item.component
22+
if (sendIdTo) {
23+
props[sendIdTo] = item.id
24+
}
25+
return props
26+
}
27+
2028
// `progress` has been renamed to `next`; shim included for backward compatibility, to remove in next major
2129
$: if (typeof item.progress !== 'undefined') {
2230
item.next = item.progress
@@ -83,7 +91,7 @@ $: if (typeof item.progress !== 'undefined') {
8391
<div class="_toastItem">
8492
<div class="_toastMsg" class:pe={item.component}>
8593
{#if item.component}
86-
<svelte:component this={item.component.src} {...item.component.props} toastId={item.id} />
94+
<svelte:component this={item.component.src} {...getProps()} />
8795
{:else}
8896
{@html item.msg}
8997
{/if}

src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare module '@zerodevx/svelte-toast'
88
* Default Toast Options
99
* ```typescript
1010
* {
11-
* duration: 4000, // duration of progress bar tween from previous to `next`
11+
* duration: 4000, // duration of progress bar tween to the `next` value
1212
* initial: 1, // initial progress bar value
1313
* next: 0, // next progress value
1414
* dismissable: true, // allow dismiss with close button
@@ -33,6 +33,7 @@ export interface SvelteToastOptions {
3333
component?: {
3434
src: object
3535
props?: { [key: string]: any }
36+
sendIdTo?: string
3637
}
3738
}
3839

0 commit comments

Comments
 (0)