Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.web.ts 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { Theme } from '@mui/material';
  2. import { adaptV4Theme, createTheme } from '@mui/material/styles';
  3. import { breakpoints, colorMap, colors, font, shape, spacing, typography } from '../base/ui/Tokens';
  4. import { createColorTokens } from '../base/ui/utils';
  5. /**
  6. * Creates MUI branding theme based on the custom theme json.
  7. *
  8. * @param {Object} customTheme - The branded custom theme.
  9. * @returns {Object} - The MUI theme.
  10. */
  11. export function createMuiBrandingTheme(customTheme: Theme) {
  12. const {
  13. palette: customPalette,
  14. shape: customShape,
  15. typography: customTypography,
  16. breakpoints: customBreakpoints,
  17. spacing: customSpacing
  18. } = customTheme;
  19. const newPalette = createColorTokens(colorMap, colors);
  20. if (customPalette) {
  21. overwriteRecurrsive(newPalette, customPalette);
  22. }
  23. const newShape = { ...shape };
  24. if (customShape) {
  25. overwriteRecurrsive(newShape, customShape);
  26. }
  27. const newTypography = {
  28. font: { ...font },
  29. ...typography
  30. };
  31. if (customTypography) {
  32. overwriteRecurrsive(newTypography, customTypography);
  33. }
  34. const newBreakpoints = { ...breakpoints };
  35. if (customBreakpoints) {
  36. overwriteRecurrsive(newBreakpoints, customBreakpoints);
  37. }
  38. let newSpacing: any = [ ...spacing ];
  39. if (customSpacing?.length) {
  40. newSpacing = customSpacing;
  41. }
  42. return createTheme(adaptV4Theme({
  43. spacing: newSpacing,
  44. palette: newPalette,
  45. shape: newShape,
  46. // @ts-ignore
  47. typography: newTypography,
  48. // @ts-ignore
  49. breakpoints: newBreakpoints
  50. }));
  51. }
  52. /**
  53. * Overwrites recursively values from object 2 into object 1 based on common keys.
  54. * (Merges object2 into object1).
  55. *
  56. * @param {Object} obj1 - The object holding the merged values.
  57. * @param {Object} obj2 - The object to compare to and take values from.
  58. * @returns {void}
  59. */
  60. function overwriteRecurrsive(obj1: Object, obj2: Object) {
  61. Object.keys(obj2).forEach(key => {
  62. if (obj1.hasOwnProperty(key)) {
  63. if (typeof obj1[key as keyof typeof obj1] === 'object') {
  64. overwriteRecurrsive(obj1[key as keyof typeof obj1], obj2[key as keyof typeof obj2]);
  65. } else {
  66. // @ts-ignore
  67. obj1[key] = obj2[key];
  68. }
  69. }
  70. });
  71. }