mirror of
https://gitee.com/ant-design-vue/ant-design-vue.git
synced 2024-12-04 04:58:16 +08:00
b103b7570d
* fix: resolve dark mode not support * fix: unified expression
127 lines
3.6 KiB
TypeScript
127 lines
3.6 KiB
TypeScript
import type { CSSInterpolation, Theme } from '../_util/cssinjs';
|
|
import { createTheme, useCacheToken, useStyleRegister } from '../_util/cssinjs';
|
|
|
|
import version from '../version';
|
|
import type {
|
|
AliasToken,
|
|
GlobalToken,
|
|
MapToken,
|
|
OverrideToken,
|
|
PresetColorType,
|
|
PresetColorKey,
|
|
SeedToken,
|
|
} from './interface';
|
|
import { PresetColors } from './interface';
|
|
import defaultDerivative from './themes/default';
|
|
import defaultSeedToken from './themes/seed';
|
|
import formatToken from './util/alias';
|
|
import type { FullToken } from './util/genComponentStyleHook';
|
|
import genComponentStyleHook from './util/genComponentStyleHook';
|
|
import statisticToken, { merge as mergeToken, statistic } from './util/statistic';
|
|
import type { VueNode } from '../_util/type';
|
|
import { objectType } from '../_util/type';
|
|
import type { ComputedRef, InjectionKey, Ref } from 'vue';
|
|
import { defineComponent, provide, computed, inject, watchEffect, ref } from 'vue';
|
|
import { toReactive } from '../_util/toReactive';
|
|
|
|
const defaultTheme = createTheme(defaultDerivative);
|
|
|
|
export {
|
|
// colors
|
|
PresetColors,
|
|
// Statistic
|
|
statistic,
|
|
statisticToken,
|
|
mergeToken,
|
|
// hooks
|
|
useStyleRegister,
|
|
genComponentStyleHook,
|
|
};
|
|
export type {
|
|
SeedToken,
|
|
AliasToken,
|
|
PresetColorType,
|
|
PresetColorKey,
|
|
// FIXME: Remove this type
|
|
AliasToken as DerivativeToken,
|
|
FullToken,
|
|
};
|
|
|
|
// ================================ Context =================================
|
|
// To ensure snapshot stable. We disable hashed in test env.
|
|
export const defaultConfig = {
|
|
token: defaultSeedToken,
|
|
hashed: true,
|
|
};
|
|
|
|
export interface DesignTokenContext {
|
|
token: Partial<AliasToken>;
|
|
theme?: Theme<SeedToken, MapToken>;
|
|
components?: OverrideToken;
|
|
hashed?: string | boolean;
|
|
}
|
|
//defaultConfig
|
|
const DesignTokenContextKey: InjectionKey<DesignTokenContext> = Symbol('DesignTokenContext');
|
|
|
|
export const globalDesignTokenApi = ref<DesignTokenContext>();
|
|
|
|
export const useDesignTokenProvider = (value: DesignTokenContext) => {
|
|
provide(DesignTokenContextKey, value);
|
|
watchEffect(() => {
|
|
globalDesignTokenApi.value = value;
|
|
});
|
|
};
|
|
|
|
export const useDesignTokenInject = () => {
|
|
return inject(DesignTokenContextKey, globalDesignTokenApi.value || defaultConfig);
|
|
};
|
|
export const DesignTokenProvider = defineComponent({
|
|
props: {
|
|
value: objectType<DesignTokenContext>(),
|
|
},
|
|
setup(props, { slots }) {
|
|
useDesignTokenProvider(toReactive(computed(() => props.value)));
|
|
return () => {
|
|
return slots.default?.();
|
|
};
|
|
},
|
|
});
|
|
// ================================== Hook ==================================
|
|
export function useToken(): [
|
|
ComputedRef<Theme<SeedToken, MapToken>>,
|
|
ComputedRef<GlobalToken>,
|
|
ComputedRef<string>,
|
|
] {
|
|
const designTokenContext = inject<DesignTokenContext>(
|
|
DesignTokenContextKey,
|
|
globalDesignTokenApi.value || defaultConfig,
|
|
);
|
|
|
|
const salt = computed(() => `${version}-${designTokenContext.hashed || ''}`);
|
|
|
|
const mergedTheme = computed(() => designTokenContext.theme || defaultTheme);
|
|
|
|
const cacheToken = useCacheToken<GlobalToken, SeedToken>(
|
|
mergedTheme,
|
|
computed(() => [defaultSeedToken, designTokenContext.token]),
|
|
computed(() => ({
|
|
salt: salt.value,
|
|
override: { override: designTokenContext.token, ...designTokenContext.components },
|
|
formatToken,
|
|
})),
|
|
);
|
|
|
|
return [
|
|
mergedTheme,
|
|
computed(() => cacheToken.value[0]),
|
|
computed(() => (designTokenContext.hashed ? cacheToken.value[1] : '')),
|
|
];
|
|
}
|
|
|
|
export type UseComponentStyleResult = [(node: VueNode) => VueNode, Ref<string>];
|
|
|
|
export type GenerateStyle<
|
|
ComponentToken extends object = AliasToken,
|
|
ReturnType = CSSInterpolation,
|
|
> = (token: ComponentToken) => ReturnType;
|