Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(usePointerSwipe): handle secondary clicks properly #2379

Merged
merged 1 commit into from Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/core/usePointerSwipe/index.ts
Expand Up @@ -13,22 +13,22 @@ export interface UsePointerSwipeOptions {
threshold?: number

/**
* Callback on swipe start
* Callback on swipe start.
*/
onSwipeStart?: (e: PointerEvent) => void

/**
* Callback on swipe move
* Callback on swipe move.
*/
onSwipe?: (e: PointerEvent) => void

/**
* Callback on swipe end
* Callback on swipe end.
*/
onSwipeEnd?: (e: PointerEvent, direction: SwipeDirection) => void

/**
* Pointer types that listen to.
* Pointer types to listen to.
*
* @default ['mouse', 'touch', 'pen']
*/
Expand Down Expand Up @@ -100,15 +100,15 @@ export function usePointerSwipe(
}
})

const filterEvent = (e: PointerEvent) => {
if (options.pointerTypes)
return options.pointerTypes.includes(e.pointerType as PointerType)
return true
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 (!filterEvent(e))
if (!eventIsAllowed(e))
return
isPointerDown.value = true
// Disable scroll on for TouchEvents
Expand All @@ -123,7 +123,7 @@ export function usePointerSwipe(
}),

useEventListener(target, 'pointermove', (e: PointerEvent) => {
if (!filterEvent(e))
if (!eventIsAllowed(e))
return
if (!isPointerDown.value)
return
Expand All @@ -137,7 +137,7 @@ export function usePointerSwipe(
}),

useEventListener(target, 'pointerup', (e: PointerEvent) => {
if (!filterEvent(e))
if (!eventIsAllowed(e))
return
if (isSwiping.value)
onSwipeEnd?.(e, direction.value)
Expand Down