diff --git a/components/_util/cssinjs/StyleContext.tsx b/components/_util/cssinjs/StyleContext.tsx index 9cb925215..5710c6b68 100644 --- a/components/_util/cssinjs/StyleContext.tsx +++ b/components/_util/cssinjs/StyleContext.tsx @@ -1,9 +1,11 @@ -import type { InjectionKey, Ref } from 'vue'; -import { unref, computed, inject } from 'vue'; +import type { App, ComputedRef, InjectionKey, Ref } from 'vue'; +import { provide, defineComponent, unref, computed, inject } from 'vue'; import CacheEntity from './Cache'; import type { Linter } from './linters/interface'; import type { Transformer } from './transformers/interface'; - +import { arrayType, objectType } from '../type'; +import PropTypes from '../vue-types'; +import initDefaultProps from '../props-util/initDefaultProps'; export const ATTR_TOKEN = 'data-token-hash'; export const ATTR_MARK = 'data-css-hash'; export const ATTR_DEV_CACHE_PATH = 'data-dev-cache-path'; @@ -71,41 +73,83 @@ export interface StyleContextProps { linters?: Linter[]; } -const StyleContextKey: InjectionKey = Symbol('StyleContextKey'); +const StyleContextKey: InjectionKey>> = + Symbol('StyleContextKey'); export type StyleProviderProps = Partial | Ref>; -export const useStyleInject = () => { - return inject(StyleContextKey, { - hashPriority: 'low', - cache: createCache(), - defaultCache: true, - }); +const defaultStyleContext: StyleContextProps = { + cache: createCache(), + defaultCache: true, + hashPriority: 'low', }; -export const useStyleProvider = (props: StyleContextProps) => { +export const useStyleInject = () => { + return inject( + StyleContextKey, + computed(() => defaultStyleContext), + ); +}; +export const useStyleProvider = (props: StyleProviderProps) => { const parentContext = useStyleInject(); - const context = computed(() => { - const mergedContext: StyleContextProps = { - ...parentContext, + const context = computed>(() => { + const mergedContext: Partial = { + ...parentContext.value, }; const propsValue = unref(props); - (Object.keys(propsValue) as (keyof StyleContextProps)[]).forEach(key => { + Object.keys(propsValue).forEach(key => { const value = propsValue[key]; if (propsValue[key] !== undefined) { - (mergedContext as any)[key] = value; + mergedContext[key] = value; } }); const { cache } = propsValue; mergedContext.cache = mergedContext.cache || createCache(); - mergedContext.defaultCache = !cache && parentContext.defaultCache; - + mergedContext.defaultCache = !cache && parentContext.value.defaultCache; return mergedContext; }); - + provide(StyleContextKey, context); return context; }; +const AStyleProviderProps = () => ({ + autoClear: PropTypes.bool, + /** @private Test only. Not work in production. */ + mock: PropTypes.oneOf(['server', 'client'] as const), + /** + * Only set when you need ssr to extract style on you own. + * If not provided, it will auto create