index.mjs 1.8 KB

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