index.mjs 1.7 KB

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