Skip to content
Permalink
Newer
Older
100644 121 lines (107 sloc) 3.53 KB
Ignoring revisions in .git-blame-ignore-revs.
1
import { deepmerge } from '@mui/utils';
2
import { generateUtilityClass } from '@mui/base';
3
import { createTheme as systemCreateTheme } from '@mui/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, 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') {
47
'focused',
48
'focusVisible',
49
'required',
50
'selected',
51
];
53
const traverse = (node, component) => {
54
let key;
55
56
// eslint-disable-next-line guard-for-in, no-restricted-syntax
57
for (key in node) {
58
const child = node[key];
59
if (stateClasses.indexOf(key) !== -1 && Object.keys(child).length > 0) {
60
if (process.env.NODE_ENV !== 'production') {
61
const stateClass = generateUtilityClass('', key);
64
`MUI: The \`${component}\` component increases ` +
65
`the CSS specificity of the \`${key}\` internal state.`,
66
'You can not override it like this: ',
67
JSON.stringify(node, null, 2),
68
'',
69
`Instead, you need to use the '&.${stateClass}' syntax:`,
70
JSON.stringify(
71
{
72
root: {
73
[`&.${stateClass}`]: child,
76
null,
77
2,
78
),
79
'',
80
'https://mui.com/r/state-classes-guide',
81
].join('\n'),
82
);
83
}
84
// Remove the style to prevent global conflicts.
85
node[key] = {};
90
Object.keys(muiTheme.components).forEach((component) => {
91
const styleOverrides = muiTheme.components[component].styleOverrides;
93
if (styleOverrides && component.indexOf('Mui') === 0) {
94
traverse(styleOverrides, component);
102
let warnedOnce = false;
103
104
export function createMuiTheme(...args) {
105
if (process.env.NODE_ENV !== 'production') {
106
if (!warnedOnce) {
107
warnedOnce = true;
108
console.error(
109
[
110
'MUI: the createMuiTheme function was renamed to createTheme.',
112
"You should use `import { createTheme } from '@mui/material/styles'`",
113
].join('\n'),
114
);
115
}
116
}
117
118
return createTheme(...args);
119
}
120
121
export default createTheme;