@vuesax-alpha/nightly
Version:
A Component Library for Vue 3
96 lines (93 loc) • 2.94 kB
JavaScript
import { ref, getCurrentInstance, inject, computed, provide, unref } from 'vue';
import '../../utils/index.mjs';
import '../../tokens/index.mjs';
import { useLocale, localeContextKey } from '../use-locale/index.mjs';
import { useNamespace, defaultNamespace, namespaceContextKey } from '../use-namespace/index.mjs';
import { useZIndex, defaultInitialZIndex, zIndexContextKey } from '../use-z-index/index.mjs';
import { configProviderContextKey } from '../../tokens/config-provider.mjs';
import { debugWarn } from '../../utils/error.mjs';
import { keysOf } from '../../utils/objects.mjs';
const globalConfig = ref();
function useGlobalConfig(key, defaultValue = void 0) {
const config = getCurrentInstance() ? inject(configProviderContextKey, globalConfig) : globalConfig;
if (key) {
return computed(() => {
var _a, _b;
return (_b = (_a = config.value) == null ? void 0 : _a[key]) != null ? _b : defaultValue;
});
} else {
return config;
}
}
const useGlobalComponentSettings = (block) => {
const config = useGlobalConfig();
const ns = useNamespace(
block,
computed(() => {
var _a;
return ((_a = config.value) == null ? void 0 : _a.namespace) || defaultNamespace;
})
);
const locale = useLocale(computed(() => {
var _a;
return (_a = config.value) == null ? void 0 : _a.locale;
}));
const zIndex = useZIndex(
computed(() => {
var _a;
return ((_a = config.value) == null ? void 0 : _a.zIndex) || defaultInitialZIndex;
})
);
return {
ns,
locale,
zIndex
};
};
const provideGlobalConfig = (config, app, global = false) => {
var _a;
const inSetup = !!getCurrentInstance();
const oldConfig = inSetup ? useGlobalConfig() : void 0;
const provideFn = (_a = app == null ? void 0 : app.provide) != null ? _a : inSetup ? provide : void 0;
if (!provideFn) {
debugWarn(
"provideGlobalConfig",
"provideGlobalConfig() can only be used inside setup()."
);
return;
}
const context = computed(() => {
const cfg = unref(config);
if (!(oldConfig == null ? void 0 : oldConfig.value))
return cfg;
return mergeConfig(oldConfig.value, cfg);
});
provideFn(configProviderContextKey, context);
provideFn(
localeContextKey,
computed(() => context.value.locale)
);
provideFn(
namespaceContextKey,
computed(() => context.value.namespace)
);
provideFn(
zIndexContextKey,
computed(() => context.value.zIndex)
);
if (global || !globalConfig.value) {
globalConfig.value = context.value;
}
return context;
};
const mergeConfig = (a, b) => {
var _a;
const keys = [.../* @__PURE__ */ new Set([...keysOf(a), ...keysOf(b)])];
const obj = {};
for (const key of keys) {
obj[key] = (_a = b[key]) != null ? _a : a[key];
}
return obj;
};
export { provideGlobalConfig, useGlobalComponentSettings, useGlobalConfig };
//# sourceMappingURL=index.mjs.map