fix: theme reactive not work #7180

feat-4.1
tangjinzhou 12 months ago
parent 509ec682f2
commit a9198e44df

@ -21,8 +21,16 @@ import statisticToken, { merge as mergeToken, statistic } from './util/statistic
import type { VueNode } from '../_util/type'; import type { VueNode } from '../_util/type';
import { objectType } from '../_util/type'; import { objectType } from '../_util/type';
import type { ComputedRef, InjectionKey, Ref } from 'vue'; import type { ComputedRef, InjectionKey, Ref } from 'vue';
import { unref, defineComponent, provide, computed, inject, watchEffect, ref } from 'vue'; import {
import { toReactive } from '../_util/toReactive'; triggerRef,
unref,
defineComponent,
provide,
computed,
inject,
watch,
shallowRef,
} from 'vue';
const defaultTheme = createTheme(defaultDerivative); const defaultTheme = createTheme(defaultDerivative);
@ -64,13 +72,18 @@ export interface DesignTokenContext {
const DesignTokenContextKey: InjectionKey<ComputedRef<DesignTokenContext>> = const DesignTokenContextKey: InjectionKey<ComputedRef<DesignTokenContext>> =
Symbol('DesignTokenContext'); Symbol('DesignTokenContext');
export const globalDesignTokenApi = ref<DesignTokenContext>(); export const globalDesignTokenApi = shallowRef<DesignTokenContext>();
export const useDesignTokenProvider = (value: ComputedRef<DesignTokenContext>) => { export const useDesignTokenProvider = (value: ComputedRef<DesignTokenContext>) => {
provide(DesignTokenContextKey, value); provide(DesignTokenContextKey, value);
watchEffect(() => { watch(
globalDesignTokenApi.value = toReactive(value); value,
}); () => {
globalDesignTokenApi.value = unref(value);
triggerRef(globalDesignTokenApi);
},
{ immediate: true, deep: true },
);
}; };
export const useDesignTokenInject = () => { export const useDesignTokenInject = () => {
@ -96,23 +109,24 @@ export function useToken(): [
ComputedRef<GlobalToken>, ComputedRef<GlobalToken>,
ComputedRef<string>, ComputedRef<string>,
] { ] {
const designTokenContext = unref( const designTokenContext = inject<ComputedRef<DesignTokenContext>>(
inject<ComputedRef<DesignTokenContext>>(
DesignTokenContextKey, DesignTokenContextKey,
computed(() => globalDesignTokenApi.value || defaultConfig), computed(() => globalDesignTokenApi.value || defaultConfig),
),
); );
const salt = computed(() => `${version}-${designTokenContext.hashed || ''}`); const salt = computed(() => `${version}-${designTokenContext.value.hashed || ''}`);
const mergedTheme = computed(() => designTokenContext.theme || defaultTheme); const mergedTheme = computed(() => designTokenContext.value.theme || defaultTheme);
const cacheToken = useCacheToken<GlobalToken, SeedToken>( const cacheToken = useCacheToken<GlobalToken, SeedToken>(
mergedTheme, mergedTheme,
computed(() => [defaultSeedToken, designTokenContext.token]), computed(() => [defaultSeedToken, designTokenContext.value.token]),
computed(() => ({ computed(() => ({
salt: salt.value, salt: salt.value,
override: { override: designTokenContext.token, ...designTokenContext.components }, override: {
override: designTokenContext.value.token,
...designTokenContext.value.components,
},
formatToken, formatToken,
})), })),
); );
@ -120,7 +134,7 @@ export function useToken(): [
return [ return [
mergedTheme, mergedTheme,
computed(() => cacheToken.value[0]), computed(() => cacheToken.value[0]),
computed(() => (designTokenContext.hashed ? cacheToken.value[1] : '')), computed(() => (designTokenContext.value.hashed ? cacheToken.value[1] : '')),
]; ];
} }

Loading…
Cancel
Save