Skip to content

Commit

Permalink
fix(useScrollLock): fix iOS touchmove bug (#2362)
Browse files Browse the repository at this point in the history
Co-authored-by: Robin Scholz <r.scholz@kms-team.com>
Co-authored-by: webfansplz <308241863@qq.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
4 people committed Jan 3, 2023
1 parent ae78d8b commit c9a9a92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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

0 comments on commit c9a9a92

Please sign in to comment.