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.

0 lines
6.3 KiB

3 months ago
  1. {"ast":null,"code":"import { isRef, computed, watch, onScopeDispose } from 'vue';\nimport { useNamespace } from '../use-namespace/index.mjs';\nimport { throwError } from '../../utils/error.mjs';\nimport { isClient } from '@vueuse/core';\nimport { hasClass, addClass, getStyle, removeClass } from '../../utils/dom/style.mjs';\nimport { getScrollBarWidth } from '../../utils/dom/scroll.mjs';\nconst useLockscreen = (trigger, options = {}) => {\n if (!isRef(trigger)) {\n throwError(\"[useLockscreen]\", \"You need to pass a ref param to this function\");\n }\n const ns = options.ns || useNamespace(\"popup\");\n const hiddenCls = computed(() => ns.bm(\"parent\", \"hidden\"));\n if (!isClient || hasClass(document.body, hiddenCls.value)) {\n return;\n }\n let scrollBarWidth = 0;\n let withoutHiddenClass = false;\n let bodyWidth = \"0\";\n const cleanup = () => {\n setTimeout(() => {\n if (typeof document === \"undefined\") return;\n if (withoutHiddenClass && document) {\n document.body.style.width = bodyWidth;\n removeClass(document.body, hiddenCls.value);\n }\n }, 200);\n };\n watch(trigger, val => {\n if (!val) {\n cleanup();\n return;\n }\n withoutHiddenClass = !hasClass(document.body, hiddenCls.value);\n if (withoutHiddenClass) {\n bodyWidth = document.body.style.width;\n addClass(document.body, hiddenCls.value);\n }\n scrollBarWidth = getScrollBarWidth(ns.namespace.value);\n const bodyHasOverflow = document.documentElement.clientHeight < document.body.scrollHeight;\n const bodyOverflowY = getStyle(document.body, \"overflowY\");\n if (scrollBarWidth > 0 && (bodyHasOverflow || bodyOverflowY === \"scroll\") && withoutHiddenClass) {\n document.body.style.width = `calc(100% - ${scrollBarWidth}px)`;\n }\n });\n onScopeDispose(() => cleanup());\n};\nexport { useLockscreen };","map":{"version":3,"names":["useLockscreen","trigger","options","isRef","throwError","ns","useNamespace","hiddenCls","computed","bm","isClient","hasClass","document","body","value","scrollBarWidth","withoutHiddenClass","bodyWidth","cleanup","setTimeout","style","width","removeClass","watch","val","addClass","getScrollBarWidth","namespace","bodyHasOverflow","documentElement","clientHeight","scrollHeight","bodyOverflowY","getStyle","onScopeDispose"],"sources":["../../../../../packages/hooks/use-lockscreen/index.ts"],"sourcesContent":["import { computed, isRef, onScopeDispose, watch } from 'vue'\nimport {\n addClass,\n getScrollBarWidth,\n getStyle,\n hasClass,\n isClient,\n removeClass,\n throwError,\n} from '@element-plus/utils'\nimport { useNamespace } from '../use-namespace'\n\nimport type { Ref } from 'vue'\nimport type { UseNamespaceReturn } from '../use-namespace'\n\nexport type UseLockScreenOptions = {\n ns?: UseNamespaceReturn\n // shouldLock?: MaybeRef<boolean>\n}\n\n/**\n * Hook that monitoring the ref value to lock or unlock the screen.\n * When the trigger became true, it assumes modal is now opened and vice versa.\n * @param trigger {Ref<boolean>}\n */\nexport const useLockscreen = (\n trigger: Ref<boolean>,\n options: UseLockScreenOptions = {}\n) => {\n if (!isRef(trigger)) {\n throwError(\n '[useLockscreen]',\n 'You need to pass a ref param to this function'\n )\n }\n\n const ns = options.ns || useNamespace('popup')\n\n const hiddenCls = computed(() => ns.bm('parent', 'hidden'))\n\n if (!isClient || hasClass(document.body, hiddenCls.value)) {\n return\n }\n\n let scrollBarWidth = 0\n let withoutHiddenClass = false\n let bodyWidth = '0'\n\n const cleanup = () => {\n setTimeout(() => {\n // When the test case is running, the context environment simulated by jsdom may have been destroyed,\n // and the document does not exist at this time.\n if (typeof document === 'undefined') return\n if (withoutHiddenClass && document) {\n document.body.style.width = bodyWidth\n removeClass(document.body, hiddenCls.value)\n }\n }, 200)\n }\n watch(trigger, (val) => {\n