hongluan-ui
Version:
Hongluan Component Library for Vue 3
60 lines (55 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var vue = require('vue');
require('../../utils/index.js');
var error = require('../../utils/error.js');
var core = require('@vueuse/core');
var style = require('../../utils/dom/style.js');
var scroll = require('../../utils/dom/scroll.js');
const useLockscreen = (trigger, autoClean = true) => {
if (!vue.isRef(trigger)) {
error.throwError("[useLockscreen]", "You need to pass a ref param to this function");
}
if (!core.isClient || style.hasClass(document.body, "no-scroll")) {
return;
}
let scrollBarWidth = 0;
let withoutHiddenClass = false;
let bodyWidth = "0";
const cleanup = () => {
if (autoClean) {
setTimeout(() => {
style.removeClass(document == null ? void 0 : document.body, "no-scroll");
if (withoutHiddenClass && document) {
document.body.style.width = bodyWidth;
}
}, 200);
} else {
if (style.hasClass(document == null ? void 0 : document.body, "no-scroll")) {
style.removeClass(document == null ? void 0 : document.body, "no-scroll");
document.body.style.width = bodyWidth;
}
}
};
vue.watch(trigger, (val) => {
if (!val && autoClean) {
cleanup();
return;
}
withoutHiddenClass = !style.hasClass(document.body, "no-scroll");
if (withoutHiddenClass) {
bodyWidth = document.body.style.width;
}
scrollBarWidth = scroll.getScrollBarWidth();
const bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
const bodyOverflowY = style.getStyle(document.body, "overflowY");
if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === "scroll") && withoutHiddenClass) {
document.body.style.width = `calc(100% - ${scrollBarWidth}px)`;
}
style.addClass(document.body, "no-scroll");
});
vue.onScopeDispose(() => cleanup());
return cleanup;
};
exports.useLockscreen = useLockscreen;
//# sourceMappingURL=index.js.map