feat: theme seperated (#38608)

* feat: theme seperated

* chore: code clean

* chore: update API
This commit is contained in:
MadCcc 2022-11-17 00:22:15 +08:00 committed by GitHub
parent 319b3966c4
commit c0de3b2737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 8 deletions

View File

@ -21,7 +21,7 @@ describe('ConfigProvider.Theme', () => {
const colorList = ['primaryColor', 'successColor', 'warningColor', 'errorColor', 'infoColor'];
colorList.forEach(colorName => {
colorList.forEach((colorName) => {
it(colorName, () => {
ConfigProvider.config({
prefixCls: 'bamboo',
@ -31,7 +31,7 @@ describe('ConfigProvider.Theme', () => {
});
const styles: any[] = Array.from(document.querySelectorAll('style'));
const themeStyle = styles.find(style =>
const themeStyle = styles.find((style) =>
style.getAttribute('rc-util-key').includes('-dynamic-theme'),
);
expect(themeStyle).toBeTruthy();
@ -123,11 +123,11 @@ describe('ConfigProvider.Theme', () => {
</ConfigProvider>,
);
const dynamicStyles = Array.from(document.querySelectorAll('style[data-css-hash]')).map(
item => item?.innerHTML ?? '',
(item) => item?.innerHTML ?? '',
);
expect(
dynamicStyles.some(
style => style.includes('.ant-input-number') && style.includes('width:50.1234px'),
(style) => style.includes('.ant-input-number') && style.includes('width:50.1234px'),
),
).toBeTruthy();
});
@ -176,4 +176,21 @@ describe('ConfigProvider.Theme', () => {
);
expect(tokens.a).toMatchObject(tokens.b);
});
it('theme seperated should work', () => {
let tokenRef: any;
const Demo = () => {
const [, token] = useToken();
tokenRef = token;
return null;
};
render(
<ConfigProvider theme={{ token: { colorPrimary: '#1890ff' } }}>
<ConfigProvider theme={{ seperated: true }}>
<Demo />
</ConfigProvider>
</ConfigProvider>,
);
expect(tokenRef?.colorPrimaryText).toBe('#1677ff');
});
});

View File

@ -30,6 +30,7 @@ export interface ThemeConfig {
components?: OverrideToken;
algorithm?: MappingAlgorithm | MappingAlgorithm[];
hashed?: boolean;
seperated?: boolean;
}
export interface ConfigConsumerProps {

View File

@ -9,7 +9,8 @@ export default function useTheme(
parentTheme?: ThemeConfig,
): ThemeConfig | undefined {
const themeConfig = theme || {};
const parentThemeConfig: ThemeConfig = parentTheme || defaultConfig;
const parentThemeConfig: ThemeConfig =
themeConfig.seperated || !parentTheme ? defaultConfig : parentTheme;
const mergedTheme = useMemo<ThemeConfig | undefined>(
() => {

View File

@ -194,9 +194,10 @@ const theme = {
| 属性 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| token | 用于修改 Design Token | `AliasToken` | - |
| algorithm | 用于修改 Seed Token 到 Map Token 的算法 | `(token: SeedToken) => MapToken` \| `((token: SeedToken) => MapToken)[]` | `defaultAlgorithm` |
| components | 用于修改各个组件的 Component Token 以及覆盖该组件消费的 Alias Token | OverrideToken | - |
| token | Modify Design Token | `AliasToken` | - |
| seperated | Ignore theme configured in upper ConfigProvider | boolean | - |
| algorithm | Modify the algorithms of theme | `(token: SeedToken) => MapToken` \| `((token: SeedToken) => MapToken)[]` | `defaultAlgorithm` |
| components | Modify Component Token and Alias Token applied to components | OverrideToken | - |
### OverrideToken

View File

@ -195,6 +195,7 @@ const theme = {
| 属性 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| token | 用于修改 Design Token | `AliasToken` | - |
| seperated | 隔离上层 ConfigProvider 中配置的主题。 | boolean | - |
| algorithm | 用于修改 Seed Token 到 Map Token 的算法 | `(token: SeedToken) => MapToken` \| `((token: SeedToken) => MapToken)[]` | `defaultAlgorithm` |
| components | 用于修改各个组件的 Component Token 以及覆盖该组件消费的 Alias Token | OverrideToken | - |