mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 23:35:38 +08:00
94b3d03765
* feat: component theme * feat: component token support algorith, * docs: fix form * chore: add test * chore: fix test case * chore: code clean * chore: code clean * chore: code clean * docs: update toc * chore: update cssinjs * chore
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import useMemo from 'rc-util/lib/hooks/useMemo';
|
|
import isEqual from 'rc-util/lib/isEqual';
|
|
import type { OverrideToken } from '../../theme/interface';
|
|
import type { ThemeConfig } from '../context';
|
|
import { defaultConfig } from '../../theme/internal';
|
|
|
|
export default function useTheme(
|
|
theme?: ThemeConfig,
|
|
parentTheme?: ThemeConfig,
|
|
): ThemeConfig | undefined {
|
|
const themeConfig = theme || {};
|
|
const parentThemeConfig: ThemeConfig =
|
|
themeConfig.inherit === false || !parentTheme ? defaultConfig : parentTheme;
|
|
|
|
return useMemo<ThemeConfig | undefined>(
|
|
() => {
|
|
if (!theme) {
|
|
return parentTheme;
|
|
}
|
|
|
|
// Override
|
|
const mergedComponents = {
|
|
...parentThemeConfig.components,
|
|
};
|
|
|
|
Object.keys(theme.components || {}).forEach((componentName: keyof OverrideToken) => {
|
|
mergedComponents[componentName] = {
|
|
...mergedComponents[componentName],
|
|
...theme.components![componentName],
|
|
} as any;
|
|
});
|
|
|
|
// Base token
|
|
return {
|
|
...parentThemeConfig,
|
|
...themeConfig,
|
|
|
|
token: {
|
|
...parentThemeConfig.token,
|
|
...themeConfig.token,
|
|
},
|
|
components: mergedComponents,
|
|
};
|
|
},
|
|
[themeConfig, parentThemeConfig],
|
|
(prev, next) =>
|
|
prev.some((prevTheme, index) => {
|
|
const nextTheme = next[index];
|
|
|
|
return !isEqual(prevTheme, nextTheme, true);
|
|
}),
|
|
);
|
|
}
|