index.mjs 1.8 KB

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