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