/
createMuiTheme.js
116 lines (107 loc) · 3.24 KB
/
createMuiTheme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import deepmerge from 'deepmerge'; // < 1kb payload overhead when lodash/merge is > 3kb.
import isPlainObject from 'is-plain-object';
import createBreakpoints from './createBreakpoints';
import createMixins from './createMixins';
import createPalette from './createPalette';
import createTypography from './createTypography';
import shadows from './shadows';
import shape from './shape';
import createSpacing from './createSpacing';
import transitions from './transitions';
import zIndex from './zIndex';
function createMuiTheme(options = {}) {
const {
breakpoints: breakpointsInput = {},
mixins: mixinsInput = {},
palette: paletteInput = {},
shadows: shadowsInput,
spacing: spacingInput,
typography: typographyInput = {},
...other
} = options;
const palette = createPalette(paletteInput);
const breakpoints = createBreakpoints(breakpointsInput);
const spacing = createSpacing(spacingInput);
const muiTheme = {
breakpoints,
direction: 'ltr',
mixins: createMixins(breakpoints, spacing, mixinsInput),
overrides: {}, // Inject custom styles
palette,
props: {}, // Inject custom props
shadows: shadowsInput || shadows,
typography: createTypography(palette, typographyInput),
spacing,
...deepmerge(
{
shape,
transitions,
zIndex,
},
other,
{
isMergeableObject: isPlainObject,
},
),
};
if (process.env.NODE_ENV !== 'production') {
const pseudoClasses = [
'checked',
'disabled',
'error',
'focused',
'focusVisible',
'required',
'expanded',
'selected',
];
const traverse = (node, parentKey, depth = 1) => {
let key;
// eslint-disable-next-line guard-for-in, no-restricted-syntax
for (key in node) {
const child = node[key];
if (depth === 1) {
if (key.indexOf('Mui') === 0 && child) {
traverse(child, key, depth + 1);
}
} else if (pseudoClasses.indexOf(key) !== -1 && Object.keys(child).length > 0) {
if (process.env.NODE_ENV !== 'production') {
console.error(
[
`Material-UI: the \`${parentKey}\` component increases ` +
`the CSS specificity of the \`${key}\` internal state.`,
'You can not override it like this: ',
JSON.stringify(node, null, 2),
'',
'Instead, you need to use the $ruleName syntax:',
JSON.stringify(
{
root: {
[`&$${key}`]: child,
},
},
null,
2,
),
'',
'https://material-ui.com/r/pseudo-classes-guide',
].join('\n'),
);
}
// Remove the style to prevent global conflicts.
node[key] = {};
}
}
};
traverse(muiTheme.overrides);
}
if (process.env.NODE_ENV !== 'production') {
if (muiTheme.shadows.length !== 25) {
console.error(
'Material-UI: the shadows array provided to createMuiTheme should support 25 elevations.',
);
}
}
return muiTheme;
}
export default createMuiTheme;