Skip to content

Commit 10c05c4

Browse files
Merge pull request #132 from oxwazz/main
fix: custom component not trigger on dismiss function
2 parents 4491642 + 70ef6e2 commit 10c05c4

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

app/components/HeadlessToast.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<script setup lang="ts">
2+
import { toast } from '@/packages'
3+
24
defineProps<{ msg?: string }>()
35
</script>
46

57
<template>
68
<div class="headless">
79
<p class="headlessTitle">Event Created {{ msg }}</p>
810
<p class="headlessDescription">Today at 4:00pm - "Louvre Museum"</p>
11+
<button class="headlessCloseAll" @click="toast.dismiss()">
12+
Close all
13+
</button>
914
<button class="headlessClose" @click="$emit('closeToast')">
1015
<svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">
1116
<path
@@ -28,7 +33,7 @@ defineProps<{ msg?: string }>()
2833
}
2934
3035
.headlessDescription {
31-
margin: 0;
36+
margin: 0 0 8px;
3237
color: var(--gray10);
3338
font-size: 14px;
3439
line-height: 1;
@@ -42,6 +47,22 @@ defineProps<{ msg?: string }>()
4247
line-height: 1;
4348
}
4449
50+
.headlessCloseAll {
51+
font-size: 14px;
52+
color: var(--gray10);
53+
font-weight: 500;
54+
line-height: 1;
55+
border: 1px solid var(--gray10);
56+
border-radius: 4px;
57+
padding: 6px 8px;
58+
transition: color 200ms, border 200ms;
59+
}
60+
61+
.headlessCloseAll:hover {
62+
color: var(--gray12);
63+
border: 1px solid var(--gray12);
64+
}
65+
4566
.headlessClose {
4667
position: absolute;
4768
cursor: pointer;

src/packages/state.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,27 @@ class Observer {
299299
// We can't provide the toast we just created as a prop as we didn't create it yet, so we can create a default toast object, I just don't know how to use function in argument when calling()?
300300
custom = (component: Component, data?: ExternalToast) => {
301301
const id = data?.id || toastsCounter++
302-
this.publish({ component, id, ...data })
302+
const alreadyExists = this.toasts.find((toast) => {
303+
return toast.id === id
304+
})
305+
const dismissible = data?.dismissible === undefined ? true : data.dismissible
306+
307+
if (this.dismissedToasts.has(id)) {
308+
this.dismissedToasts.delete(id)
309+
}
310+
311+
if (alreadyExists) {
312+
this.toasts = this.toasts.map((toast) => {
313+
if (toast.id === id) {
314+
this.publish({ ...toast, component, dismissible, id, ...data })
315+
return { ...toast, component, dismissible, id, ...data, }
316+
}
317+
318+
return toast
319+
})
320+
} else {
321+
this.addToast({ component, dismissible, id, ...data })
322+
}
303323
return id
304324
}
305325

0 commit comments

Comments
 (0)