2022-03-24 18:44:42 +08:00
|
|
|
import useMemo from 'rc-util/lib/hooks/useMemo';
|
2022-12-27 15:46:51 +08:00
|
|
|
import isEqual from 'rc-util/lib/isEqual';
|
2022-06-27 14:25:59 +08:00
|
|
|
import type { OverrideToken } from '../../theme/interface';
|
2022-03-24 18:44:42 +08:00
|
|
|
import type { ThemeConfig } from '../context';
|
2022-11-23 20:22:38 +08:00
|
|
|
import { defaultConfig } from '../../theme/internal';
|
2022-03-24 18:44:42 +08:00
|
|
|
|
|
|
|
export default function useTheme(
|
|
|
|
theme?: ThemeConfig,
|
|
|
|
parentTheme?: ThemeConfig,
|
|
|
|
): ThemeConfig | undefined {
|
|
|
|
const themeConfig = theme || {};
|
2022-11-17 00:22:15 +08:00
|
|
|
const parentThemeConfig: ThemeConfig =
|
2022-11-17 01:06:33 +08:00
|
|
|
themeConfig.inherit === false || !parentTheme ? defaultConfig : parentTheme;
|
2022-03-24 18:44:42 +08:00
|
|
|
|
|
|
|
const mergedTheme = useMemo<ThemeConfig | undefined>(
|
|
|
|
() => {
|
|
|
|
if (!theme) {
|
|
|
|
return parentTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override
|
2022-10-18 23:18:11 +08:00
|
|
|
const mergedComponents = {
|
2022-09-15 21:26:04 +08:00
|
|
|
...parentThemeConfig.components,
|
2022-03-24 18:44:42 +08:00
|
|
|
};
|
|
|
|
|
2022-09-15 21:26:04 +08:00
|
|
|
Object.keys(theme.components || {}).forEach((componentName: keyof OverrideToken) => {
|
2022-10-18 23:18:11 +08:00
|
|
|
mergedComponents[componentName] = {
|
|
|
|
...mergedComponents[componentName],
|
2022-09-15 21:26:04 +08:00
|
|
|
...theme.components![componentName],
|
2022-03-24 18:44:42 +08:00
|
|
|
} as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Base token
|
2022-10-18 23:18:11 +08:00
|
|
|
return {
|
2022-03-24 18:44:42 +08:00
|
|
|
...parentThemeConfig,
|
|
|
|
...themeConfig,
|
|
|
|
|
|
|
|
token: {
|
|
|
|
...parentThemeConfig.token,
|
|
|
|
...themeConfig.token,
|
|
|
|
},
|
2022-10-18 23:18:11 +08:00
|
|
|
components: mergedComponents,
|
2022-03-24 18:44:42 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
[themeConfig, parentThemeConfig],
|
|
|
|
(prev, next) =>
|
|
|
|
prev.some((prevTheme, index) => {
|
|
|
|
const nextTheme = next[index];
|
|
|
|
|
2022-12-27 15:46:51 +08:00
|
|
|
return !isEqual(prevTheme, nextTheme, true);
|
2022-03-24 18:44:42 +08:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return mergedTheme;
|
|
|
|
}
|