UNPKG

hongluan-ui

Version:
56 lines (53 loc) 1.94 kB
import { isRef, watch, onScopeDispose } from 'vue'; import '../../utils/index.mjs'; import { throwError } from '../../utils/error.mjs'; import { isClient } from '@vueuse/core'; import { hasClass, removeClass, getStyle, addClass } from '../../utils/dom/style.mjs'; import { getScrollBarWidth } from '../../utils/dom/scroll.mjs'; const useLockscreen = (trigger, autoClean = true) => { if (!isRef(trigger)) { throwError("[useLockscreen]", "You need to pass a ref param to this function"); } if (!isClient || hasClass(document.body, "no-scroll")) { return; } let scrollBarWidth = 0; let withoutHiddenClass = false; let bodyWidth = "0"; const cleanup = () => { if (autoClean) { setTimeout(() => { removeClass(document == null ? void 0 : document.body, "no-scroll"); if (withoutHiddenClass && document) { document.body.style.width = bodyWidth; } }, 200); } else { if (hasClass(document == null ? void 0 : document.body, "no-scroll")) { removeClass(document == null ? void 0 : document.body, "no-scroll"); document.body.style.width = bodyWidth; } } }; watch(trigger, (val) => { if (!val && autoClean) { cleanup(); return; } withoutHiddenClass = !hasClass(document.body, "no-scroll"); if (withoutHiddenClass) { bodyWidth = document.body.style.width; } scrollBarWidth = getScrollBarWidth(); const bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight; const bodyOverflowY = getStyle(document.body, "overflowY"); if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === "scroll") && withoutHiddenClass) { document.body.style.width = `calc(100% - ${scrollBarWidth}px)`; } addClass(document.body, "no-scroll"); }); onScopeDispose(() => cleanup()); return cleanup; }; export { useLockscreen }; //# sourceMappingURL=index.mjs.map