index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. require('../../utils/index.js');
  5. var index = require('../use-namespace/index.js');
  6. var error = require('../../utils/error.js');
  7. var core = require('@vueuse/core');
  8. var style = require('../../utils/dom/style.js');
  9. var scroll = require('../../utils/dom/scroll.js');
  10. const useLockscreen = (trigger, options = {}) => {
  11. if (!vue.isRef(trigger)) {
  12. error.throwError("[useLockscreen]", "You need to pass a ref param to this function");
  13. }
  14. const ns = options.ns || index.useNamespace("popup");
  15. const hiddenCls = vue.computed(() => ns.bm("parent", "hidden"));
  16. if (!core.isClient || style.hasClass(document.body, hiddenCls.value)) {
  17. return;
  18. }
  19. let scrollBarWidth = 0;
  20. let withoutHiddenClass = false;
  21. let bodyWidth = "0";
  22. const cleanup = () => {
  23. setTimeout(() => {
  24. style.removeClass(document == null ? void 0 : document.body, hiddenCls.value);
  25. if (withoutHiddenClass && document) {
  26. document.body.style.width = bodyWidth;
  27. }
  28. }, 200);
  29. };
  30. vue.watch(trigger, (val) => {
  31. if (!val) {
  32. cleanup();
  33. return;
  34. }
  35. withoutHiddenClass = !style.hasClass(document.body, hiddenCls.value);
  36. if (withoutHiddenClass) {
  37. bodyWidth = document.body.style.width;
  38. }
  39. scrollBarWidth = scroll.getScrollBarWidth(ns.namespace.value);
  40. const bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
  41. const bodyOverflowY = style.getStyle(document.body, "overflowY");
  42. if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === "scroll") && withoutHiddenClass) {
  43. document.body.style.width = `calc(100% - ${scrollBarWidth}px)`;
  44. }
  45. style.addClass(document.body, hiddenCls.value);
  46. });
  47. vue.onScopeDispose(() => cleanup());
  48. };
  49. exports.useLockscreen = useLockscreen;
  50. //# sourceMappingURL=index.js.map