Skip to content

Commit 1c9b976

Browse files
committedNov 21, 2024
fix: fixed the position bug
1 parent 5cb93ff commit 1c9b976

File tree

5 files changed

+78
-53
lines changed

5 files changed

+78
-53
lines changed
 

‎app/components/Position.vue

+6-5
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040

4141
<script lang="ts" setup>
4242
import type { PropType } from 'vue'
43-
import { ref, computed } from 'vue'
43+
import { ref, computed, watch } from 'vue'
4444
45-
import { toast } from '@/packages'
45+
import { toast, useVueSonner } from '@/packages'
4646
import type { Position } from '@/packages/types'
4747
import { useCopyCode } from '~/composables/useCopyCode'
4848
import CopyIcon from '~/components/icons/CopyIcon.vue'
@@ -71,11 +71,12 @@ const renderedCode = computed(() => {
7171
const showCheckIcon = ref(false)
7272
7373
const handleChangePosition = (activePosition: Position) => {
74-
const toastsAmount = document.querySelectorAll('[data-sonner-toast]').length
74+
toast.dismiss()
7575
emit('update:position', activePosition)
7676
77-
// No need to show a toast when there is already one
78-
if (toastsAmount > 0 && props.position !== activePosition) return
77+
// const toastsAmount = document.querySelectorAll('[data-sonner-toast]').length
78+
// // No need to show a toast when there is already one
79+
// if (toastsAmount > 0 && props.position !== activePosition) return
7980
8081
toast('Event has been created', {
8182
description: 'Monday, January 3rd at 6:00pm'

‎src/packages/Sonner.vue

-9
This file was deleted.

‎src/packages/Toast.vue

+40-22
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
role="status"
77
tabindex="0"
88
data-sonner-toast="true"
9-
:class="cn(
10-
props.class,
11-
toastClass,
12-
classes?.toast,
13-
toast.classes?.toast,
14-
// @ts-ignore
15-
classes?.[toastType],
16-
// @ts-ignore
17-
toast?.classes?.[toastType]
18-
)"
9+
:class="
10+
cn(
11+
props.class,
12+
toastClass,
13+
classes?.toast,
14+
toast.classes?.toast,
15+
// @ts-ignore
16+
classes?.[toastType],
17+
// @ts-ignore
18+
toast?.classes?.[toastType]
19+
)
20+
"
1921
:data-rich-colors="toast.richColors ?? defaultRichColors"
2022
:data-styled="!Boolean(toast.component || toast?.unstyled || unstyled)"
2123
:data-mounted="mounted"
@@ -90,7 +92,10 @@
9092
</div>
9193
</template>
9294

93-
<div data-content="" :class="cn(classes?.content, toast?.classes?.content)">
95+
<div
96+
data-content=""
97+
:class="cn(classes?.content, toast?.classes?.content)"
98+
>
9499
<div data-title="" :class="cn(classes?.title, toast.classes?.title)">
95100
<template v-if="isStringOfTitle">
96101
<component :is="toast.title" v-bind="toast.componentProps" />
@@ -168,7 +173,14 @@
168173
<script lang="ts" setup>
169174
import './styles.css'
170175
171-
import { computed, onBeforeUnmount, onMounted, onUnmounted, ref, watch, watchEffect } from 'vue'
176+
import {
177+
computed,
178+
onBeforeUnmount,
179+
onMounted,
180+
ref,
181+
watch,
182+
watchEffect
183+
} from 'vue'
172184
import { type HeightT, type ToastProps, type ToastT, isAction } from './types'
173185
import { useIsDocumentHidden } from './hooks'
174186
@@ -193,7 +205,9 @@ const swipeOut = ref(false)
193205
const swiped = ref(false)
194206
const offsetBeforeRemove = ref(0)
195207
const initialHeight = ref(0)
196-
const remainingTime = ref(props.toast.duration || props.duration || TOAST_LIFETIME)
208+
const remainingTime = ref(
209+
props.toast.duration || props.duration || TOAST_LIFETIME
210+
)
197211
const dragStartTime = ref<Date | null>(null)
198212
const toastRef = ref<HTMLLIElement | null>(null)
199213
const isFront = computed(() => props.index === 0)
@@ -256,7 +270,7 @@ onMounted(() => {
256270
initialHeight.value = newHeight
257271
258272
let newHeightArr
259-
273+
260274
const alreadyExists = props.heights.find(
261275
(height) => height.toastId === props.toast.id
262276
)
@@ -316,7 +330,7 @@ function onPointerDown(event: PointerEvent) {
316330
}
317331
318332
function onPointerUp() {
319-
if (swipeOut.value || !dismissible) return;
333+
if (swipeOut.value || !dismissible) return
320334
pointerStartRef.value = null
321335
322336
const swipeAmount = Number(
@@ -348,17 +362,18 @@ function onPointerMove(event: PointerEvent) {
348362
const yPosition = event.clientY - pointerStartRef.value.y
349363
350364
// @ts-expect-error
351-
const isHighlighted = window.getSelection()?.toString().length > 0;
365+
const isHighlighted = window.getSelection()?.toString().length > 0
352366
353-
const swipeAmount = y.value === 'top' ? Math.min(0, yPosition) : Math.max(0, yPosition);
367+
const swipeAmount =
368+
y.value === 'top' ? Math.min(0, yPosition) : Math.max(0, yPosition)
354369
355370
if (Math.abs(swipeAmount) > 0) {
356-
swiped.value = true;
371+
swiped.value = true
357372
}
358373
359-
if (isHighlighted) return;
374+
if (isHighlighted) return
360375
361-
toastRef.value?.style.setProperty('--swipe-amount', `${swipeAmount}px`);
376+
toastRef.value?.style.setProperty('--swipe-amount', `${swipeAmount}px`)
362377
}
363378
364379
watchEffect((onInvalidate) => {
@@ -411,10 +426,13 @@ watchEffect((onInvalidate) => {
411426
412427
watch(
413428
() => props.toast.delete,
414-
(value) => {
415-
if (value) {
429+
() => {
430+
if (props.toast.delete) {
416431
deleteToast()
417432
}
433+
},
434+
{
435+
deep: true
418436
}
419437
)
420438

‎src/packages/Toaster.vue

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<template>
22
<!-- Remove item from normal navigation flow, only available via hotkey -->
3-
<section :aria-label="`${containerAriaLabel} ${hotkeyLabel}`" :tabIndex="-1" aria-live="polite" aria-relevant="additions text" aria-atomic="false">
3+
<section
4+
:aria-label="`${containerAriaLabel} ${hotkeyLabel}`"
5+
:tabIndex="-1"
6+
aria-live="polite"
7+
aria-relevant="additions text"
8+
aria-atomic="false"
9+
>
410
<template v-for="(pos, index) in possiblePositions" :key="pos">
511
<ol
612
ref="listRef"
@@ -40,10 +46,7 @@
4046
@pointerup="() => (interacting = false)"
4147
>
4248
<template
43-
v-for="(toast, idx) in toasts.filter(
44-
(toast) =>
45-
(!toast.position && index === 0) || toast.position === pos
46-
)"
49+
v-for="(toast, idx) in filteredToasts(pos, index)"
4750
:key="toast.id"
4851
>
4952
<Toast
@@ -142,7 +145,15 @@ function _cn(...classes: (string | undefined)[]) {
142145
</script>
143146

144147
<script lang="ts" setup>
145-
import { computed, nextTick, ref, useAttrs, watch, watchEffect } from 'vue'
148+
import {
149+
computed,
150+
nextTick,
151+
ref,
152+
toRaw,
153+
useAttrs,
154+
watch,
155+
watchEffect
156+
} from 'vue'
146157
import type {
147158
HeightT,
148159
Position,
@@ -200,6 +211,12 @@ function getDocumentDirection(): ToasterProps['dir'] {
200211
201212
const attrs = useAttrs()
202213
const toasts = ref<ToastT[]>([])
214+
const filteredToasts = computed(() => {
215+
return (pos: string, index: number) =>
216+
toasts.value.filter(
217+
(toast) => (!toast.position && index === 0) || toast.position === pos
218+
)
219+
})
203220
const possiblePositions = computed(() => {
204221
const posList = toasts.value
205222
.filter((toast) => toast.position)
@@ -303,9 +320,7 @@ watchEffect((onInvalidate) => {
303320
})
304321
})
305322
306-
onInvalidate(() => {
307-
unsubscribe()
308-
})
323+
onInvalidate(unsubscribe)
309324
})
310325
311326
watch(
@@ -332,7 +347,7 @@ watch(
332347
333348
if (typeof window === 'undefined') return
334349
335-
const darkMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
350+
const darkMediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
336351
337352
try {
338353
// Chrome & Firefox
@@ -343,7 +358,7 @@ watch(
343358
actualTheme.value = 'light'
344359
}
345360
})
346-
} catch(error) {
361+
} catch (error) {
347362
darkMediaQuery.addListener(({ matches }) => {
348363
try {
349364
if (matches) {
@@ -352,18 +367,18 @@ watch(
352367
actualTheme.value = 'light'
353368
}
354369
} catch (e) {
355-
console.error(e);
370+
console.error(e)
356371
}
357-
});
372+
})
358373
}
359374
}
360375
)
361376
362377
watchEffect(() => {
363378
if (listRef.value && lastFocusedElementRef.value) {
364-
lastFocusedElementRef.value.focus({ preventScroll: true })
365-
lastFocusedElementRef.value = null
366-
isFocusWithinRef.value = false
379+
lastFocusedElementRef.value.focus({ preventScroll: true })
380+
lastFocusedElementRef.value = null
381+
isFocusWithinRef.value = false
367382
}
368383
})
369384

‎src/packages/state.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010

1111
let toastsCounter = 0
1212

13-
type titleT = (() => string | Component) | string | Component;
13+
type titleT = (() => string | Component) | string | Component
1414

1515
class Observer {
1616
subscribers: Array<(toast: ExternalToast | ToastToDismiss) => void>

0 commit comments

Comments
 (0)
Please sign in to comment.