{"version":3,"file":"use-global-config.js","sources":["../../../../../../../packages/components/config-provider/src/hooks/use-global-config.ts"],"sourcesContent":["import { computed, getCurrentInstance, inject, provide, ref, unref } from 'vue'\nimport { debugWarn, keysOf } from '@element-plus/utils'\nimport {\n SIZE_INJECTION_KEY,\n defaultInitialZIndex,\n defaultNamespace,\n emptyValuesContextKey,\n localeContextKey,\n namespaceContextKey,\n useLocale,\n useNamespace,\n useZIndex,\n zIndexContextKey,\n} from '@element-plus/hooks'\nimport { configProviderContextKey } from '../constants'\n\nimport type { MaybeRef } from '@vueuse/core'\nimport type { App, Ref } from 'vue'\nimport type { ConfigProviderContext } from '../constants'\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 globalConfig = ref()\n\nexport function useGlobalConfig<\n K extends keyof ConfigProviderContext,\n D extends ConfigProviderContext[K]\n>(\n key: K,\n defaultValue?: D\n): Ref | D>\nexport function useGlobalConfig(): Ref\nexport function useGlobalConfig(\n key?: keyof ConfigProviderContext,\n defaultValue = undefined\n) {\n const config = getCurrentInstance()\n ? inject(configProviderContextKey, globalConfig)\n : globalConfig\n if (key) {\n return computed(() => config.value?.[key] ?? defaultValue)\n } else {\n return config\n }\n}\n\n// for components like `ElMessage` `ElNotification` `ElMessageBox`.\nexport function useGlobalComponentSettings(\n block: string,\n sizeFallback?: MaybeRef\n) {\n const config = useGlobalConfig()\n\n const ns = useNamespace(\n block,\n computed(() => config.value?.namespace || defaultNamespace)\n )\n\n const locale = useLocale(computed(() => config.value?.locale))\n const zIndex = useZIndex(\n computed(() => config.value?.zIndex || defaultInitialZIndex)\n )\n const size = computed(() => unref(sizeFallback) || config.value?.size || '')\n provideGlobalConfig(computed(() => unref(config) || {}))\n\n return {\n ns,\n locale,\n zIndex,\n size,\n }\n}\n\nexport const provideGlobalConfig = (\n config: MaybeRef,\n app?: App,\n global = false\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?.value) return cfg\n return mergeConfig(oldConfig.value, cfg)\n })\n provideFn(configProviderContextKey, context)\n provideFn(\n localeContextKey,\n computed(() => context.value.locale)\n )\n provideFn(\n namespaceContextKey,\n computed(() => context.value.namespace)\n )\n provideFn(\n zIndexContextKey,\n computed(() => context.value.zIndex)\n )\n\n provideFn(SIZE_INJECTION_KEY, {\n size: computed(() => context.value.size || ''),\n })\n\n provideFn(\n emptyValuesContextKey,\n computed(() => ({\n emptyValues: context.value.emptyValues,\n valueOnClear: context.value.valueOnClear,\n }))\n )\n\n if (global || !globalConfig.value) {\n globalConfig.value = context.value\n }\n return context\n}\n\nconst mergeConfig = (\n a: ConfigProviderContext,\n b: ConfigProviderContext\n): ConfigProviderContext => {\n const keys = [...new Set([...keysOf(a), ...keysOf(b)])]\n const obj: Record = {}\n for (const key of keys) {\n obj[key] = b[key] !== undefined ? b[key] : a[key]\n }\n return obj\n}\n"],"names":["ref","getCurrentInstance","inject","configProviderContextKey","computed","useNamespace","defaultNamespace","useLocale","useZIndex","defaultInitialZIndex","unref","provide","debugWarn","localeContextKey","namespaceContextKey","zIndexContextKey","SIZE_INJECTION_KEY","emptyValuesContextKey","keysOf"],"mappings":";;;;;;;;;;;;;;;;AAeA,MAAM,YAAY,GAAGA,OAAG,EAAE,CAAC;AACpB,SAAS,eAAe,CAAC,GAAG,EAAE,YAAY,GAAG,KAAK,CAAC,EAAE;AAC5D,EAAE,MAAM,MAAM,GAAGC,sBAAkB,EAAE,GAAGC,UAAM,CAACC,kCAAwB,EAAE,YAAY,CAAC,GAAG,YAAY,CAAC;AACtG,EAAE,IAAI,GAAG,EAAE;AACX,IAAI,OAAOC,YAAQ,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