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