Skip to content

Commit 0c84b97

Browse files
committed
chore: wip
1 parent b7c4840 commit 0c84b97

File tree

11 files changed

+332
-201
lines changed

11 files changed

+332
-201
lines changed

storage/framework/core/components/calendar/src/components/Others.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const allTypes = [
3838
},
3939
{
4040
name: 'Rich Colors Warning',
41-
snippet: `notification.Warning('Event has been created')
41+
snippet: `notification.warning('Event has been created')
4242
4343
// ...
4444

storage/framework/core/components/dropdown/src/components/Others.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const allTypes = [
3838
},
3939
{
4040
name: 'Rich Colors Warning',
41-
snippet: `notification.Warning('Event has been created')
41+
snippet: `notification.warning('Event has been created')
4242
4343
// ...
4444

storage/framework/core/components/modal/src/components/Others.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const allTypes = [
3838
},
3939
{
4040
name: 'Rich Colors Warning',
41-
snippet: `notification.Warning('Event has been created')
41+
snippet: `notification.warning('Event has been created')
4242
4343
// ...
4444

storage/framework/core/components/notification/src/components/Others.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const allTypes = [
3838
},
3939
{
4040
name: 'Rich Colors Warning',
41-
snippet: `notification.Warning('Event has been created')
41+
snippet: `notification.warning('Event has been created')
4242
4343
// ...
4444

storage/framework/core/components/notification/src/components/Toast.vue

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import '../styles/styles.css'
33
import { computed, onMounted, onUnmounted, ref, watchEffect } from 'vue'
44
import { useIsDocumentHidden } from '../composables/useIsDocumentHidden'
5-
import type { HeightT, ToastProps, ToastT } from '../types'
6-
import CloseIcon from './icons/CloseIcon.vue'
5+
import { type HeightT, type ToastProps, type ToastT, isAction } from './types'
76
87
const props = defineProps<ToastProps>()
98
@@ -14,12 +13,9 @@ const emit = defineEmits<{
1413
1514
// Default lifetime of a toasts (in ms)
1615
const TOAST_LIFETIME = 4000
17-
1816
// Default gap between toasts
1917
const GAP = 14
20-
2118
const SWIPE_THRESHOLD = 20
22-
2319
const TIME_BEFORE_UNMOUNT = 200
2420
2521
const mounted = ref(false)
@@ -34,15 +30,8 @@ const isFront = computed(() => props.index === 0)
3430
const isVisible = computed(() => props.index + 1 <= props.visibleToasts)
3531
const toastType = computed(() => props.toast.type)
3632
const dismissible = computed(() => props.toast.dismissible !== false)
37-
const toastClass = computed(() => {
38-
return props.cn(
39-
props.classes?.toast,
40-
props.toast?.classes?.toast,
41-
props.classes?.default,
42-
props.classes?.[props.toast.type || 'default'],
43-
props.toast?.classes?.[props.toast.type || 'default'],
44-
)
45-
})
33+
const toastClass = computed(() => props.toast.class || '')
34+
const toastDescriptionClass = computed(() => props.descriptionClass || '')
4635
4736
const toastStyle = props.toast.style || {}
4837
@@ -53,7 +42,6 @@ const duration = computed(() => props.toast.duration || props.duration || TOAST_
5342
5443
const closeTimerStartTimeRef = ref(0)
5544
const offset = ref(0)
56-
const remainingTime = ref(duration.value)
5745
const lastCloseTimerStartTimeRef = ref(0)
5846
const pointerStartRef = ref<{ x: number; y: number } | null>(null)
5947
const coords = computed(() => props.position.split('-'))
@@ -113,6 +101,9 @@ function deleteToast() {
113101
removed.value = true
114102
offsetBeforeRemove.value = offset.value
115103
104+
const height = props.heights.filter((height) => height.toastId !== props.toast.id)
105+
emit('update:heights', height)
106+
116107
setTimeout(() => {
117108
emit('removeToast', props.toast)
118109
}, TIME_BEFORE_UNMOUNT)
@@ -136,7 +127,7 @@ function onPointerDown(event: PointerEvent) {
136127
pointerStartRef.value = { x: event.clientX, y: event.clientY }
137128
}
138129
139-
function onPointerUp(event: PointerEvent) {
130+
function onPointerUp() {
140131
if (swipeOut.value) return
141132
pointerStartRef.value = null
142133
@@ -159,7 +150,7 @@ function onPointerUp(event: PointerEvent) {
159150
}
160151
161152
function onPointerMove(event: PointerEvent) {
162-
if (!pointerStartRef.value) return
153+
if (!pointerStartRef.value || !dismissible.value) return
163154
164155
const yPosition = event.clientY - pointerStartRef.value.y
165156
const xPosition = event.clientX - pointerStartRef.value.x
@@ -179,37 +170,41 @@ function onPointerMove(event: PointerEvent) {
179170
}
180171
181172
watchEffect(() => {
182-
offset.value = heightIndex.value * GAP + toastsHeightBefore.value
173+
offset.value = heightIndex.value * props?.gap + toastsHeightBefore.value
183174
})
184175
185176
watchEffect((onInvalidate) => {
186177
if (
187178
(props.toast.promise && toastType.value === 'loading') ||
188179
props.toast.duration === Number.POSITIVE_INFINITY ||
189180
props.toast.type === 'loading'
190-
)
181+
) {
191182
return
183+
}
184+
192185
let timeoutId: ReturnType<typeof setTimeout>
186+
let remainingTime = duration.value
193187
194188
// Pause the timer on each hover
195189
const pauseTimer = () => {
196190
if (lastCloseTimerStartTimeRef.value < closeTimerStartTimeRef.value) {
197191
// Get the elapsed time since the timer started
198192
const elapsedTime = new Date().getTime() - closeTimerStartTimeRef.value
199193
200-
remainingTime.value = remainingTime.value - elapsedTime
194+
remainingTime = remainingTime - elapsedTime
201195
}
202196
203197
lastCloseTimerStartTimeRef.value = new Date().getTime()
204198
}
205199
206200
const startTimer = () => {
201+
if (remainingTime === Number.POSITIVE_INFINITY) return
207202
closeTimerStartTimeRef.value = new Date().getTime()
208203
// Let the toast know it has started
209204
timeoutId = setTimeout(() => {
210205
props.toast.onAutoClose?.(props.toast)
211206
deleteToast()
212-
}, remainingTime.value)
207+
}, remainingTime)
213208
}
214209
215210
if (props.expanded || props.interacting || (props.pauseWhenPageIsHidden && isDocumentHidden)) pauseTimer()
@@ -220,9 +215,9 @@ watchEffect((onInvalidate) => {
220215
})
221216
})
222217
223-
watchEffect(() => {
224-
if (props.toast.delete) deleteToast()
225-
})
218+
// watchEffect(() => {
219+
// if (props.toast.delete) deleteToast()
220+
// })
226221
227222
onMounted(() => {
228223
if (toastRef.value) {
@@ -253,8 +248,9 @@ onUnmounted(() => {
253248
aria-atomic="true"
254249
role="status"
255250
tabindex="0"
256-
data-sonner-toast=""
251+
data-sonner-toast="true"
257252
:class="toastClass"
253+
:data-rich-colors="toast.richColors ?? defaultRichColors"
258254
:data-styled="!Boolean(toast.component || toast?.unstyled || unstyled)"
259255
:data-mounted="mounted"
260256
:data-promise="Boolean(toast.promise)"
@@ -287,11 +283,16 @@ onUnmounted(() => {
287283
<button
288284
:aria-label="closeButtonAriaLabel || 'Close toast'"
289285
:data-disabled="disabled"
290-
data-close-button
286+
data-close-button="true"
291287
:class="cn(classes?.closeButton, toast?.classes?.closeButton)"
292288
@click="handleCloseToast"
293289
>
294-
<CloseIcon />
290+
<template v-if="icons?.close">
291+
<component :is="icons?.close" />
292+
</template>
293+
<template v-else>
294+
<slot name="close-icon" />
295+
</template>
295296
</button>
296297
</template>
297298

@@ -339,7 +340,7 @@ onUnmounted(() => {
339340
:class="
340341
cn(
341342
descriptionClass,
342-
toast.descriptionClass,
343+
toastDescriptionClass,
343344
classes?.description,
344345
toast.classes?.description,
345346
)
@@ -359,34 +360,38 @@ onUnmounted(() => {
359360
</div>
360361
<template v-if="toast.cancel">
361362
<button
363+
:style="toast.cancelButtonStyle || cancelButtonStyle"
362364
:class="cn(classes?.cancelButton, toast.classes?.cancelButton)"
363365
data-button
364366
data-cancel
365367
@click="
366-
() => {
367-
deleteToast()
368-
if (toast.cancel?.onClick) {
369-
toast.cancel.onClick()
370-
}
368+
(event) => {
369+
if (!isAction(toast.cancel!)) return;
370+
if (!dismissible) return;
371+
toast.cancel.onClick?.(event);
372+
deleteToast();
371373
}
372374
"
373375
>
374-
{{ toast.cancel.label }}
376+
{{ isAction(toast.cancel) ? toast.cancel?.label : toast.cancel }}
375377
</button>
376378
</template>
377379
<template v-if="toast.action">
378380
<button
381+
:style="toast.actionButtonStyle || actionButtonStyle"
379382
:class="cn(classes?.actionButton, toast.classes?.actionButton)"
380383
data-button
384+
data-action
381385
@click="
382386
(event) => {
383-
toast.action?.onClick(event)
384-
if (event.defaultPrevented) return
385-
deleteToast()
387+
if (!isAction(toast.action!)) return;
388+
if (event.defaultPrevented) return;
389+
toast.action.onClick?.(event);
390+
deleteToast();
386391
}
387392
"
388393
>
389-
{{ toast.action.label }}
394+
{{ isAction(toast.action) ? toast.action?.label : toast.action }}
390395
</button>
391396
</template>
392397
</template>

0 commit comments

Comments
 (0)