Skip to content

Commit 8a3ed32

Browse files
committed
docs: update compatiple #6415
1 parent 1151bda commit 8a3ed32

File tree

7 files changed

+101
-59
lines changed

7 files changed

+101
-59
lines changed

components/_util/cssinjs/StyleContext.tsx

+48-47
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import type { App, ComputedRef, InjectionKey, Ref } from 'vue';
2-
import { provide, defineComponent, unref, computed, inject } from 'vue';
1+
import type { ShallowRef, ExtractPropTypes, InjectionKey, Ref } from 'vue';
2+
import { provide, defineComponent, unref, inject, watch, shallowRef } from 'vue';
33
import CacheEntity from './Cache';
44
import type { Linter } from './linters/interface';
55
import type { Transformer } from './transformers/interface';
6-
import { arrayType, objectType } from '../type';
7-
import PropTypes from '../vue-types';
6+
import { arrayType, booleanType, objectType, someType, stringType, withInstall } from '../type';
87
import initDefaultProps from '../props-util/initDefaultProps';
98
export const ATTR_TOKEN = 'data-token-hash';
109
export const ATTR_MARK = 'data-css-hash';
@@ -73,61 +72,62 @@ export interface StyleContextProps {
7372
linters?: Linter[];
7473
}
7574

76-
const StyleContextKey: InjectionKey<ComputedRef<Partial<StyleContextProps>>> =
75+
const StyleContextKey: InjectionKey<ShallowRef<Partial<StyleContextProps>>> =
7776
Symbol('StyleContextKey');
7877

