fix(Popup): should watch lockScroll (#8169)

* fix(Popup): should watch lockScroll

* chore: oreder

* chore: upd

* chore: upd
This commit is contained in:
neverland
2021-02-16 16:22:34 +08:00
committed by GitHub
parent f7b09d1677
commit 86b276caa5
2 changed files with 38 additions and 30 deletions
+36 -16
View File
@@ -1,5 +1,9 @@
import { Ref } from 'vue';
import { getScrollParent, supportsPassive } from '@vant/use';
import { Ref, watch, onBeforeUnmount, onDeactivated } from 'vue';
import {
getScrollParent,
supportsPassive,
onMountedOrActivated,
} from '@vant/use';
import { useTouch } from './use-touch';
import { preventDefault } from '../utils';
@@ -40,24 +44,22 @@ export function useLockScroll(
};
const lock = () => {
if (shouldLock()) {
document.addEventListener('touchstart', touch.start);
document.addEventListener(
'touchmove',
onTouchMove,
supportsPassive ? { passive: false } : false
);
document.addEventListener('touchstart', touch.start);
document.addEventListener(
'touchmove',
onTouchMove,
supportsPassive ? { passive: false } : false
);
if (!totalLockCount) {
document.body.classList.add(BODY_LOCK_CLASS);
}
totalLockCount++;
if (!totalLockCount) {
document.body.classList.add(BODY_LOCK_CLASS);
}
totalLockCount++;
};
const unlock = () => {
if (shouldLock() && totalLockCount) {
if (totalLockCount) {
document.removeEventListener('touchstart', touch.start);
document.removeEventListener('touchmove', onTouchMove);
@@ -69,5 +71,23 @@ export function useLockScroll(
}
};
return [lock, unlock];
const init = () => {
if (shouldLock()) {
lock();
}
};
const destroy = () => {
if (shouldLock()) {
unlock();
}
};
onMountedOrActivated(init);
onDeactivated(destroy);
onBeforeUnmount(destroy);
watch(shouldLock, (value) => {
value ? lock() : unlock();
});
}