UNPKG

tdesign-mobile-vue

Version:
134 lines (130 loc) 4.64 kB
/** * tdesign v1.7.0 * (c) 2024 TDesign Group * @license MIT */ import { onMounted, nextTick, onActivated, onDeactivated, onBeforeUnmount, watch } from 'vue'; import { useTouch } from '../_util/useTouch.mjs'; import getScrollParent from '../_util/getScrollParent.mjs'; import { supportsPassive } from '../_util/supportsPassive.mjs'; import '../shared/index.mjs'; import '../shared/functions.mjs'; import '../shared/util.mjs'; import '../_chunks/dep-cd74809c.mjs'; import '../_chunks/dep-8d37dbf8.mjs'; import '../_chunks/dep-52fdc8e1.mjs'; import '../_chunks/dep-8bf3054e.mjs'; import '../_chunks/dep-3d249f65.mjs'; import '../_chunks/dep-620d73f7.mjs'; import '../_chunks/dep-9b2de386.mjs'; import '../config.mjs'; import '../shared/component.mjs'; import '../shared/constants.mjs'; import '../shared/render.mjs'; import '../_chunks/dep-60cadef8.mjs'; import '../_chunks/dep-8140c29b.mjs'; import '../_chunks/dep-0d52e58f.mjs'; import '../_chunks/dep-019e292f.mjs'; import '../_chunks/dep-32364550.mjs'; import '../_chunks/dep-a836a38c.mjs'; import '../_chunks/dep-0ea7bbde.mjs'; import '../_chunks/dep-b437ef0b.mjs'; import '../shared/dom.mjs'; import '../_chunks/dep-91d696ea.mjs'; import '../_chunks/dep-e6c129ab.mjs'; import '../_chunks/dep-6303c50c.mjs'; import '../shared/render-tnode.mjs'; import '../shared/useToggle/index.mjs'; import '../shared/useCountDown/index.mjs'; import '../_chunks/dep-80e4de18.mjs'; import '@vueuse/core'; import '../shared/useCountDown/utils.mjs'; import '../shared/useDefault/index.mjs'; import '../shared/useChildSlots/index.mjs'; import '../_chunks/dep-f0f403be.mjs'; import '../shared/useVModel/index.mjs'; import '../_chunks/dep-6917b9bc.mjs'; import '../shared/useTouch/index.mjs'; import '../shared/useScrollParent/index.mjs'; import '../shared/useExpose/index.mjs'; import '../shared/useTest/index.mjs'; import '../_chunks/dep-219bb5a7.mjs'; import '../shared/useClickAway/index.mjs'; import '../shared/useGesture/index.mjs'; import '@use-gesture/vanilla'; import '../shared/hover.mjs'; var totalLockCount = /* @__PURE__ */new Map(); var mounted = null; function useLockScroll(rootRef, shouldLock, componentName) { var touch = useTouch(); var BODY_LOCK_CLASS = "".concat(componentName, "--lock"); var onTouchMove = function onTouchMove(event) { touch.move(event); var direction = touch.deltaY.value > 0 ? "10" : "01"; var el = getScrollParent(event.target, rootRef.value); if (!el) return; var scrollHeight = el.scrollHeight, offsetHeight = el.offsetHeight, scrollTop = el.scrollTop; var status = "11"; if (scrollTop === 0) { status = offsetHeight >= scrollHeight ? "00" : "01"; } else if (scrollTop + offsetHeight >= scrollHeight) { status = "10"; } if (status !== "11" && touch.isVertical() && !(parseInt(status, 2) & parseInt(direction, 2))) { if (event.cancelable) { event.preventDefault(); } } }; var lock = function lock() { var _totalLockCount$get; document.addEventListener("touchstart", touch.start); document.addEventListener("touchmove", onTouchMove, supportsPassive.value ? { passive: false } : false); if (!totalLockCount.get(BODY_LOCK_CLASS)) { document.body.classList.add(BODY_LOCK_CLASS); } totalLockCount.set(BODY_LOCK_CLASS, ((_totalLockCount$get = totalLockCount.get(BODY_LOCK_CLASS)) !== null && _totalLockCount$get !== void 0 ? _totalLockCount$get : 0) + 1); }; var unlock = function unlock() { var sum = Array.from(totalLockCount.values()).reduce(function (acc, val) { return acc + val; }, 0); if (sum) { var _totalLockCount$get2; document.removeEventListener("touchstart", touch.start); document.removeEventListener("touchmove", onTouchMove); totalLockCount.set(BODY_LOCK_CLASS, Math.max(((_totalLockCount$get2 = totalLockCount.get(BODY_LOCK_CLASS)) !== null && _totalLockCount$get2 !== void 0 ? _totalLockCount$get2 : 0) - 1, 0)); if (!totalLockCount.get(BODY_LOCK_CLASS)) { document.body.classList.remove(BODY_LOCK_CLASS); } } }; var init = function init() { return shouldLock() && lock(); }; var destroy = function destroy() { return shouldLock() && unlock(); }; onMounted(function () { init(); nextTick(function () { mounted = true; }); }); onActivated(function () { if (mounted) { init(); } }); onDeactivated(destroy); onBeforeUnmount(destroy); watch(shouldLock, function (value) { value ? lock() : unlock(); }); } export { useLockScroll }; //# sourceMappingURL=useLockScroll.mjs.map