index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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(
  11. "[useLockscreen]",
  12. "You need to pass a ref param to this function"
  13. );
  14. }
  15. const ns = options.ns || index.useNamespace("popup");
  16. const hiddenCls = vue.computed(() => ns.bm("parent", "hidden"));
  17. let scrollBarWidth = 0;
  18. let withoutHiddenClass = false;
  19. let bodyWidth = "0";
  20. let cleaned = false;
  21. const cleanup = () => {
  22. if (cleaned)
  23. return;
  24. cleaned = true;
  25. setTimeout(() => {
  26. if (typeof document === "undefined")
  27. return;
  28. if (withoutHiddenClass && document) {
  29. document.body.style.width = bodyWidth;
  30. style.removeClass(document.body, hiddenCls.value);
  31. }
  32. }, 200);
  33. };
  34. vue.watch(trigger, (val) => {
  35. if (!val) {
  36. cleanup();
  37. return;
  38. }
  39. cleaned = false;
  40. withoutHiddenClass = !style.hasClass(document.body, hiddenCls.value);
  41. if (withoutHiddenClass) {
  42. bodyWidth = document.body.style.width;
  43. style.addClass(document.body, hiddenCls.value);
  44. }
  45. scrollBarWidth = scroll.getScrollBarWidth(ns.namespace.value);
  46. const bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
  47. const bodyOverflowY = style.getStyle(document.body, "overflowY");
  48. if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === "scroll") && withoutHiddenClass) {
  49. document.body.style.width = `calc(100% - ${scrollBarWidth}px)`;
  50. }
  51. });
  52. vue.onScopeDispose(() => cleanup());
  53. };
  54. exports.useLockscreen = useLockscreen;
  55. //# sourceMappingURL=index.js.map