-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToast.vue
239 lines (232 loc) · 11.5 KB
/
Toast.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<template>
<TransitionRoot as="template" :show="active">
<div class="fixed top-0 right-0 z-50 overflow-hidden">
<TransitionChild
as="template"
enter="transform ease-out duration-300 transition"
enter-from="translate-y-2 opacity-0 sm:translate-y-0 sm:translate-x-2"
enter-to="translate-y-0 opacity-100 sm:translate-x-0"
leave="transform ease-in duration-100 transition"
leave-from="opacity-100 sm:translate-x-0 translate-y-0"
leave-to="opacity-0 sm:translate-x-1 translate-y-1 sm:translate-y-0"
>
<div
:class="[
!toast.primary ? 'max-w-sm' : 'max-w-md',
'w-screen p-3'
]"
>
<div
v-if="active && !toast.primary"
:class="[
toast.type == ToastType.DENIED
? 'bg-danger-600'
: 'bg-white',
'relative overflow-hidden rounded-lg p-3 shadow-lg'
]"
>
<div class="flex items-start">
<ToastIcon :type="toast.type" />
<div class="ml-3 grow pt-0.5">
<p
v-if="toast.title"
:class="[
toast.type == ToastType.DENIED
? 'text-danger-100'
: 'text-gray-900',
'text-sm font-medium leading-5'
]"
>
{{ toast.title }}
</p>
<p
:class="[
toast.type == ToastType.DENIED
? 'text-danger-200'
: 'text-gray-500',
toast.title ? 'mt-1' : ''
]"
class="text-sm leading-5"
v-html="toast.message"
></p>
</div>
<div class="ml-4 shrink-0">
<button
class="inline-flex text-gray-400 transition duration-150 ease-in-out focus:text-gray-500 focus:outline-none"
@click="destroy"
>
<ph-x
:class="[
toast.type == ToastType.DENIED
? 'text-danger-300'
: 'text-gray-400',
'h-4 w-4'
]"
/>
</button>
</div>
</div>
</div>
<div
v-if="active && toast.primary && toast.secondary"
:class="[
toast.type == ToastType.DENIED
? 'bg-danger-600'
: 'bg-white',
'rounded-lg shadow-lg'
]"
>
<div class="shadow-xs flex rounded-lg">
<div class="flex grow items-center p-3">
<ToastIcon class="mr-4" :type="toast.type" />
<div class="w-full">
<p
v-if="toast.title"
:class="[
toast.type == ToastType.DENIED
? 'text-danger-100'
: 'text-gray-900',
'text-sm font-medium leading-5'
]"
>
{{ toast.title }}
</p>
<p
:class="[
toast.type == ToastType.DENIED
? 'text-danger-200'
: 'text-gray-500',
toast.title ? 'mt-1' : ''
]"
class="text-sm leading-5"
v-html="toast.message"
></p>
</div>
</div>
<div class="flex border-l border-gray-200">
<div class="flex flex-col">
<div
class="flex grow border-b border-gray-200"
>
<button
class="flex w-full items-center justify-center rounded-tr-lg border border-transparent px-4 py-1 text-sm font-medium leading-5 transition duration-150 ease-in-out focus:outline-none"
:class="[
toast.type == ToastType.DENIED
? 'text-danger-100'
: 'focus:shadow-outline-blue text-secondary-500 hover:text-secondary-400'
]"
@click="primaryAction"
>
{{ toast.primary.label }}
</button>
</div>
<div class="flex grow">
<button
class="flex w-full items-center justify-center rounded-br-lg border border-transparent px-4 py-1 text-sm font-medium leading-5 transition duration-150 ease-in-out focus:outline-none"
:class="
toast.type == ToastType.DENIED
? ''
: 'focus:shadow-outline-blue text-gray-700 hover:text-gray-500 active:bg-gray-50 active:text-gray-800'
"
@click="secondaryAction"
>
{{ toast.secondary.label }}
</button>
</div>
</div>
</div>
</div>
</div>
<div
v-if="active && toast.primary && !toast.secondary"
:class="[
toast.type == ToastType.DENIED
? 'bg-danger-600'
: 'bg-white',
'rounded-lg shadow-lg'
]"
>
<div class="shadow-xs overflow-hidden rounded-lg p-4">
<div class="flex items-center">
<ToastIcon class="mr-4" :type="toast.type" />
<div class="flex grow justify-between">
<p
class="grow text-sm leading-5"
v-html="toast.message"
></p>
<button
class="ml-3 shrink-0 text-sm font-medium leading-5 transition duration-150 ease-in-out focus:outline-none"
:class="
toast.type == ToastType.DENIED
? ''
: 'text-secondary-500 hover:text-secondary-400'
"
@click="primaryAction"
>
{{ toast.primary.label }}
</button>
</div>
<div class="ml-4 flex shrink-0">
<button
class="inline-flex text-gray-400 transition duration-150 ease-in-out focus:text-gray-500 focus:outline-none"
@click="destroy"
>
<ph-x
:class="[
toast.type == ToastType.DENIED
? 'text-danger-300'
: 'text-gray-400',
'h-4 w-4'
]"
/>
</button>
</div>
</div>
</div>
</div>
</div>
</TransitionChild>
</div>
</TransitionRoot>
</template>
<script lang="ts" setup>
import { TransitionChild, TransitionRoot } from '@headlessui/vue';
import { PhX } from 'phosphor-vue';
import { Toast, ToastType } from '~~/models/toast';
// Get the current Toast and active boolean from useState
const active = useState<boolean>('showToast', () => false);
const toast = useState<Toast>('toast');
const interval = ref(null);
const timeLeft = ref(0);
const speed = ref(100);
// When the toast is shown, start the interval timer
watch(
() => active.value,
(isOpen, prevIsOpen) => {
if (isOpen && toast.value.timeout > 0) {
timeLeft.value = toast.value.timeout * 1000;
interval.value = setInterval(() => updateTime(), speed.value);
}
}
);
// Update the interval timer
const updateTime = () => {
timeLeft.value -= speed.value;
if (timeLeft.value === 0) destroy();
};
// Remove the current Toast
const destroy = () => {
active.value = false;
clearInterval(interval.value);
};
// Callback for the primary action
const primaryAction = () => {
toast.value.primary.action();
destroy();
};
// Callback for the secondary action
const secondaryAction = () => {
toast.value.secondary.action();
destroy();
};
</script>