/
getStylesCreator.js
67 lines (58 loc) · 2 KB
/
getStylesCreator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { deepmerge } from '@material-ui/utils';
import noopTheme from './noopTheme';
function getStylesCreator(stylesOrCreator) {
const themingEnabled = typeof stylesOrCreator === 'function';
if (process.env.NODE_ENV !== 'production') {
if (typeof stylesOrCreator !== 'object' && !themingEnabled) {
console.error(
[
'Material-UI: the `styles` argument provided is invalid.',
'You need to provide a function generating the styles or a styles object.',
].join('\n'),
);
}
}
return {
create: (theme, name) => {
let styles;
try {
styles = themingEnabled ? stylesOrCreator(theme) : stylesOrCreator;
} catch (err) {
if (process.env.NODE_ENV !== 'production') {
if (themingEnabled === true && theme === noopTheme) {
// TODO: prepend error message/name instead
console.error(
[
'Material-UI: the `styles` argument provided is invalid.',
'You are providing a function without a theme in the context.',
'One of the parent elements needs to use a ThemeProvider.',
].join('\n'),
);
}
}
throw err;
}
if (!name || !theme.overrides || !theme.overrides[name]) {
return styles;
}
const overrides = theme.overrides[name];
const stylesWithOverrides = { ...styles };
Object.keys(overrides).forEach(key => {
if (process.env.NODE_ENV !== 'production') {
if (!stylesWithOverrides[key]) {
console.warn(
[
'Material-UI: you are trying to override a style that does not exist.',
`Fix the \`${key}\` key of \`theme.overrides.${name}\`.`,
].join('\n'),
);
}
}
stylesWithOverrides[key] = deepmerge(stylesWithOverrides[key], overrides[key]);
});
return stylesWithOverrides;
},
options: {},
};
}
export default getStylesCreator;