Skip to content
Permalink
Newer
Older
100644 129 lines (114 sloc) 3.76 KB
Ignoring revisions in .git-blame-ignore-revs.
1
import { deepmerge } from '@mui/utils';
2
import { createTheme as systemCreateTheme } from '@mui/system';
3
import MuiError from '@mui/utils/macros/MuiError.macro';
4
import generateUtilityClass from '../generateUtilityClass';
5
import createMixins from './createMixins';
6
import createPalette from './createPalette';
7
import createTypography from './createTypography';
8
import shadows from './shadows';
9
import createTransitions from './createTransitions';
10
import zIndex from './zIndex';
11
12
function createTheme(options = {}, ...args) {
14
breakpoints: breakpointsInput,
15
mixins: mixinsInput = {},
16
spacing: spacingInput,
17
palette: paletteInput = {},
18
transitions: transitionsInput = {},
19
typography: typographyInput = {},
20
shape: shapeInput,
21
...other
22
} = options;
23
24
if (options.vars) {
25
throw new MuiError(
26
'MUI: `vars` is a private field used for CSS variables support.\n' +
27
'Please use another name.',
28
);
29
}
30
31
const palette = createPalette(paletteInput);
32
const systemTheme = systemCreateTheme(options);
34
let muiTheme = deepmerge(systemTheme, {
35
mixins: createMixins(systemTheme.breakpoints, mixinsInput),
36
palette,
37
// Don't use [...shadows] until you've verified its transpiled code is not invoking the iterator protocol.
38
shadows: shadows.slice(),
39
typography: createTypography(palette, typographyInput),
40
transitions: createTransitions(transitionsInput),
41
zIndex: { ...zIndex },
42
});
44
muiTheme = deepmerge(muiTheme, other);
45
muiTheme = args.reduce((acc, argument) => deepmerge(acc, argument), muiTheme);
47
if (process.env.NODE_ENV !== 'production') {
55
'focused',
56
'focusVisible',
57
'required',
58
'selected',
59
];
61
const traverse = (node, component) => {
62
let key;
63
64
// eslint-disable-next-line guard-for-in, no-restricted-syntax
65
for (key in node) {
66
const child = node[key];
67
if (stateClasses.indexOf(key) !== -1 && Object.keys(child).length > 0) {
68
if (process.env.NODE_ENV !== 'production') {
69
const stateClass = generateUtilityClass('', key);
72
`MUI: The \`${component}\` component increases ` +
73
`the CSS specificity of the \`${key}\` internal state.`,
74
'You can not override it like this: ',
75
JSON.stringify(node, null, 2),
76
'',
77
`Instead, you need to use the '&.${stateClass}' syntax:`,
78
JSON.stringify(
79
{
80
root: {
81
[`&.${stateClass}`]: child,
84
null,
85
2,
86
),
87
'',
88
'https://mui.com/r/state-classes-guide',
89
].join('\n'),
90
);
91
}
92
// Remove the style to prevent global conflicts.
93
node[key] = {};
98
Object.keys(muiTheme.components).forEach((component) => {
99
const styleOverrides = muiTheme.components[component].styleOverrides;
101
if (styleOverrides && component.indexOf('Mui') === 0) {
102
traverse(styleOverrides, component);
110
let warnedOnce = false;
111
112
export function createMuiTheme(...args) {
113
if (process.env.NODE_ENV !== 'production') {
114
if (!warnedOnce) {
115
warnedOnce = true;
116
console.error(
117
[
118
'MUI: the createMuiTheme function was renamed to createTheme.',
120
"You should use `import { createTheme } from '@mui/material/styles'`",
121
].join('\n'),
122
);
123
}
124
}
125
126
return createTheme(...args);
127
}
128
129
export default createTheme;