|
1 | 1 | import type { ShallowRef, ExtractPropTypes, InjectionKey, Ref } from 'vue'; |
2 | | -import { provide, defineComponent, unref, inject, watch, shallowRef } from 'vue'; |
| 2 | +import { |
| 3 | + provide, |
| 4 | + defineComponent, |
| 5 | + unref, |
| 6 | + inject, |
| 7 | + watch, |
| 8 | + shallowRef, |
| 9 | + getCurrentInstance, |
| 10 | +} from 'vue'; |
3 | 11 | import CacheEntity from './Cache'; |
4 | 12 | import type { Linter } from './linters/interface'; |
5 | 13 | import type { Transformer } from './transformers/interface'; |
@@ -80,14 +88,36 @@ const StyleContextKey: InjectionKey<ShallowRef<Partial<StyleContextProps>>> = |
80 | 88 | Symbol('StyleContextKey'); |
81 | 89 |
|
82 | 90 | export type UseStyleProviderProps = Partial<StyleContextProps> | Ref<Partial<StyleContextProps>>; |
| 91 | + |
| 92 | +// fix: https://github.com/vueComponent/ant-design-vue/issues/7023 |
| 93 | +const getCache = () => { |
| 94 | + const instance = getCurrentInstance(); |
| 95 | + let cache: CacheEntity; |
| 96 | + if (instance && instance.appContext) { |
| 97 | + const globalCache = instance.appContext?.config?.globalProperties?.__ANTDV_CSSINJS_CACHE__; |
| 98 | + if (globalCache) { |
| 99 | + cache = globalCache; |
| 100 | + } else { |
| 101 | + cache = createCache(); |
| 102 | + if (instance.appContext.config.globalProperties) { |
| 103 | + instance.appContext.config.globalProperties.__ANTDV_CSSINJS_CACHE__ = cache; |
| 104 | + } |
| 105 | + } |
| 106 | + } else { |
| 107 | + cache = createCache(); |
| 108 | + } |
| 109 | + return cache; |
| 110 | +}; |
| 111 | + |
83 | 112 | const defaultStyleContext: StyleContextProps = { |
84 | 113 | cache: createCache(), |
85 | 114 | defaultCache: true, |
86 | 115 | hashPriority: 'low', |
87 | 116 | }; |
88 | 117 | // fix: https://github.com/vueComponent/ant-design-vue/issues/6912 |
89 | 118 | export const useStyleInject = () => { |
90 | | - return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache: createCache() })); |
| 119 | + const cache = getCache(); |
| 120 | + return inject(StyleContextKey, shallowRef({ ...defaultStyleContext, cache })); |
91 | 121 | }; |
92 | 122 | export const useStyleProvider = (props: UseStyleProviderProps) => { |
93 | 123 | const parentContext = useStyleInject(); |
|
0 commit comments