use-global-config.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { keysOf } from "../../../../utils/objects.mjs";
  2. import { debugWarn } from "../../../../utils/error.mjs";
  3. import { localeContextKey, useLocale } from "../../../../hooks/use-locale/index.mjs";
  4. import { namespaceContextKey, useNamespace } from "../../../../hooks/use-namespace/index.mjs";
  5. import { defaultInitialZIndex, useZIndex, zIndexContextKey } from "../../../../hooks/use-z-index/index.mjs";
  6. import { SIZE_INJECTION_KEY } from "../../../../hooks/use-size/index.mjs";
  7. import { emptyValuesContextKey } from "../../../../hooks/use-empty-values/index.mjs";
  8. import { configProviderContextKey } from "../constants.mjs";
  9. import { isNil } from "lodash-unified";
  10. import { computed, getCurrentInstance, inject, provide, ref, unref } from "vue";
  11. //#region ../../packages/components/config-provider/src/hooks/use-global-config.ts
  12. const globalConfig = ref();
  13. function useGlobalConfig(key, defaultValue = void 0) {
  14. const config = getCurrentInstance() ? inject(configProviderContextKey, globalConfig) : globalConfig;
  15. if (key) return computed(() => config.value?.[key] ?? defaultValue);
  16. else return config;
  17. }
  18. function useGlobalComponentSettings(block, sizeFallback) {
  19. const config = useGlobalConfig();
  20. const ns = useNamespace(block, computed(() => config.value?.namespace || "el"));
  21. const locale = useLocale(computed(() => config.value?.locale));
  22. const zIndex = useZIndex(computed(() => {
  23. const zIndex = config.value?.zIndex;
  24. return isNil(zIndex) || Number.isNaN(zIndex) ? defaultInitialZIndex : zIndex;
  25. }));
  26. const size = computed(() => unref(sizeFallback) || config.value?.size || "");
  27. provideGlobalConfig(computed(() => unref(config) || {}));
  28. return {
  29. ns,
  30. locale,
  31. zIndex,
  32. size
  33. };
  34. }
  35. const provideGlobalConfig = (config, app, global = false) => {
  36. const inSetup = !!getCurrentInstance();
  37. const oldConfig = inSetup ? useGlobalConfig() : void 0;
  38. const provideFn = app?.provide ?? (inSetup ? provide : void 0);
  39. if (!provideFn) {
  40. debugWarn("provideGlobalConfig", "provideGlobalConfig() can only be used inside setup().");
  41. return;
  42. }
  43. const context = computed(() => {
  44. const cfg = unref(config);
  45. if (!oldConfig?.value) return cfg;
  46. return mergeConfig(oldConfig.value, cfg);
  47. });
  48. provideFn(configProviderContextKey, context);
  49. provideFn(localeContextKey, computed(() => context.value.locale));
  50. provideFn(namespaceContextKey, computed(() => context.value.namespace));
  51. provideFn(zIndexContextKey, computed(() => context.value.zIndex));
  52. provideFn(SIZE_INJECTION_KEY, { size: computed(() => context.value.size || "") });
  53. provideFn(emptyValuesContextKey, computed(() => ({
  54. emptyValues: context.value.emptyValues,
  55. valueOnClear: context.value.valueOnClear
  56. })));
  57. if (global || !globalConfig.value) globalConfig.value = context.value;
  58. return context;
  59. };
  60. const mergeConfig = (a, b) => {
  61. const keys = [...new Set([...keysOf(a), ...keysOf(b)])];
  62. const obj = {};
  63. for (const key of keys) obj[key] = b[key] !== void 0 ? b[key] : a[key];
  64. return obj;
  65. };
  66. //#endregion
  67. export { provideGlobalConfig, useGlobalComponentSettings, useGlobalConfig };
  68. //# sourceMappingURL=use-global-config.mjs.map