-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
162 lines (141 loc) · 4.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveRef } from '@vueuse/shared'
import type { Ref } from 'vue-demi'
import { computed, reactive, readonly, ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { SwipeDirection } from '../useSwipe/index'
import type { PointerType, Position } from '../types'
export interface UsePointerSwipeOptions {
/**
* @default 50
*/
threshold?: number
/**
* Callback on swipe start.
*/
onSwipeStart?: (e: PointerEvent) => void
/**
* Callback on swipe move.
*/
onSwipe?: (e: PointerEvent) => void
/**
* Callback on swipe end.
*/
onSwipeEnd?: (e: PointerEvent, direction: SwipeDirection) => void
/**
* Pointer types to listen to.
*
* @default ['mouse', 'touch', 'pen']
*/
pointerTypes?: PointerType[]
}
export interface UsePointerSwipeReturn {
readonly isSwiping: Ref<boolean>
direction: Readonly<Ref<SwipeDirection | null>>
readonly posStart: Position
readonly posEnd: Position
distanceX: Readonly<Ref<number>>
distanceY: Readonly<Ref<number>>
stop: () => void
}
/**
* Reactive swipe detection based on PointerEvents.
*
* @see https://vueuse.org/usePointerSwipe
* @param target
* @param options
*/
export function usePointerSwipe(
target: MaybeComputedRef<HTMLElement | null | undefined>,
options: UsePointerSwipeOptions = {},
): UsePointerSwipeReturn {
const targetRef = resolveRef(target)
const {
threshold = 50,
onSwipe,
onSwipeEnd,
onSwipeStart,
} = options
const posStart = reactive<Position>({ x: 0, y: 0 })
const updatePosStart = (x: number, y: number) => {
posStart.x = x
posStart.y = y
}
const posEnd = reactive<Position>({ x: 0, y: 0 })
const updatePosEnd = (x: number, y: number) => {
posEnd.x = x
posEnd.y = y
}
const distanceX = computed(() => posStart.x - posEnd.x)
const distanceY = computed(() => posStart.y - posEnd.y)
const { max, abs } = Math
const isThresholdExceeded = computed(() => max(abs(distanceX.value), abs(distanceY.value)) >= threshold)
const isSwiping = ref(false)
const isPointerDown = ref(false)
const direction = computed(() => {
if (!isThresholdExceeded.value)
return SwipeDirection.NONE
if (abs(distanceX.value) > abs(distanceY.value)) {
return distanceX.value > 0
? SwipeDirection.LEFT
: SwipeDirection.RIGHT
}
else {
return distanceY.value > 0
? SwipeDirection.UP
: SwipeDirection.DOWN
}
})
const eventIsAllowed = (e: PointerEvent): boolean => {
const isReleasingButton = e.buttons === 0
const isPrimaryButton = e.buttons === 1
return options.pointerTypes?.includes(e.pointerType as PointerType) ?? (isReleasingButton || isPrimaryButton) ?? true
}
const stops = [
useEventListener(target, 'pointerdown', (e: PointerEvent) => {
if (!eventIsAllowed(e))
return
isPointerDown.value = true
// Disable scroll on for TouchEvents
targetRef.value?.style?.setProperty('touch-action', 'none')
// Future pointer events will be retargeted to target until pointerup/cancel
const eventTarget = e.target as HTMLElement | undefined
eventTarget?.setPointerCapture(e.pointerId)
const { clientX: x, clientY: y } = e
updatePosStart(x, y)
updatePosEnd(x, y)
onSwipeStart?.(e)
}),
useEventListener(target, 'pointermove', (e: PointerEvent) => {
if (!eventIsAllowed(e))
return
if (!isPointerDown.value)
return
const { clientX: x, clientY: y } = e
updatePosEnd(x, y)
if (!isSwiping.value && isThresholdExceeded.value)
isSwiping.value = true
if (isSwiping.value)
onSwipe?.(e)
}),
useEventListener(target, 'pointerup', (e: PointerEvent) => {
if (!eventIsAllowed(e))
return
if (isSwiping.value)
onSwipeEnd?.(e, direction.value)
isPointerDown.value = false
isSwiping.value = false
targetRef.value?.style?.setProperty('touch-action', 'initial')
}),
]
const stop = () => stops.forEach(s => s())
return {
isSwiping: readonly(isSwiping),
direction: readonly(direction),
posStart: readonly(posStart),
posEnd: readonly(posEnd),
distanceX,
distanceY,
stop,
}
}