mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-02 03:59:01 +08:00
feat: theme seperated (#38608)
* feat: theme seperated * chore: code clean * chore: update API
This commit is contained in:
parent
319b3966c4
commit
c0de3b2737
@ -21,7 +21,7 @@ describe('ConfigProvider.Theme', () => {
|
|||||||
|
|
||||||
const colorList = ['primaryColor', 'successColor', 'warningColor', 'errorColor', 'infoColor'];
|
const colorList = ['primaryColor', 'successColor', 'warningColor', 'errorColor', 'infoColor'];
|
||||||
|
|
||||||
colorList.forEach(colorName => {
|
colorList.forEach((colorName) => {
|
||||||
it(colorName, () => {
|
it(colorName, () => {
|
||||||
ConfigProvider.config({
|
ConfigProvider.config({
|
||||||
prefixCls: 'bamboo',
|
prefixCls: 'bamboo',
|
||||||
@ -31,7 +31,7 @@ describe('ConfigProvider.Theme', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const styles: any[] = Array.from(document.querySelectorAll('style'));
|
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'),
|
style.getAttribute('rc-util-key').includes('-dynamic-theme'),
|
||||||
);
|
);
|
||||||
expect(themeStyle).toBeTruthy();
|
expect(themeStyle).toBeTruthy();
|
||||||
@ -123,11 +123,11 @@ describe('ConfigProvider.Theme', () => {
|
|||||||
</ConfigProvider>,
|
</ConfigProvider>,
|
||||||
);
|
);
|
||||||
const dynamicStyles = Array.from(document.querySelectorAll('style[data-css-hash]')).map(
|
const dynamicStyles = Array.from(document.querySelectorAll('style[data-css-hash]')).map(
|
||||||
item => item?.innerHTML ?? '',
|
(item) => item?.innerHTML ?? '',
|
||||||
);
|
);
|
||||||
expect(
|
expect(
|
||||||
dynamicStyles.some(
|
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();
|
).toBeTruthy();
|
||||||
});
|
});
|
||||||
@ -176,4 +176,21 @@ describe('ConfigProvider.Theme', () => {
|
|||||||
);
|
);
|
||||||
expect(tokens.a).toMatchObject(tokens.b);
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -30,6 +30,7 @@ export interface ThemeConfig {
|
|||||||
components?: OverrideToken;
|
components?: OverrideToken;
|
||||||
algorithm?: MappingAlgorithm | MappingAlgorithm[];
|
algorithm?: MappingAlgorithm | MappingAlgorithm[];
|
||||||
hashed?: boolean;
|
hashed?: boolean;
|
||||||
|
seperated?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ConfigConsumerProps {
|
export interface ConfigConsumerProps {
|
||||||
|
@ -9,7 +9,8 @@ export default function useTheme(
|
|||||||
parentTheme?: ThemeConfig,
|
parentTheme?: ThemeConfig,
|
||||||
): ThemeConfig | undefined {
|
): ThemeConfig | undefined {
|
||||||
const themeConfig = theme || {};
|
const themeConfig = theme || {};
|
||||||
const parentThemeConfig: ThemeConfig = parentTheme || defaultConfig;
|
const parentThemeConfig: ThemeConfig =
|
||||||
|
themeConfig.seperated || !parentTheme ? defaultConfig : parentTheme;
|
||||||
|
|
||||||
const mergedTheme = useMemo<ThemeConfig | undefined>(
|
const mergedTheme = useMemo<ThemeConfig | undefined>(
|
||||||
() => {
|
() => {
|
||||||
|
@ -194,9 +194,10 @@ const theme = {
|
|||||||
|
|
||||||
| 属性 | 说明 | 类型 | 默认值 |
|
| 属性 | 说明 | 类型 | 默认值 |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| token | 用于修改 Design Token | `AliasToken` | - |
|
| token | Modify Design Token | `AliasToken` | - |
|
||||||
| algorithm | 用于修改 Seed Token 到 Map Token 的算法 | `(token: SeedToken) => MapToken` \| `((token: SeedToken) => MapToken)[]` | `defaultAlgorithm` |
|
| seperated | Ignore theme configured in upper ConfigProvider | boolean | - |
|
||||||
| components | 用于修改各个组件的 Component Token 以及覆盖该组件消费的 Alias Token | OverrideToken | - |
|
| 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
|
### OverrideToken
|
||||||
|
|
||||||
|
@ -195,6 +195,7 @@ const theme = {
|
|||||||
| 属性 | 说明 | 类型 | 默认值 |
|
| 属性 | 说明 | 类型 | 默认值 |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| token | 用于修改 Design Token | `AliasToken` | - |
|
| token | 用于修改 Design Token | `AliasToken` | - |
|
||||||
|
| seperated | 隔离上层 ConfigProvider 中配置的主题。 | boolean | - |
|
||||||
| algorithm | 用于修改 Seed Token 到 Map Token 的算法 | `(token: SeedToken) => MapToken` \| `((token: SeedToken) => MapToken)[]` | `defaultAlgorithm` |
|
| algorithm | 用于修改 Seed Token 到 Map Token 的算法 | `(token: SeedToken) => MapToken` \| `((token: SeedToken) => MapToken)[]` | `defaultAlgorithm` |
|
||||||
| components | 用于修改各个组件的 Component Token 以及覆盖该组件消费的 Alias Token | OverrideToken | - |
|
| components | 用于修改各个组件的 Component Token 以及覆盖该组件消费的 Alias Token | OverrideToken | - |
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user