Skip to content
Permalink
Newer
Older
100644 120 lines (106 sloc) 3.59 KB
Ignoring revisions in .git-blame-ignore-revs.
1
import { deepmerge } from '@material-ui/utils';
2
import { generateUtilityClass } from '@material-ui/unstyled';
3
import { createTheme as systemCreateTheme } from '@material-ui/system';
4
import createMixins from './createMixins';
5
import createPalette from './createPalette';
6
import createTypography from './createTypography';
7
import shadows from './shadows';
8
import createTransitions from './createTransitions';
9
import zIndex from './zIndex';
10
11
function createTheme(options = {}, ...args) {
13
breakpoints: breakpointsInput,
14
mixins: mixinsInput = {},
15
spacing: spacingInput,
16
palette: paletteInput = {},
17
transitions: transitionsInput = {},
18
typography: typographyInput = {},
19
shape: shapeInput,
20
...other
21
} = options;
22
23
const palette = createPalette(paletteInput);
24
const systemTheme = systemCreateTheme(options);
26
let muiTheme = deepmerge(systemTheme, {
27
mixins: createMixins(systemTheme.breakpoints, systemTheme.spacing, mixinsInput),
28
palette,
29
// Don't use [...shadows] until you've verified its transpiled code is not invoking the iterator protocol.
30
shadows: shadows.slice(),
31
typography: createTypography(palette, typographyInput),
32
transitions: createTransitions(transitionsInput),
33
zIndex: { ...zIndex },
34
});
36
muiTheme = deepmerge(muiTheme, other);
37
muiTheme = args.reduce((acc, argument) => deepmerge(acc, argument), muiTheme);
39
if (process.env.NODE_ENV !== 'production') {
42
'checked',
43
'disabled',
44
'error',
45
'focused',
46
'focusVisible',
47
'required',
48
'expanded',
49
'selected',
50
];
52
const traverse = (node, component) => {
53
let key;
54
55
// eslint-disable-next-line guard-for-in, no-restricted-syntax
56
for (key in node) {
57
const child = node[key];
58
if (pseudoClasses.indexOf(key) !== -1 && Object.keys(child).length > 0) {
59
if (process.env.NODE_ENV !== 'production') {
60
const pseudoClass = generateUtilityClass('', key);
63
`Material-UI: The \`${component}\` component increases ` +
64
`the CSS specificity of the \`${key}\` internal state.`,
65
'You can not override it like this: ',
66
JSON.stringify(node, null, 2),
67
'',
68
`Instead, you need to use the '&.${pseudoClass}' syntax:`,
69
JSON.stringify(
70
{
71
root: {
72
[`&.${pseudoClass}`]: child,
75
null,
76
2,
77
),
78
'',
79
'https://material-ui.com/r/pseudo-classes-guide',
80
].join('\n'),
81
);
82
}
83
// Remove the style to prevent global conflicts.
84
node[key] = {};
89
Object.keys(muiTheme.components).forEach((component) => {
90
const styleOverrides = muiTheme.components[component].styleOverrides;
92
if (styleOverrides && component.indexOf('Mui') === 0) {
93
traverse(styleOverrides, component);
101
let warnedOnce = false;
102
103
export function createMuiTheme(...args) {
104
if (process.env.NODE_ENV !== 'production') {
105
if (!warnedOnce) {
106
warnedOnce = true;
107
console.error(
108
[
109
'Material-UI: the createMuiTheme function was renamed to createTheme.',
110
'',
111
"You should use `import { createTheme } from '@material-ui/core/styles'`",
112
].join('\n'),
113
);
114
}
115
}
116
117
return createTheme(...args);
118
}
119
120
export default createTheme;