You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.8 KiB

1 month ago
  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, addClass, getStyle, 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. if (withoutHiddenClass && document) {
  24. document.body.style.width = bodyWidth;
  25. removeClass(document.body, hiddenCls.value);
  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. addClass(document.body, hiddenCls.value);
  38. }
  39. scrollBarWidth = getScrollBarWidth(ns.namespace.value);
  40. const bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;
  41. const bodyOverflowY = getStyle(document.body, "overflowY");
  42. if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === "scroll") && withoutHiddenClass) {
  43. document.body.style.width = `calc(100% - ${scrollBarWidth}px)`;
  44. }
  45. });
  46. onScopeDispose(() => cleanup());
  47. };
  48. export { useLockscreen };
  49. //# sourceMappingURL=index.mjs.map