2022-09-13 07:36:00 +00:00
|
|
|
import { Theme } from '@mui/material';
|
2022-09-27 07:10:28 +00:00
|
|
|
import { adaptV4Theme, createTheme } from '@mui/material/styles';
|
2021-11-15 08:37:54 +00:00
|
|
|
|
2022-09-27 07:10:28 +00:00
|
|
|
import { breakpoints, colorMap, colors, font, shape, spacing, typography } from '../base/ui/Tokens';
|
2021-11-15 08:37:54 +00:00
|
|
|
import { createColorTokens } from '../base/ui/utils';
|
2020-05-22 06:36:24 +00:00
|
|
|
|
2021-11-15 08:37:54 +00:00
|
|
|
/**
|
|
|
|
* Creates MUI branding theme based on the custom theme json.
|
|
|
|
*
|
|
|
|
* @param {Object} customTheme - The branded custom theme.
|
|
|
|
* @returns {Object} - The MUI theme.
|
|
|
|
*/
|
2022-09-13 07:36:00 +00:00
|
|
|
export function createMuiBrandingTheme(customTheme: Theme) {
|
2021-11-15 08:37:54 +00:00
|
|
|
const {
|
|
|
|
palette: customPalette,
|
|
|
|
shape: customShape,
|
|
|
|
typography: customTypography,
|
|
|
|
breakpoints: customBreakpoints,
|
|
|
|
spacing: customSpacing
|
|
|
|
} = customTheme;
|
|
|
|
|
|
|
|
const newPalette = createColorTokens(colorMap, colors);
|
|
|
|
|
|
|
|
if (customPalette) {
|
|
|
|
overwriteRecurrsive(newPalette, customPalette);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newShape = { ...shape };
|
|
|
|
|
|
|
|
if (customShape) {
|
|
|
|
overwriteRecurrsive(newShape, customShape);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newTypography = {
|
|
|
|
font: { ...font },
|
|
|
|
...typography
|
|
|
|
};
|
|
|
|
|
|
|
|
if (customTypography) {
|
|
|
|
overwriteRecurrsive(newTypography, customTypography);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newBreakpoints = { ...breakpoints };
|
|
|
|
|
|
|
|
if (customBreakpoints) {
|
|
|
|
overwriteRecurrsive(newBreakpoints, customBreakpoints);
|
|
|
|
}
|
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
let newSpacing: any = [ ...spacing ];
|
2021-11-15 08:37:54 +00:00
|
|
|
|
2022-09-08 09:52:36 +00:00
|
|
|
if (customSpacing?.length) {
|
2021-11-15 08:37:54 +00:00
|
|
|
newSpacing = customSpacing;
|
|
|
|
}
|
|
|
|
|
2022-09-13 07:36:00 +00:00
|
|
|
return createTheme(adaptV4Theme({
|
|
|
|
spacing: newSpacing,
|
2021-11-15 08:37:54 +00:00
|
|
|
palette: newPalette,
|
|
|
|
shape: newShape,
|
2022-09-13 07:36:00 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
2021-11-15 08:37:54 +00:00
|
|
|
typography: newTypography,
|
2022-09-13 07:36:00 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
2021-11-15 08:37:54 +00:00
|
|
|
breakpoints: newBreakpoints
|
2022-09-13 07:36:00 +00:00
|
|
|
}));
|
2021-11-15 08:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-16 21:16:18 +00:00
|
|
|
* Overwrites recursively values from object 2 into object 1 based on common keys.
|
|
|
|
* (Merges object2 into object1).
|
|
|
|
*
|
|
|
|
* @param {Object} obj1 - The object holding the merged values.
|
|
|
|
* @param {Object} obj2 - The object to compare to and take values from.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-11-15 08:37:54 +00:00
|
|
|
function overwriteRecurrsive(obj1: Object, obj2: Object) {
|
|
|
|
Object.keys(obj2).forEach(key => {
|
|
|
|
if (obj1.hasOwnProperty(key)) {
|
2022-08-26 09:54:03 +00:00
|
|
|
if (typeof obj1[key as keyof typeof obj1] === 'object') {
|
|
|
|
overwriteRecurrsive(obj1[key as keyof typeof obj1], obj2[key as keyof typeof obj2]);
|
2021-11-15 08:37:54 +00:00
|
|
|
} else {
|
2022-08-26 09:54:03 +00:00
|
|
|
// @ts-ignore
|
2021-11-15 08:37:54 +00:00
|
|
|
obj1[key] = obj2[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|