Skip to content

Commit 176cfec

Browse files
authored
feat(Toast): support disabling swipe-to-close with the disableSwipe prop (#2189)
1 parent 47876c5 commit 176cfec

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

docs/content/meta/ToastProvider.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<!-- This file was automatic generated. Do not edit it manually -->
22

33
<PropsTable :data="[
4+
{
5+
'name': 'disableSwipe',
6+
'description': '<p>Whether to disable the ability to swipe to close the toast.</p>\n',
7+
'type': 'boolean',
8+
'required': false
9+
},
410
{
511
'name': 'duration',
612
'description': '<p>Time in milliseconds that each toast should remain visible for.</p>\n',

packages/core/src/Toast/ToastProvider.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createContext } from '@/shared'
77
type ToastProviderContext = {
88
label: Ref<string>
99
duration: Ref<number>
10+
disableSwipe: Ref<boolean>
1011
swipeDirection: Ref<SwipeDirection>
1112
swipeThreshold: Ref<number>
1213
toastCount: Ref<number>
@@ -30,6 +31,11 @@ export interface ToastProviderProps {
3031
* @defaultValue 5000
3132
*/
3233
duration?: number
34+
/**
35+
* Whether to disable the ability to swipe to close the toast.
36+
* @defaultValue false
37+
*/
38+
disableSwipe?: boolean
3339
/**
3440
* Direction of pointer swipe that should close the toast.
3541
* @defaultValue 'right'
@@ -59,7 +65,7 @@ const props = withDefaults(defineProps<ToastProviderProps>(), {
5965
swipeDirection: 'right',
6066
swipeThreshold: 50,
6167
})
62-
const { label, duration, swipeDirection, swipeThreshold } = toRefs(props)
68+
const { label, duration, disableSwipe, swipeDirection, swipeThreshold } = toRefs(props)
6369
useCollection({ isProvider: true })
6470
6571
const viewport = ref<HTMLElement>()
@@ -75,6 +81,7 @@ if (props.label && typeof props.label === 'string' && !props.label.trim()) {
7581
provideToastProviderContext({
7682
label,
7783
duration,
84+
disableSwipe,
7885
swipeDirection,
7986
swipeThreshold,
8087
toastCount,

packages/core/src/Toast/ToastRootImpl.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,17 @@ provideToastRootContext({ onClose: handleClose })
203203
:as-child="asChild"
204204
:data-state="open ? 'open' : 'closed'"
205205
:data-swipe-direction="providerContext.swipeDirection.value"
206-
:style="{ userSelect: 'none', touchAction: 'none' }"
206+
:style="providerContext.disableSwipe.value
207+
? undefined
208+
: { userSelect: 'none', touchAction: 'none' }"
207209
@pointerdown.left="(event: PointerEvent) => {
210+
if (providerContext.disableSwipe.value) return;
211+
208212
pointerStartRef = { x: event.clientX, y: event.clientY };
209213
}"
210214
@pointermove="(event: PointerEvent) => {
211-
if (!pointerStartRef) return;
215+
if (providerContext.disableSwipe.value || !pointerStartRef) return;
216+
212217
const x = event.clientX - pointerStartRef.x;
213218
const y = event.clientY - pointerStartRef.y;
214219
const hasSwipeMoveStarted = Boolean(swipeDeltaRef);
@@ -237,6 +242,8 @@ provideToastRootContext({ onClose: handleClose })
237242
}
238243
}"
239244
@pointerup="(event: PointerEvent) => {
245+
if (providerContext.disableSwipe.value) return;
246+
240247
const delta = swipeDeltaRef;
241248
const target = event.target as HTMLElement;
242249
if (target.hasPointerCapture(event.pointerId)) {

0 commit comments

Comments
 (0)