You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.web.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @flow
  2. import { createMuiTheme } from '@material-ui/core/styles';
  3. import { font, colors, colorMap, spacing, shape, typography, breakpoints } 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: Object) {
  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 = [ ...spacing ];
  39. if (customSpacing && customSpacing.length) {
  40. newSpacing = customSpacing;
  41. }
  42. return createMuiTheme({
  43. props: {
  44. // disable ripple effect on buttons globally
  45. MuiButtonBase: {
  46. disableRipple: true
  47. }
  48. },
  49. // use token spacing array
  50. spacing: newSpacing
  51. }, {
  52. palette: newPalette,
  53. shape: newShape,
  54. typography: newTypography,
  55. breakpoints: newBreakpoints
  56. });
  57. }
  58. /**
  59. * Overwrites recursively values from object 2 into object 1 based on common keys.
  60. * (Merges object2 into object1).
  61. *
  62. * @param {Object} obj1 - The object holding the merged values.
  63. * @param {Object} obj2 - The object to compare to and take values from.
  64. * @returns {void}
  65. */
  66. function overwriteRecurrsive(obj1: Object, obj2: Object) {
  67. Object.keys(obj2).forEach(key => {
  68. if (obj1.hasOwnProperty(key)) {
  69. if (typeof obj1[key] === 'object') {
  70. overwriteRecurrsive(obj1[key], obj2[key]);
  71. } else {
  72. obj1[key] = obj2[key];
  73. }
  74. }
  75. });
  76. }