index.mjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { isRef, watch, onScopeDispose } from 'vue';
  2. import { computed } from '@vue/reactivity';
  3. import { isClient } from '@vueuse/core';
  4. import '../../utils/index.mjs';
  5. import { useNamespace } from '../use-namespace/index.mjs';
  6. import { throwError } from '../../utils/error.mjs';
  7. import { hasClass, removeClass, getStyle, addClass } from '../../utils/dom/style.mjs';
  8. import { getScrollBarWidth } from '../../utils/dom/scroll.mjs';
  9. const useLockscreen = (trigger) => {
  10. if (!isRef(trigger)) {
  11. throwError("[useLockscreen]", "You need to pass a ref param to this function");
  12. }
  13. const ns = useNamespace("popup");
  14. const hiddenCls = computed(() => ns.bm("parent", "hidden"));
  15. if (!isClient || hasClass(document.body, hiddenCls.value)) {
  16. return;
  17. }
  18. let scrollBarWidth = 0;
  19. let withoutHiddenClass = false;
  20. let bodyWidth = "0";
  21. const cleanup = () => {
  22. setTimeout(() => {
  23. removeClass(document.body, hiddenCls.value);
  24. if (withoutHiddenClass) {
  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