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 line
3.2 KiB

{"version":3,"file":"index.mjs","sources":["../../../../../packages/hooks/use-global-config/index.ts"],"sourcesContent":["import { inject, ref, computed, unref, provide, getCurrentInstance } from 'vue'\nimport { configProviderContextKey } from '@element-plus/tokens'\nimport { hasOwn, isObject, merge } from '@element-plus/utils/util'\nimport { debugWarn } from '@element-plus/utils/error'\nimport type { MaybeRef } from '@vueuse/core'\nimport type { Ref, App } from 'vue'\nimport type { ConfigProviderContext } from '@element-plus/tokens'\n\n// this is meant to fix global methods like `ElMessage(opts)`, this way we can inject current locale\n// into the component as default injection value.\n// refer to: https://github.com/element-plus/element-plus/issues/2610#issuecomment-887965266\nconst cache = ref<ConfigProviderContext>({})\n\nexport function useGlobalConfig<K extends keyof ConfigProviderContext>(\n key: K\n): Ref<ConfigProviderContext[K]>\nexport function useGlobalConfig(): Ref<ConfigProviderContext>\nexport function useGlobalConfig(key?: keyof ConfigProviderContext) {\n const config = inject(configProviderContextKey, cache)\n if (key) {\n return isObject(config.value) && hasOwn(config.value, key)\n ? computed(() => config.value[key])\n : ref(undefined)\n } else {\n return config\n }\n}\n\nexport const provideGlobalConfig = (\n config: MaybeRef<ConfigProviderContext>,\n app?: App\n) => {\n const inSetup = !!getCurrentInstance()\n const oldConfig = inSetup ? useGlobalConfig() : undefined\n\n const provideFn = app?.provide ?? (inSetup ? provide : undefined)\n if (!provideFn) {\n debugWarn(\n 'provideGlobalConfig',\n 'provideGlobalConfig() can only be used inside setup().'\n )\n return\n }\n\n const context = computed(() => {\n const cfg = unref(config)\n if (!oldConfig) return cfg\n return merge(oldConfig.value, cfg)\n })\n provideFn(configProviderContextKey, context)\n cache.value = context.value\n return context\n}\n"],"names":[],"mappings":";;;;;;;AAIA,MAAM,KAAK,GAAG,GAAG,CAAC,EAAE,CAAC,CAAC;AACf,SAAS,eAAe,CAAC,GAAG,EAAE;AACrC,EAAE,MAAM,MAAM,GAAG,MA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