index.mjs 1.8 KB

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