Permalink
material-ui/packages/mui-material/src/styles/createTheme.js
Newer
100644
144 lines (128 sloc)
4.07 KB
Ignoring revisions in .git-blame-ignore-revs.
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';
10
import createPalette from './createPalette';
11
import createTypography from './createTypography';
13
import createTransitions from './createTransitions';
16
function createTheme(options = {}, ...args) {
22
transitions: transitionsInput = {},
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);
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
});
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,
92
'https://mui.com/r/state-classes-guide',
96
// Remove the style to prevent global conflicts.
97
node[key] = {};
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;