UNPKG

4.08 kBJavaScriptView Raw
1import _formatMuiErrorMessage from "@mui/utils/formatMuiErrorMessage";
2import deepmerge from '@mui/utils/deepmerge';
3import styleFunctionSx, { unstable_defaultSxConfig as defaultSxConfig } from '@mui/system/styleFunctionSx';
4import systemCreateTheme from '@mui/system/createTheme';
5import generateUtilityClass from '@mui/utils/generateUtilityClass';
6import createMixins from "./createMixins.js";
7import createPalette from "./createPalette.js";
8import createTypography from "./createTypography.js";
9import shadows from "./shadows.js";
10import createTransitions from "./createTransitions.js";
11import zIndex from "./zIndex.js";
12import { stringifyTheme } from "./stringifyTheme.js";
13function createThemeNoVars(options = {}, ...args) {
14 const {
15 breakpoints: breakpointsInput,
16 mixins: mixinsInput = {},
17 spacing: spacingInput,
18 palette: paletteInput = {},
19 transitions: transitionsInput = {},
20 typography: typographyInput = {},
21 shape: shapeInput,
22 ...other
23 } = options;
24 if (options.vars) {
25 throw new Error(process.env.NODE_ENV !== "production" ? 'MUI: `vars` is a private field used for CSS variables support.\n' + 'Please use another name.' : _formatMuiErrorMessage(20));
26 }
27 const palette = createPalette(paletteInput);
28 const systemTheme = systemCreateTheme(options);
29 let muiTheme = deepmerge(systemTheme, {
30 mixins: createMixins(systemTheme.breakpoints, mixinsInput),
31 palette,
32 // Don't use [...shadows] until you've verified its transpiled code is not invoking the iterator protocol.
33 shadows: shadows.slice(),
34 typography: createTypography(palette, typographyInput),
35 transitions: createTransitions(transitionsInput),
36 zIndex: {
37 ...zIndex
38 }
39 });
40 muiTheme = deepmerge(muiTheme, other);
41 muiTheme = args.reduce((acc, argument) => deepmerge(acc, argument), muiTheme);
42 if (process.env.NODE_ENV !== 'production') {
43 // TODO v6: Refactor to use globalStateClassesMapping from @mui/utils once `readOnly` state class is used in Rating component.
44 const stateClasses = ['active', 'checked', 'completed', 'disabled', 'error', 'expanded', 'focused', 'focusVisible', 'required', 'selected'];
45 const traverse = (node, component) => {
46 let key;
47
48 // eslint-disable-next-line guard-for-in
49 for (key in node) {
50 const child = node[key];
51 if (stateClasses.includes(key) && Object.keys(child).length > 0) {
52 if (process.env.NODE_ENV !== 'production') {
53 const stateClass = generateUtilityClass('', key);
54 console.error([`MUI: The \`${component}\` component increases ` + `the CSS specificity of the \`${key}\` internal state.`, 'You can not override it like this: ', JSON.stringify(node, null, 2), '', `Instead, you need to use the '&.${stateClass}' syntax:`, JSON.stringify({
55 root: {
56 [`&.${stateClass}`]: child
57 }
58 }, null, 2), '', 'https://mui.com/r/state-classes-guide'].join('\n'));
59 }
60 // Remove the style to prevent global conflicts.
61 node[key] = {};
62 }
63 }
64 };
65 Object.keys(muiTheme.components).forEach(component => {
66 const styleOverrides = muiTheme.components[component].styleOverrides;
67 if (styleOverrides && component.startsWith('Mui')) {
68 traverse(styleOverrides, component);
69 }
70 });
71 }
72 muiTheme.unstable_sxConfig = {
73 ...defaultSxConfig,
74 ...other?.unstable_sxConfig
75 };
76 muiTheme.unstable_sx = function sx(props) {
77 return styleFunctionSx({
78 sx: props,
79 theme: this
80 });
81 };
82 muiTheme.toRuntimeSource = stringifyTheme; // for Pigment CSS integration
83
84 return muiTheme;
85}
86let warnedOnce = false;
87export function createMuiTheme(...args) {
88 if (process.env.NODE_ENV !== 'production') {
89 if (!warnedOnce) {
90 warnedOnce = true;
91 console.error(['MUI: the createMuiTheme function was renamed to createTheme.', '', "You should use `import { createTheme } from '@mui/material/styles'`"].join('\n'));
92 }
93 }
94 return createThemeNoVars(...args);
95}
96export default createThemeNoVars;
\No newline at end of file