ā¬ ļø Back to Table of Contents
š useSwipe¶
š Analysis Summary¶
| Metric | Count |
|---|---|
| š§ Functions | 6 |
| š¦ Imports | 9 |
| š¢ Vue Composition API | 6 |
| š Interfaces | 2 |
| š Type Aliases | 1 |
š Table of Contents¶
š ļø File Location:¶
š packages/core/useSwipe/index.ts
š¦ Imports¶
| Name | Source |
|---|---|
ComputedRef |
vue |
MaybeRefOrGetter |
vue |
ShallowRef |
vue |
ConfigurableWindow |
../_configurable |
Position |
../types |
computed |
vue |
reactive |
vue |
shallowRef |
vue |
useEventListener |
../useEventListener |
Vue Composition API¶
| Name | Type | Reactive Variables | Composables |
|---|---|---|---|
reactive |
reactive | none | none |
reactive |
reactive | none | none |
computed |
computed | none | none |
computed |
computed | none | none |
computed |
computed | none | none |
computed |
computed | none | none |
Functions¶
useSwipe(target: MaybeRefOrGetter<EventTarget | null | uā¦, options: UseSwipeOptions): UseSwipeReturn¶
Reactive swipe detection.
Parameters:
targetany: No descriptionoptionsany: No description
See: https://vueuse.org/useSwipe
Raw JSDoc
Calls:
reactive (from vue)computed (from vue)maxabsshallowRef (from vue)onSwipeEnduseEventListener (from ../useEventListener)getTouchEventCoordsupdateCoordsStartupdateCoordsEndonSwipeStartMath.abse.preventDefaultonSwipestops.forEachs
Code
export function useSwipe(
target: MaybeRefOrGetter<EventTarget | null | undefined>,
options: UseSwipeOptions = {},
): UseSwipeReturn {
const {
threshold = 50,
onSwipe,
onSwipeEnd,
onSwipeStart,
passive = true,
} = options
const coordsStart = reactive<Position>({ x: 0, y: 0 })
const coordsEnd = reactive<Position>({ x: 0, y: 0 })
const diffX = computed(() => coordsStart.x - coordsEnd.x)
const diffY = computed(() => coordsStart.y - coordsEnd.y)
const { max, abs } = Math
const isThresholdExceeded = computed(() => max(abs(diffX.value), abs(diffY.value)) >= threshold)
const isSwiping = shallowRef(false)
const direction = computed((): UseSwipeDirection => {
if (!isThresholdExceeded.value)
return 'none'
if (abs(diffX.value) > abs(diffY.value)) {
return diffX.value > 0
? 'left'
: 'right'
}
else {
return diffY.value > 0
? 'up'
: 'down'
}
})
const getTouchEventCoords = (e: TouchEvent) => [e.touches[0].clientX, e.touches[0].clientY]
const updateCoordsStart = (x: number, y: number) => {
coordsStart.x = x
coordsStart.y = y
}
const updateCoordsEnd = (x: number, y: number) => {
coordsEnd.x = x
coordsEnd.y = y
}
const listenerOptions = { passive, capture: !passive }
const onTouchEnd = (e: TouchEvent) => {
if (isSwiping.value)
onSwipeEnd?.(e, direction.value)
isSwiping.value = false
}
const stops = [
useEventListener(target, 'touchstart', (e: TouchEvent) => {
if (e.touches.length !== 1)
return
const [x, y] = getTouchEventCoords(e)
updateCoordsStart(x, y)
updateCoordsEnd(x, y)
onSwipeStart?.(e)
}, listenerOptions),
useEventListener(target, 'touchmove', (e: TouchEvent) => {
if (e.touches.length !== 1)
return
const [x, y] = getTouchEventCoords(e)
updateCoordsEnd(x, y)
if (listenerOptions.capture && !listenerOptions.passive && Math.abs(diffX.value) > Math.abs(diffY.value))
e.preventDefault()
if (!isSwiping.value && isThresholdExceeded.value)
isSwiping.value = true
if (isSwiping.value)
onSwipe?.(e)
}, listenerOptions),
useEventListener(target, ['touchend', 'touchcancel'], onTouchEnd, listenerOptions),
]
const stop = () => stops.forEach(s => s())
return {
isSwiping,
direction,
coordsStart,
coordsEnd,
lengthX: diffX,
lengthY: diffY,
stop,
}
}
Internal helpers¶
Declared inside another function in this file.
getTouchEventCoords(e: TouchEvent): number[]¶
Parameters:
eTouchEvent
Returns: number[]
updateCoordsStart(x: number, y: number): void¶
Parameters:
xnumberynumber
Returns: void
updateCoordsEnd(x: number, y: number): void¶
Parameters:
xnumberynumber
Returns: void
onTouchEnd(e: TouchEvent): void¶
Parameters:
eTouchEvent
Returns: void
Calls:
onSwipeEnd
Code
stop(): void¶
Returns: void
Calls:
stops.forEach
Interfaces¶
UseSwipeOptions¶
Interface Code
export interface UseSwipeOptions extends ConfigurableWindow {
/**
* Register events as passive
*
* @default true
*/
passive?: boolean
/**
* @default 50
*/
threshold?: number
/**
* Callback on swipe start
*/
onSwipeStart?: (e: TouchEvent) => void
/**
* Callback on swipe moves
*/
onSwipe?: (e: TouchEvent) => void
/**
* Callback on swipe ends
*/
onSwipeEnd?: (e: TouchEvent, direction: UseSwipeDirection) => void
}
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
passive |
boolean |
ā | not shown |
threshold |
number |
ā | not shown |
onSwipeStart |
(e: TouchEvent) => void |
ā | not shown |
onSwipe |
(e: TouchEvent) => void |
ā | not shown |
onSwipeEnd |
(e: TouchEvent, direction: UseSwipeDirection) => void |
ā | not shown |
UseSwipeReturn¶
Interface Code
Properties¶
| Name | Type | Optional | Description |
|---|---|---|---|
isSwiping |
ShallowRef<boolean> |
ā | not shown |
direction |
ComputedRef<UseSwipeDirection> |
ā | not shown |
coordsStart |
Readonly<Position> |
ā | not shown |
coordsEnd |
Readonly<Position> |
ā | not shown |
lengthX |
ComputedRef<number> |
ā | not shown |
lengthY |
ComputedRef<number> |
ā | not shown |
stop |
() => void |
ā | not shown |
Type Aliases¶
UseSwipeDirection¶
Generated by Syntax Scribe