79-
export type StyleProviderProps = Partial<StyleContextProps> | Ref<Partial<StyleContextProps>>;
78+
export type UseStyleProviderProps = Partial<StyleContextProps> | Ref<Partial<StyleContextProps>>;
8079
const defaultStyleContext: StyleContextProps = {
8180
cache: createCache(),
8281
defaultCache: true,
8382
hashPriority: 'low',
8483
};
8584
export const useStyleInject = () => {
86-
return inject(
87-
StyleContextKey,
88-
computed(() => defaultStyleContext),
89-
);
85+
return inject(StyleContextKey, shallowRef({ ...defaultStyleContext }));
9086
};
91-
export const useStyleProvider = (props: StyleProviderProps) => {
87+
export const useStyleProvider = (props: UseStyleProviderProps) => {
9288
const parentContext = useStyleInject();
89+
const context = shallowRef<Partial<StyleContextProps>>({ ...defaultStyleContext });
90+
watch(
91+
[props, parentContext],
92+
() => {
93+
const mergedContext: Partial<StyleContextProps> = {
94+
...parentContext.value,
95+
};
96+
const propsValue = unref(props);
97+
Object.keys(propsValue).forEach(key => {
98+
const value = propsValue[key];
99+
if (propsValue[key] !== undefined) {
100+
mergedContext[key] = value;
101+
}
102+
});
93103

94-
const context = computed<Partial<StyleContextProps>>(() => {
95-
const mergedContext: Partial<StyleContextProps> = {
96-
...parentContext.value,
97-
};
98-
const propsValue = unref(props);
99-
Object.keys(propsValue).forEach(key => {
100-
const value = propsValue[key];
101-
if (propsValue[key] !== undefined) {
102-
mergedContext[key] = value;
103-
}
104-
});
105-
106-
const { cache } = propsValue;
107-
mergedContext.cache = mergedContext.cache || createCache();
108-
mergedContext.defaultCache = !cache && parentContext.value.defaultCache;
109-
return mergedContext;
110-
});
104+
const { cache } = propsValue;
105+
mergedContext.cache = mergedContext.cache || createCache();
106+
mergedContext.defaultCache = !cache && parentContext.value.defaultCache;
107+
context.value = mergedContext;
108+
},
109+
{ immediate: true },
110+
);
111111
provide(StyleContextKey, context);
112112
return context;
113113
};
114-
const AStyleProviderProps = () => ({
115-
autoClear: PropTypes.bool,
114+
export const styleProviderProps = () => ({
115+
autoClear: booleanType(),
116116
/** @private Test only. Not work in production. */
117-
mock: PropTypes.oneOf(['server', 'client'] as const),
117+
mock: stringType<'server' | 'client'>(),
118118
/**
119119
* Only set when you need ssr to extract style on you own.
120120
* If not provided, it will auto create <style /> on the end of Provider in server side.
121121
*/
122122
cache: objectType<CacheEntity>(),
123123
/** Tell children that this context is default generated context */
124-
defaultCache: PropTypes.bool,
124+
defaultCache: booleanType(),
125125
/** Use `:where` selector to reduce hashId css selector priority */
126-
hashPriority: PropTypes.oneOf(['low', 'high'] as const),
126+
hashPriority: stringType<HashPriority>(),
127127
/** Tell cssinjs where to inject style in */
128-
container: PropTypes.oneOfType([objectType<Element>(), objectType<ShadowRoot>()]),
128+
container: someType<Element | ShadowRoot>(),
129129
/** Component wil render inline `<style />` for fallback in SSR. Not recommend. */
130-
ssrInline: PropTypes.bool,
130+
ssrInline: booleanType(),
131131
/** Transform css before inject in document. Please note that `transformers` do not support dynamic update */
132132
transformers: arrayType<Transformer[]>(),
133133
/**
@@ -137,20 +137,21 @@ const AStyleProviderProps = () => ({
137137
*/
138138
linters: arrayType<Linter[]>(),
139139
});
140+
export type StyleProviderProps = Partial<ExtractPropTypes<ReturnType<typeof styleProviderProps>>>;
141+
export const StyleProvider = withInstall(
142+
defineComponent({
143+
name: 'AStyleProvider',
144+
inheritAttrs: false,
145+
props: initDefaultProps(styleProviderProps(), defaultStyleContext),
146+
setup(props, { slots }) {
147+
useStyleProvider(props);
148+
return () => slots.default?.();
149+
},
150+
}),
151+
);
140152

141-
export const StyleProvider = defineComponent({
142-
name: 'AStyleProvider',
143-
props: initDefaultProps(AStyleProviderProps(), defaultStyleContext),
144-
setup(props, { slots }) {
145-
useStyleProvider(props);
146-
return () => slots.default?.();
147-
},
148-
});
149-
150-
StyleProvider.install = function (app: App) {
151-
app.component(StyleProvider.name, StyleProvider);
152-
};
153153
export default {
154154
useStyleInject,
155155
useStyleProvider,
156+
StyleProvider,
156157
};

components/_util/cssinjs/index.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,34 @@ import useStyleRegister, { extractStyle } from './hooks/useStyleRegister';
44
import Keyframes from './Keyframes';
55
import type { Linter } from './linters';
66
import { legacyNotSelectorLinter, logicalPropertiesLinter } from './linters';
7-
import type { StyleContextProps } from './StyleContext';
7+
import type { StyleContextProps, StyleProviderProps } from './StyleContext';
88
import { createCache, useStyleInject, useStyleProvider, StyleProvider } from './StyleContext';
99
import type { DerivativeFunc, TokenType } from './theme';
1010
import { createTheme, Theme } from './theme';
1111
import type { Transformer } from './transformers/interface';
1212
import legacyLogicalPropertiesTransformer from './transformers/legacyLogicalProperties';
1313

14+
const cssinjs = {
15+
Theme,
16+
createTheme,
17+
useStyleRegister,
18+
useCacheToken,
19+
createCache,
20+
useStyleInject,
21+
useStyleProvider,
22+
Keyframes,
23+
extractStyle,
24+
25+
// Transformer
26+
legacyLogicalPropertiesTransformer,
27+
28+
// Linters
29+
logicalPropertiesLinter,
30+
legacyNotSelectorLinter,
31+
32+
// cssinjs
33+
StyleProvider,
34+
};
1435
export {
1536
Theme,
1637
createTheme,
@@ -40,4 +61,7 @@ export type {
4061
Transformer,
4162
Linter,
4263
StyleContextProps,
64+
StyleProviderProps,
4365
};
66+
67+
export default cssinjs;

components/components.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export { default as Comment } from './comment';
4848

4949
export type { ConfigProviderProps } from './config-provider';
5050
export { default as ConfigProvider } from './config-provider';
51-
export * from './_util/cssinjs';
51+
5252
export type { DatePickerProps } from './date-picker';
5353
export {
5454
default as DatePicker,

components/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type { App } from 'vue';
22

33
import * as components from './components';
44
import { default as version } from './version';
5+
import cssinjs from './_util/cssinjs';
56
export * from './components';
7+
export * from './_util/cssinjs';
8+
69
export { default as theme } from './theme';
710
export const install = function (app: App) {
811
Object.keys(components).forEach(key => {
@@ -11,7 +14,7 @@ export const install = function (app: App) {
1114
app.use(component);
1215
}
1316
});
14-
17+
app.use(cssinjs.StyleProvider);
1518
app.config.globalProperties.$message = components.message;
1619
app.config.globalProperties.$notification = components.notification;
1720
app.config.globalProperties.$info = components.Modal.info;
@@ -23,7 +26,7 @@ export const install = function (app: App) {
2326
return app;
2427
};
2528

26-
export { version };
29+
export { version, cssinjs };
2730

2831
export default {
2932
version,

site/src/App.vue

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<template>
2-
<a-config-provider :locale="locale" :theme="themeConfig">
3-
<SiteToken>
4-
<router-view />
5-
</SiteToken>
6-
</a-config-provider>
2+
<a-style-provider :hash-priority="hashPriority">
3+
<a-config-provider :locale="locale" :theme="themeConfig">
4+
<SiteToken>
5+
<router-view />
6+
</SiteToken>
7+
</a-config-provider>
8+
</a-style-provider>
79
</template>
810

911
<script lang="ts">
@@ -57,6 +59,10 @@ export default defineComponent({
5759
const themeConfig = computed(() => {
5860
return { algorithm: getAlgorithm([...new Set([theme.value, compactTheme.value])]) };
5961
});
62+
const hashPriority = ref('low' as const);
63+
watch(hashPriority, () => {
64+
location.reload();
65+
});
6066
// useSiteToken();
6167
const responsive = computed(() => {
6268
if (colSize.value === 'xs') {
@@ -132,7 +138,7 @@ export default defineComponent({
132138
},
133139
{ immediate: true },
134140
);
135-
return { globalConfig, locale, themeConfig };
141+
return { globalConfig, locale, themeConfig, hashPriority };
136142
},
137143
});
138144
</script>

site/src/vueDocs/compatible-style.en-US.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ It will turn `:where` to class selector:
2727
}
2828
```
2929

30-
Note: After turning off the `:where` downgrade, you may need to manually adjust the priority of some styles.
30+
Note:
31+
32+
1、After turning off the `:where` downgrade, you may need to manually adjust the priority of some styles.
33+
34+
2、hashPriority not support change,you can reload for new value.
3135

3236
### CSS Logical Properties
3337

site/src/vueDocs/compatible-style.zh-CN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ Ant Design Vue 的 CSS-in-JS 默认通过 `:where` 选择器降低 CSS Selector
2727
}
2828
```
2929

30-
注意:关闭 `:where` 降权后,你可能需要手动调整一些样式的优先级。
30+
注意:
31+
32+
1、关闭 `:where` 降权后,你可能需要手动调整一些样式的优先级。
33+
34+
2、hashPriority 不支持动态修改,修改后请刷新浏览器
3135

3236
### CSS 逻辑属性
3337

0 commit comments

Comments
 (0)