72
72
73
73
<template v-else >
74
74
<template v-if =" toastType !== ' default' || toast .icon || toast .promise " >
75
- <div data-icon =" " >
75
+ <div data-icon =" " :class = " cn(classes?.icon, toast?.classes?.icon) " >
76
76
<template
77
77
v-if =" (toast .promise || toastType === ' loading' ) && ! toast .icon "
78
78
>
153
153
if (!isAction(toast.action!)) return;
154
154
if (event.defaultPrevented) return;
155
155
toast.action.onClick?.(event);
156
+ if (event.defaultPrevented) return;
156
157
deleteToast();
157
158
}
158
159
"
167
168
<script lang="ts" setup>
168
169
import ' ./styles.css'
169
170
170
- import { computed , onMounted , onUnmounted , ref , watch , watchEffect } from ' vue'
171
+ import { computed , onBeforeUnmount , onMounted , onUnmounted , ref , watch , watchEffect } from ' vue'
171
172
import { type HeightT , type ToastProps , type ToastT , isAction } from ' ./types'
172
173
import { useIsDocumentHidden } from ' ./hooks'
173
174
@@ -189,8 +190,10 @@ const mounted = ref(false)
189
190
const removed = ref (false )
190
191
const swiping = ref (false )
191
192
const swipeOut = ref (false )
193
+ const swiped = ref (false )
192
194
const offsetBeforeRemove = ref (0 )
193
195
const initialHeight = ref (0 )
196
+ const remainingTime = ref (props .toast .duration || props .duration || TOAST_LIFETIME )
194
197
const dragStartTime = ref <Date | null >(null )
195
198
const toastRef = ref <HTMLLIElement | null >(null )
196
199
const isFront = computed (() => props .index === 0 )
@@ -213,7 +216,6 @@ const duration = computed(
213
216
)
214
217
215
218
const closeTimerStartTimeRef = ref (0 )
216
- const offset = ref (0 )
217
219
const lastCloseTimerStartTimeRef = ref (0 )
218
220
const pointerStartRef = ref <{ x: number ; y: number } | null >(null )
219
221
const coords = computed (() => props .position .split (' -' ))
@@ -238,6 +240,10 @@ const isDocumentHidden = useIsDocumentHidden()
238
240
const invert = computed (() => props .toast .invert || props .invert )
239
241
const disabled = computed (() => toastType .value === ' loading' )
240
242
243
+ const offset = computed (() => {
244
+ return heightIndex .value * props .gap ! + toastsHeightBefore .value || 0
245
+ })
246
+
241
247
onMounted (() => {
242
248
if (! mounted .value ) return
243
249
@@ -250,6 +256,7 @@ onMounted(() => {
250
256
initialHeight .value = newHeight
251
257
252
258
let newHeightArr
259
+
253
260
const alreadyExists = props .heights .find (
254
261
(height ) => height .toastId === props .toast .id
255
262
)
@@ -290,7 +297,7 @@ function deleteToast() {
290
297
291
298
function handleCloseToast() {
292
299
if (disabled .value || ! dismissible .value ) {
293
- return
300
+ return {}
294
301
}
295
302
296
303
deleteToast ()
@@ -309,7 +316,7 @@ function onPointerDown(event: PointerEvent) {
309
316
}
310
317
311
318
function onPointerUp() {
312
- if (swipeOut .value ) return
319
+ if (swipeOut .value || ! dismissible ) return ;
313
320
pointerStartRef .value = null
314
321
315
322
const swipeAmount = Number (
@@ -327,6 +334,7 @@ function onPointerUp() {
327
334
props .toast .onDismiss ?.(props .toast )
328
335
deleteToast ()
329
336
swipeOut .value = true
337
+ swiped .value = false
330
338
return
331
339
}
332
340
@@ -338,25 +346,20 @@ function onPointerMove(event: PointerEvent) {
338
346
if (! pointerStartRef .value || ! dismissible .value ) return
339
347
340
348
const yPosition = event .clientY - pointerStartRef .value .y
341
- const xPosition = event .clientX - pointerStartRef .value .x
342
-
343
- const clamp = coords .value [0 ] === ' top' ? Math .min : Math .max
344
- const clampedY = clamp (0 , yPosition )
345
- const swipeStartThreshold = event .pointerType === ' touch' ? 10 : 2
346
- const isAllowedToSwipe = Math .abs (clampedY ) > swipeStartThreshold
347
-
348
- if (isAllowedToSwipe ) {
349
- toastRef .value ?.style .setProperty (' --swipe-amount' , ` ${yPosition }px ` )
350
- } else if (Math .abs (xPosition ) > swipeStartThreshold ) {
351
- // User is swiping in wrong direction so we disable swipe gesture
352
- // for the current pointer down interaction
353
- pointerStartRef .value = null
349
+
350
+ // @ts-expect-error
351
+ const isHighlighted = window .getSelection ()?.toString ().length > 0 ;
352
+
353
+ const swipeAmount = y .value === ' top' ? Math .min (0 , yPosition ) : Math .max (0 , yPosition );
354
+
355
+ if (Math .abs (swipeAmount ) > 0 ) {
356
+ swiped .value = true ;
354
357
}
355
- }
356
358
357
- watchEffect (() => {
358
- offset .value = heightIndex .value * props ?.gap ! + toastsHeightBefore .value
359
- })
359
+ if (isHighlighted ) return ;
360
+
361
+ toastRef .value ?.style .setProperty (' --swipe-amount' , ` ${swipeAmount }px ` );
362
+ }
360
363
361
364
watchEffect ((onInvalidate ) => {
362
365
if (
@@ -367,29 +370,28 @@ watchEffect((onInvalidate) => {
367
370
return
368
371
}
369
372
let timeoutId: ReturnType <typeof setTimeout >
370
- let remainingTime = duration .value
371
373
372
374
// Pause the timer on each hover
373
375
const pauseTimer = () => {
374
376
if (lastCloseTimerStartTimeRef .value < closeTimerStartTimeRef .value ) {
375
377
// Get the elapsed time since the timer started
376
378
const elapsedTime = new Date ().getTime () - closeTimerStartTimeRef .value
377
379
378
- remainingTime = remainingTime - elapsedTime
380
+ remainingTime . value = remainingTime . value - elapsedTime
379
381
}
380
382
381
383
lastCloseTimerStartTimeRef .value = new Date ().getTime ()
382
384
}
383
385
384
386
const startTimer = () => {
385
- if (remainingTime === Infinity ) return
387
+ if (remainingTime . value === Infinity ) return
386
388
closeTimerStartTimeRef .value = new Date ().getTime ()
387
389
388
390
// Let the toast know it has started
389
391
timeoutId = setTimeout (() => {
390
392
props .toast .onAutoClose ?.(props .toast )
391
393
deleteToast ()
392
- }, remainingTime )
394
+ }, remainingTime . value )
393
395
}
394
396
395
397
if (
@@ -417,6 +419,8 @@ watch(
417
419
)
418
420
419
421
onMounted (() => {
422
+ mounted .value = true
423
+
420
424
if (toastRef .value ) {
421
425
const height = toastRef .value .getBoundingClientRect ().height
422
426
// Add toast height tot heights array after the toast is mounted
@@ -428,10 +432,9 @@ onMounted(() => {
428
432
]
429
433
emit (' update:heights' , newHeights )
430
434
}
431
- mounted .value = true
432
435
})
433
436
434
- onUnmounted (() => {
437
+ onBeforeUnmount (() => {
435
438
if (toastRef .value ) {
436
439
const newHeights = props .heights .filter (
437
440
(height ) => height .toastId !== props .toast .id
0 commit comments