index.js 1.8 KB

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