5
5
aria-atomic =" true"
6
6
role =" status"
7
7
tabindex =" 0"
8
- data-sonner-toast =" "
8
+ data-sonner-toast =" true "
9
9
:class =" toastClass"
10
10
:data-rich-colors =" toast.richColors ?? defaultRichColors"
11
11
:data-styled =" !Boolean(toast.component || toast?.unstyled || unstyled)"
30
30
'--offset': `${removed ? offsetBeforeRemove : offset}px`,
31
31
'--initial-height': expandByDefault ? 'auto' : `${initialHeight}px`,
32
32
...style,
33
- ...toastStyle,
33
+ ...toastStyle
34
34
}"
35
35
@pointerdown =" onPointerDown"
36
36
@pointerup =" onPointerUp"
40
40
<button
41
41
:aria-label =" closeButtonAriaLabel || 'Close toast'"
42
42
:data-disabled =" disabled"
43
- data-close-button
43
+ data-close-button = " true "
44
44
:class =" cn(classes?.closeButton, toast?.classes?.closeButton)"
45
45
@click =" handleCloseToast"
46
46
>
47
47
<template v-if =" icons ?.close " >
48
- <component :is =" icons?.close" />
48
+ <component :is =" icons?.close" />
49
49
</template >
50
50
<template v-else >
51
51
<slot name =" close-icon" />
99
99
descriptionClass,
100
100
toastDescriptionClass,
101
101
classes?.description,
102
- toast.classes?.description,
102
+ toast.classes?.description
103
103
)
104
104
"
105
105
>
@@ -198,22 +198,24 @@ const toastStyle = props.toast.style || {}
198
198
// Height index is used to calculate the offset as it gets updated before the toast array, which means we can calculate the new layout faster.
199
199
const heightIndex = computed (
200
200
() =>
201
- props .heights .findIndex (height => height .toastId === props .toast .id ) || 0 ,
201
+ props .heights .findIndex (( height ) => height .toastId === props .toast .id ) || 0
202
202
)
203
203
const closeButton = computed (() => props .toast .closeButton ?? props .closeButton )
204
204
const duration = computed (
205
- () => props .toast .duration || props .duration || TOAST_LIFETIME ,
205
+ () => props .toast .duration || props .duration || TOAST_LIFETIME
206
206
)
207
207
208
208
const closeTimerStartTimeRef = ref (0 )
209
209
const offset = ref (0 )
210
210
const lastCloseTimerStartTimeRef = ref (0 )
211
- const pointerStartRef = ref <{ x: number , y: number } | null >(null )
211
+ const pointerStartRef = ref <{ x: number ; y: number } | null >(null )
212
212
const coords = computed (() => props .position .split (' -' ))
213
213
const y = computed (() => coords .value [0 ])
214
214
const x = computed (() => coords .value [1 ])
215
215
const isStringOfTitle = computed (() => typeof props .toast .title !== ' string' )
216
- const isStringOfDescription = computed (() => typeof props .toast .description !== ' string' )
216
+ const isStringOfDescription = computed (
217
+ () => typeof props .toast .description !== ' string'
218
+ )
217
219
218
220
const toastsHeightBefore = computed (() => {
219
221
return props .heights .reduce ((prev , curr , reducerIndex ) => {
@@ -230,8 +232,7 @@ const invert = computed(() => props.toast.invert || props.invert)
230
232
const disabled = computed (() => toastType .value === ' loading' )
231
233
232
234
onMounted (() => {
233
- if (! mounted .value )
234
- return
235
+ if (! mounted .value ) return
235
236
236
237
const toastNode = toastRef .value
237
238
const originalHeight = toastNode ?.style .height
@@ -243,24 +244,23 @@ onMounted(() => {
243
244
244
245
let newHeightArr
245
246
const alreadyExists = props .heights .find (
246
- height => height .toastId === props .toast .id ,
247
+ ( height ) => height .toastId === props .toast .id
247
248
)
248
249
249
250
if (! alreadyExists ) {
250
251
newHeightArr = [
251
252
{
252
253
toastId: props .toast .id ,
253
254
height: newHeight ,
254
- position: props .toast .position ,
255
+ position: props .toast .position
255
256
},
256
- ... props .heights ,
257
+ ... props .heights
257
258
]
258
- }
259
- else {
260
- newHeightArr = props .heights .map (height =>
259
+ } else {
260
+ newHeightArr = props .heights .map ((height ) =>
261
261
height .toastId === props .toast .id
262
262
? { ... height , height: newHeight }
263
- : height ,
263
+ : height
264
264
)
265
265
}
266
266
@@ -271,7 +271,9 @@ function deleteToast() {
271
271
// Save the offset for the exit swipe animation
272
272
removed .value = true
273
273
offsetBeforeRemove .value = offset .value
274
- const height = props .heights .filter (height => height .toastId !== props .toast .id )
274
+ const height = props .heights .filter (
275
+ (height ) => height .toastId !== props .toast .id
276
+ )
275
277
emit (' update:heights' , height )
276
278
277
279
setTimeout (() => {
@@ -289,27 +291,24 @@ function handleCloseToast() {
289
291
}
290
292
291
293
function onPointerDown(event : PointerEvent ) {
292
- if (disabled .value || ! dismissible .value )
293
- return
294
+ if (disabled .value || ! dismissible .value ) return
294
295
dragStartTime .value = new Date ()
295
296
offsetBeforeRemove .value = offset .value
296
297
// Ensure we maintain correct pointer capture even when going outside of the toast (e.g. when swiping)
297
298
;(event .target as HTMLElement ).setPointerCapture (event .pointerId )
298
- if ((event .target as HTMLElement ).tagName === ' BUTTON' )
299
- return
299
+ if ((event .target as HTMLElement ).tagName === ' BUTTON' ) return
300
300
swiping .value = true
301
301
pointerStartRef .value = { x: event .clientX , y: event .clientY }
302
302
}
303
303
304
304
function onPointerUp() {
305
- if (swipeOut .value )
306
- return
305
+ if (swipeOut .value ) return
307
306
pointerStartRef .value = null
308
307
309
308
const swipeAmount = Number (
310
309
toastRef .value ?.style
311
310
.getPropertyValue (' --swipe-amount' )
312
- .replace (' px' , ' ' ) || 0 ,
311
+ .replace (' px' , ' ' ) || 0
313
312
)
314
313
315
314
const timeTaken = new Date ().getTime () - dragStartTime .value ?.getTime ()!
@@ -329,8 +328,7 @@ function onPointerUp() {
329
328
}
330
329
331
330
function onPointerMove(event : PointerEvent ) {
332
- if (! pointerStartRef .value || ! dismissible .value )
333
- return
331
+ if (! pointerStartRef .value || ! dismissible .value ) return
334
332
335
333
const yPosition = event .clientY - pointerStartRef .value .y
336
334
const xPosition = event .clientX - pointerStartRef .value .x
@@ -342,8 +340,7 @@ function onPointerMove(event: PointerEvent) {
342
340
343
341
if (isAllowedToSwipe ) {
344
342
toastRef .value ?.style .setProperty (' --swipe-amount' , ` ${yPosition }px ` )
345
- }
346
- else if (Math .abs (xPosition ) > swipeStartThreshold ) {
343
+ } else if (Math .abs (xPosition ) > swipeStartThreshold ) {
347
344
// User is swiping in wrong direction so we disable swipe gesture
348
345
// for the current pointer down interaction
349
346
pointerStartRef .value = null
@@ -356,9 +353,9 @@ watchEffect(() => {
356
353
357
354
watchEffect ((onInvalidate ) => {
358
355
if (
359
- (props .toast .promise && toastType .value === ' loading' )
360
- || props .toast .duration === Infinity
361
- || props .toast .type === ' loading'
356
+ (props .toast .promise && toastType .value === ' loading' ) ||
357
+ props .toast .duration === Infinity ||
358
+ props .toast .type === ' loading'
362
359
) {
363
360
return
364
361
}
@@ -378,8 +375,7 @@ watchEffect((onInvalidate) => {
378
375
}
379
376
380
377
const startTimer = () => {
381
- if (remainingTime === Infinity )
382
- return
378
+ if (remainingTime === Infinity ) return
383
379
closeTimerStartTimeRef .value = new Date ().getTime ()
384
380
385
381
// Let the toast know it has started
@@ -390,13 +386,12 @@ watchEffect((onInvalidate) => {
390
386
}
391
387
392
388
if (
393
- props .expanded
394
- || props .interacting
395
- || (props .pauseWhenPageIsHidden && isDocumentHidden )
389
+ props .expanded ||
390
+ props .interacting ||
391
+ (props .pauseWhenPageIsHidden && isDocumentHidden )
396
392
) {
397
393
pauseTimer ()
398
- }
399
- else {
394
+ } else {
400
395
startTimer ()
401
396
}
402
397
@@ -419,7 +414,7 @@ onMounted(() => {
419
414
420
415
const newHeights = [
421
416
{ toastId: props .toast .id , height , position: props .toast .position ! },
422
- ... props .heights ,
417
+ ... props .heights
423
418
]
424
419
emit (' update:heights' , newHeights )
425
420
}
@@ -429,9 +424,9 @@ onMounted(() => {
429
424
onUnmounted (() => {
430
425
if (toastRef .value ) {
431
426
const newHeights = props .heights .filter (
432
- height => height .toastId !== props .toast .id ,
427
+ ( height ) => height .toastId !== props .toast .id
433
428
)
434
429
emit (' update:heights' , newHeights )
435
430
}
436
431
})
437
- </script >
432
+ </script >
0 commit comments