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.
1 lines
6.3 KiB
1 lines
6.3 KiB
{"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 if (!val) {\n cleanup()\n return\n }\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 =\n document.documentElement.clientHeight < document.body.scrollHeight\n const bodyOverflowY = getStyle(document.body, 'overflowY')\n if (\n scrollBarWidth > 0 &&\n (bodyHasOverflow || bodyOverflowY === 'scroll') &&\n withoutHiddenClass\n ) {\n document.body.style.width = `calc(100% - ${scrollBarWidth}px)`\n }\n })\n onScopeDispose(() => cleanup())\n}\n"],"mappings":";;;;;;AAWY,MAACA,aAAa,GAAGA,CAACC,OAAO,EAAEC,OAAO,GAAG,EAAE,KAAK;EACtD,IAAI,CAACC,KAAK,CAACF,OAAO,CAAC,EAAE;IACnBG,UAAU,CAAC,iBAAiB,EAAE,+CAA+C,CAAC;EAClF;EACE,MAAMC,EAAE,GAAGH,OAAO,CAACG,EAAE,IAAIC,YAAY,CAAC,OAAO,CAAC;EAC9C,MAAMC,SAAS,GAAGC,QAAQ,CAAC,MAAMH,EAAE,CAACI,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;EAC3D,IAAI,CAACC,QAAQ,IAAIC,QAAQ,CAACC,QAAQ,CAACC,IAAI,EAAEN,SAAS,CAACO,KAAK,CAAC,EAAE;IACzD;EACJ;EACE,IAAIC,cAAc,GAAG,CAAC;EACtB,IAAIC,kBAAkB,GAAG,KAAK;EAC9B,IAAIC,SAAS,GAAG,GAAG;EACnB,MAAMC,OAAO,GAAGA,CAAA,KAAM;IACpBC,UAAU,CAAC,MAAM;MACf,IAAI,OAAOP,QAAQ,KAAK,WAAW,EACjC;MACF,IAAII,kBAAkB,IAAIJ,QAAQ,EAAE;QAClCA,QAAQ,CAACC,IAAI,CAACO,KAAK,CAACC,KAAK,GAAGJ,SAAS;QACrCK,WAAW,CAACV,QAAQ,CAACC,IAAI,EAAEN,SAAS,CAACO,KAAK,CAAC;MACnD;IACA,CAAK,EAAE,GAAG,CAAC;EACX,CAAG;EACDS,KAAK,CAACtB,OAAO,EAAGuB,GAAG,IAAK;IACtB,IAAI,CAACA,GAAG,EAAE;MACRN,OAAO,EAAE;MACT;IACN;IACIF,kBAAkB,GAAG,CAACL,QAAQ,CAACC,QAAQ,CAACC,IAAI,EAAEN,SAAS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