amis-rpc-design/node_modules/antd/es/config-provider/hooks/useTheme.js

25 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-10-07 19:42:30 +08:00
import useMemo from "rc-util/es/hooks/useMemo";
import isEqual from "rc-util/es/isEqual";
import { defaultConfig } from '../../theme/internal';
export default function useTheme(theme, parentTheme) {
const themeConfig = theme || {};
const parentThemeConfig = themeConfig.inherit === false || !parentTheme ? defaultConfig : parentTheme;
return useMemo(() => {
if (!theme) {
return parentTheme;
}
// Override
const mergedComponents = Object.assign({}, parentThemeConfig.components);
Object.keys(theme.components || {}).forEach(componentName => {
mergedComponents[componentName] = Object.assign(Object.assign({}, mergedComponents[componentName]), theme.components[componentName]);
});
// Base token
return Object.assign(Object.assign(Object.assign({}, parentThemeConfig), themeConfig), {
token: Object.assign(Object.assign({}, parentThemeConfig.token), themeConfig.token),
components: mergedComponents
});
}, [themeConfig, parentThemeConfig], (prev, next) => prev.some((prevTheme, index) => {
const nextTheme = next[index];
return !isEqual(prevTheme, nextTheme, true);
}));
}