Skip to content

Commit 30375a0

Browse files
authored
Add support for component-messages (#24)
1 parent 6d94692 commit 30375a0

File tree

5 files changed

+93
-42
lines changed

5 files changed

+93
-42
lines changed

cypress/integration/test.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ describe('Integration Tests', () => {
9696
.click()
9797
})
9898

99+
it('Uses component', () => {
100+
cy.get('[data-btn=useComponent]')
101+
.click()
102+
.get('._toastItem span')
103+
.should('have.text', 'I am a Dummy.svelte component with property foo=bar (id: 10)')
104+
.get('._toastBtn')
105+
.click()
106+
})
107+
99108
it('Restores defaults', () => {
100109
cy.get('[data-btn=restoreDefaults]')
101110
.click()
@@ -138,7 +147,10 @@ describe('Integration Tests', () => {
138147
.get('._toastItem')
139148
.should('have.css', 'top', '0px')
140149
.get('._toastBtn')
141-
.click()
150+
.should($e => {
151+
expect($e).to.have.length(2)
152+
})
153+
cy.window().invoke('toast.pop', 2)
142154
})
143155

144156
it('Removes all toasts from selected container target', () => {

docs/App.svelte

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { tick } from 'svelte'
33
import { SvelteToast, toast } from '../src'
44
import Prism from './Prism.svelte'
5+
import DummyComponent from './Dummy.svelte'
56
import camelCase from 'camelcase'
67
78
// Hoist to `window` for debug
@@ -175,6 +176,28 @@ toast.set(id, { progress: 1 })`,
175176
toast.push('Bottoms up!')
176177
}
177178
},
179+
{
180+
name: 'USE COMPONENT',
181+
code: `
182+
toast.push({
183+
component: {
184+
src: DummyComponent,
185+
props: {
186+
foo: 'bar'
187+
}
188+
}
189+
})`,
190+
run: async () => {
191+
toast.push({
192+
component: {
193+
src: DummyComponent,
194+
props: {
195+
foo: 'bar'
196+
}
197+
}
198+
})
199+
}
200+
},
178201
{
179202
name: 'RESTORE DEFAULTS',
180203
code: '// All default settings restored!',
@@ -189,29 +212,36 @@ toast.set(id, { progress: 1 })`,
189212
{
190213
name: 'CREATE NEW TOAST CONTAINER',
191214
code: `<style>
192-
.wrap {
193-
--toastContainerTop: 0.5rem;
194-
--toastContainerRight: 2rem;
195-
--toastContainerBottom: auto;
196-
--toastContainerLeft: 2rem;
197-
--toastWidth: 100%;
198-
--toastMinHeight: 1.5rem;
199-
--toastBackground: rgba(59,130,246,0.95);
200-
--toastMsgPadding: 0.25rem 0.5rem;
201-
font-size: 0.875rem;
202-
}
215+
.wrap {
216+
--toastContainerTop: 0.5rem;
217+
--toastContainerRight: 2rem;
218+
--toastContainerBottom: auto;
219+
--toastContainerLeft: 2rem;
220+
--toastWidth: 100%;
221+
--toastMinHeight: 1.5rem;
222+
--toastBackground: rgba(59,130,246,0.95);
223+
--toastMsgPadding: 0.25rem 0.5rem;
224+
font-size: 0.875rem;
225+
}
203226
</style>
204227
205228
<div class="wrap">
206229
<SvelteToast target="new" options={{ initial: 0, intro: { y: -64 } }} />
207230
</div>
208231
232+
<div>
233+
<SvelteToast options={{ initial: 0, intro: { y: -64 } }} />
234+
</div>
235+
209236
<script>
210237
// Send toast to "new" container
211-
toast.push('NEW: Multiple toast container support', { target: 'new' })
238+
toast.push('NEW: Multiple toast container support', { target: 'new' });
239+
// Send toast to "default" container
240+
toast.push('Default container toast')
212241
<\/script>`, // eslint-disable-line no-useless-escape
213242
run: () => {
214243
toast.push('NEW: Multiple toast container support', { target: 'new' })
244+
toast.push('Default container toast')
215245
}
216246
},
217247
{
@@ -252,32 +282,32 @@ toast.pop(0)`,
252282
font-size: 0.875rem;
253283
}
254284
</style>
255-
256-
<div class="container">
257-
<div class="w-full h-64 px-2 mt-4 mb-8">
258-
<Prism classes="w-full h-full bg-gray-700 text-gray-200 font-mono shadow rounded-sm overflow-scroll p-4">
259-
{code}
260-
</Prism>
285+
286+
<div class="container">
287+
<div class="w-full h-64 px-2 mt-4 mb-8">
288+
<Prism classes="w-full h-full bg-gray-700 text-gray-200 font-mono shadow rounded-sm overflow-scroll p-4">
289+
{code}
290+
</Prism>
291+
</div>
292+
293+
<div class="flex flex-row flex-wrap items-center justify-center">
294+
295+
{#each buttons as btn}
296+
<button
297+
class:selected={selected === btn.name}
298+
on:click={() => { handleClick(btn) }}
299+
data-btn={camelCase(btn.name)}>
300+
{btn.name}
301+
</button>
302+
{/each}
303+
304+
</div>
261305
</div>
262-
263-
<div class="flex flex-row flex-wrap items-center justify-center">
264-
265-
{#each buttons as btn}
266-
<button
267-
class:selected={selected === btn.name}
268-
on:click={() => { handleClick(btn) }}
269-
data-btn={camelCase(btn.name)}>
270-
{btn.name}
271-
</button>
272-
{/each}
273-
306+
307+
<div class="top">
308+
<SvelteToast options={{ initial: 0, intro: { y: -64 } }} target="new" />
274309
</div>
275-
</div>
276-
277-
<div class="top">
278-
<SvelteToast options={{ initial: 0, intro: { y: -64 } }} target="new" />
279-
</div>
280-
281-
<div class:colors class:bottom>
282-
<SvelteToast {options} />
283-
</div>
310+
311+
<div class:colors class:bottom>
312+
<SvelteToast {options} />
313+
</div>

docs/Dummy.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
export let foo
3+
export let toastId
4+
</script>
5+
<span>I am a Dummy.svelte component with property foo={foo} (id: {toastId})</span>

src/SvelteToast.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ul {
2525
margin: 0;
2626
padding: 0;
2727
list-style-type: none;
28-
pointer-events: none;
28+
pointer-events: var(--toastContainerPointerEvents, auto);
2929
z-index: 9999;
3030
}
3131
</style>

src/ToastItem.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ $: if (prevProgress !== item.progress) {
7979

8080
<div class="_toastItem">
8181
<div class="_toastMsg">
82-
{@html item.msg}
82+
{#if item.component}
83+
<svelte:component this="{item.component.src}" { ...item.component.props} toastId={item.id} />
84+
{:else}
85+
{@html item.msg}
86+
{/if}
8387
</div>
8488

8589
{#if item.dismissable}

0 commit comments

Comments
 (0)