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(useScrollLock): fix iOS touchmove bug #2362

Merged
merged 12 commits into from Jan 3, 2023
2 changes: 1 addition & 1 deletion packages/core/useScrollLock/demo.vue
Expand Up @@ -10,7 +10,7 @@ const toggleLock = useToggle(isLocked)
</script>

<template>
<div class="flex flex-wrap">
<div class="flex flex-wrap gap-4">
<div ref="el" class="w-300px h-300px m-auto overflow-scroll bg-gray-500/5 rounded">
<div class="w-500px h-400px relative">
<div position="absolute left-0 top-0" bg="gray-500/5" p="x-2 y-1">
Expand Down
24 changes: 23 additions & 1 deletion packages/core/useScrollLock/index.ts
Expand Up @@ -4,8 +4,30 @@ import { isIOS, resolveRef, resolveUnref, tryOnScopeDispose } from '@vueuse/shar

import { useEventListener } from '../useEventListener'

function checkOverflowScroll(ele: Element): boolean {
const style = window.getComputedStyle(ele)
if (style.overflowX === 'scroll' || style.overflowY === 'scroll') {
return true
}
else {
const parent = ele.parentNode as Element

if (!parent || parent.tagName === 'BODY')
return false

return checkOverflowScroll(parent)
}
}

function preventDefault(rawEvent: TouchEvent): boolean {
const e = rawEvent || window.event

const _target = e.target as Element

// Do not prevent if element or parentNodes have overflow: scroll set.
if (checkOverflowScroll(_target))
return false

// Do not prevent if the event has more than one touch (usually meaning this is a multi touch gesture like pinch to zoom).
if (e.touches.length > 1)
return true
Expand Down Expand Up @@ -49,7 +71,7 @@ export function useScrollLock(
stopTouchMoveListener = useEventListener(
ele,
'touchmove',
preventDefault,
(e) => { preventDefault(e as TouchEvent) },
{ passive: false },
)
}
Expand Down