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

feat(usePointerSwipe): add disableTextSelect option #3604

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/core/usePointerSwipe/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function reset() {
}

const { distanceX, isSwiping } = usePointerSwipe(target, {
disableTextSelect: true,
onSwipe(e: PointerEvent) {
if (containerWidth.value) {
if (distanceX.value < 0) {
Expand Down
26 changes: 22 additions & 4 deletions packages/core/usePointerSwipe/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MaybeRefOrGetter } from '@vueuse/shared'
import { toRef } from '@vueuse/shared'
import { toRef, tryOnMounted } from '@vueuse/shared'
import type { Ref } from 'vue-demi'
import { computed, reactive, readonly, ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
Expand Down Expand Up @@ -33,6 +33,13 @@ export interface UsePointerSwipeOptions {
* @default ['mouse', 'touch', 'pen']
*/
pointerTypes?: PointerType[]

/**
* Disable text selection on swipe.
*
* @default false
*/
disableTextSelect?: boolean
}

export interface UsePointerSwipeReturn {
Expand All @@ -57,11 +64,13 @@ export function usePointerSwipe(
options: UsePointerSwipeOptions = {},
): UsePointerSwipeReturn {
const targetRef = toRef(target)

const {
threshold = 50,
onSwipe,
onSwipeEnd,
onSwipeStart,
disableTextSelect = false,
} = options

const posStart = reactive<Position>({ x: 0, y: 0 })
Expand Down Expand Up @@ -111,8 +120,6 @@ export function usePointerSwipe(
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)
Expand Down Expand Up @@ -144,10 +151,21 @@ export function usePointerSwipe(

isPointerDown.value = false
isSwiping.value = false
targetRef.value?.style?.setProperty('touch-action', 'initial')
}),
]

tryOnMounted(() => {
// Disable scroll on for TouchEvents
targetRef.value?.style?.setProperty('touch-action', 'none')

if (disableTextSelect) {
// Disable text selection on swipe
targetRef.value?.style?.setProperty('-webkit-user-select', 'none')
targetRef.value?.style?.setProperty('-ms-user-select', 'none')
targetRef.value?.style?.setProperty('user-select', 'none')
}
})

const stop = () => stops.forEach(s => s())

return {
Expand Down