fix(StyleContext): fix style cache loaded repeat (#7029)
Co-authored-by: unknown <aibayanyu20>pull/7033/head
parent
7786a81e76
commit
61e5746b0d
|
@ -1,5 +1,13 @@
|
||||||
import type { ShallowRef, ExtractPropTypes, InjectionKey, Ref } from 'vue';
|
import type { ShallowRef, ExtractPropTypes, InjectionKey, Ref } from 'vue';
|
||||||
import { provide, defineComponent, unref, inject, watch, shallowRef } from 'vue';
|
import {
|
||||||
|
provide,
|
||||||
|
defineComponent,
|
||||||
|
unref,
|
||||||
|
inject,
|
||||||
|
watch,
|
||||||
|
shallowRef,
|
||||||
|
getCurrentInstance,
|
||||||
|
} from 'vue';
|
||||||
import CacheEntity from './Cache';
|
import CacheEntity from './Cache';
|
||||||
import type { Linter } from './linters/interface';
|
import type { Linter } from './linters/interface';
|
||||||
import type { Transformer } from './transformers/interface';
|
import type { Transformer } from './transformers/interface';
|
||||||
|
@ -80,6 +88,27 @@ const StyleContextKey: InjectionKey<ShallowRef<Partial<StyleContextProps>>> =
|
||||||
Symbol('StyleContextKey');
|
Symbol('StyleContextKey');
|
||||||
|
|
||||||
export type UseStyleProviderProps = Partial<StyleContextProps> | Ref<Partial<StyleContextProps>>;
|
export type UseStyleProviderProps = Partial<StyleContextProps> | Ref<Partial<StyleContextProps>>;
|
||||||
|
|
||||||
|
// fix: https://github.com/vueComponent/ant-design-vue/issues/7023
|
||||||
|
const getCache = () => {
|
||||||
|
const instance = getCurrentInstance();
|
||||||
|
let cache: CacheEntity;
|
||||||
|
if (instance && instance.appContext) {
|
||||||
|
const globalCache = instance.appContext?.config?.globalProperties?.__ANTDV_CSSINJS_CACHE__;
|
||||||
|
if (globalCache) {
|
||||||
|
cache = globalCache;
|
||||||
|
} else {
|
||||||
|
cache = createCache();
|
||||||
|
if (instance.appContext.config.globalProperties) {
|
||||||
|
instance.appContext.config.globalProperties.__ANTDV_CSSINJS_CACHE__ = cache;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cache = createCache();
|
||||||
|
}
|
||||||
|
return cache;
|
||||||
|
};
|
||||||
|
|
||||||
const defaultStyleContext: StyleContextProps = {
|
const defaultStyleContext: StyleContextProps = {
|
||||||
cache: createCache(),
|
cache: createCache(),
|
||||||
defaultCache: true,
|
defaultCache: true,
|
||||||
|
@ -87,7 +116,8 @@ const defaultStyleContext: StyleContextProps = {
|
||||||
};
|
};
|
||||||
// fix: https://github.com/vueComponent/ant-design-vue/issues/6912
|
// fix: https://github.com/vueComponent/ant-design-vue/issues/6912
|
||||||
export const useStyleInject = () => {
|
export const useStyleInject = () => {
|
||||||
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache: createCache() }));
|
const cache = getCache();
|
||||||
|
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache }));
|
||||||
};
|
};
|
||||||
export const useStyleProvider = (props: UseStyleProviderProps) => {
|
export const useStyleProvider = (props: UseStyleProviderProps) => {
|
||||||
const parentContext = useStyleInject();
|
const parentContext = useStyleInject();
|
||||||
|
|
Loading…
Reference in New Issue