Skip to content

Commit 32d647c

Browse files
[theme] Warn when palette structure is wrong (#20253)
1 parent 81b1cd8 commit 32d647c

File tree

2 files changed

+61
-59
lines changed

2 files changed

+61
-59
lines changed

packages/material-ui/src/styles/createPalette.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,6 @@ export default function createPalette(palette) {
132132
// Bootstrap: https://github.com/twbs/bootstrap/blob/1d6e3710dd447de1a200f29e8fa521f8a0908f70/scss/_functions.scss#L59
133133
// and material-components-web https://github.com/material-components/material-components-web/blob/ac46b8863c4dab9fc22c4c662dc6bd1b65dd652f/packages/mdc-theme/_functions.scss#L54
134134
function getContrastText(background) {
135-
if (!background) {
136-
throw new TypeError(
137-
`Material-UI: missing background argument in getContrastText(${background}).`,
138-
);
139-
}
140-
141135
const contrastText =
142136
getContrastRatio(background, dark.text.primary) >= contrastThreshold
143137
? dark.text.primary
@@ -159,21 +153,42 @@ export default function createPalette(palette) {
159153
return contrastText;
160154
}
161155

162-
function augmentColor(color, mainShade = 500, lightShade = 300, darkShade = 700) {
156+
const augmentColor = (color, mainShade = 500, lightShade = 300, darkShade = 700) => {
163157
color = { ...color };
164158
if (!color.main && color[mainShade]) {
165159
color.main = color[mainShade];
166160
}
167161

168-
if (process.env.NODE_ENV !== 'production') {
169-
if (!color.main) {
170-
throw new Error(
171-
[
172-
'Material-UI: the color provided to augmentColor(color) is invalid.',
173-
`The color object needs to have a \`main\` property or a \`${mainShade}\` property.`,
174-
].join('\n'),
175-
);
176-
}
162+
if (!color.main) {
163+
throw new Error(
164+
[
165+
'Material-UI: the color provided to augmentColor(color) is invalid.',
166+
`The color object needs to have a \`main\` property or a \`${mainShade}\` property.`,
167+
].join('\n'),
168+
);
169+
}
170+
171+
if (typeof color.main !== 'string') {
172+
throw new Error(
173+
[
174+
'Material-UI: the color provided to augmentColor(color) is invalid.',
175+
`\`color.main\` should be a string, but \`${JSON.stringify(
176+
color.main,
177+
)}\` was provided instead.`,
178+
'',
179+
'Did you intend to use one of the following approaches?',
180+
'',
181+
'import { green } from "@material-ui/core/colors";',
182+
'',
183+
'const theme1 = createMuiTheme({ palette: {',
184+
' primary: green,',
185+
'} });',
186+
'',
187+
'const theme2 = createMuiTheme({ palette: {',
188+
' primary: { main: green[500] },',
189+
'} });',
190+
].join('\n'),
191+
);
177192
}
178193

179194
addLightOrDark(color, 'light', lightShade, tonalOffset);
@@ -183,7 +198,7 @@ export default function createPalette(palette) {
183198
}
184199

185200
return color;
186-
}
201+
};
187202

188203
const types = { dark, light };
189204

packages/material-ui/src/styles/createPalette.test.js

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ import { darken, lighten } from './colorManipulator';
55
import createPalette, { dark, light } from './createPalette';
66

77
describe('createPalette()', () => {
8-
beforeEach(() => {
9-
consoleErrorMock.spy();
10-
});
11-
12-
afterEach(() => {
13-
consoleErrorMock.reset();
14-
});
15-
168
it('should create a palette with a rich color object', () => {
179
const palette = createPalette({
1810
primary: deepOrange,
@@ -85,26 +77,11 @@ describe('createPalette()', () => {
8577
pink.A400,
8678
);
8779
expect(palette.text, 'should use dark theme text').to.equal(dark.text);
88-
expect(consoleErrorMock.callCount()).to.equal(0);
89-
});
90-
91-
it('logs an error when an invalid type is specified', () => {
92-
createPalette({ type: 'foo' });
93-
expect(consoleErrorMock.callCount()).to.equal(1);
94-
expect(consoleErrorMock.messages()[0]).to.match(
95-
/Material-UI: the palette type `foo` is not supported/,
96-
);
9780
});
9881

9982
describe('augmentColor', () => {
10083
const palette = createPalette({});
10184

102-
it('should throw when the input is invalid', () => {
103-
expect(() => {
104-
palette.augmentColor({});
105-
}).to.throw(/The color object needs to have a/);
106-
});
107-
10885
it('should accept a color', () => {
10986
const color1 = palette.augmentColor(indigo);
11087
expect(color1).to.deep.include({
@@ -135,20 +112,37 @@ describe('createPalette()', () => {
135112
});
136113
});
137114

138-
describe('getContrastText', () => {
139-
it('throws an exception with a falsy argument', () => {
140-
const { getContrastText } = createPalette({});
115+
it('should create a palette with unique object references', () => {
116+
const redPalette = createPalette({ background: { paper: 'red' } });
117+
const bluePalette = createPalette({ background: { paper: 'blue' } });
118+
expect(redPalette).to.not.equal(bluePalette);
119+
expect(redPalette.background).to.not.equal(bluePalette.background);
120+
});
121+
122+
describe('warnings', () => {
123+
beforeEach(() => {
124+
consoleErrorMock.spy();
125+
});
141126

142-
[
143-
[undefined, 'missing background argument in getContrastText(undefined)'],
144-
[null, 'missing background argument in getContrastText(null)'],
145-
['', 'missing background argument in getContrastText()'],
146-
[0, 'missing background argument in getContrastText(0)'],
147-
].forEach((testEntry) => {
148-
const [argument, errorMessage] = testEntry;
127+
afterEach(() => {
128+
consoleErrorMock.reset();
129+
});
149130

150-
expect(() => getContrastText(argument), errorMessage).to.throw();
151-
});
131+
it('throws an exception when an invalid type is specified', () => {
132+
createPalette({ type: 'foo' });
133+
expect(consoleErrorMock.callCount()).to.equal(1);
134+
expect(consoleErrorMock.messages()[0]).to.include(
135+
'Material-UI: the palette type `foo` is not supported',
136+
);
137+
});
138+
139+
it('throws an exception when a wrong color is provided', () => {
140+
expect(() => createPalette({ primary: '#fff' })).to.throw(
141+
'The color object needs to have a `main` property or a `500` property.',
142+
);
143+
expect(() => createPalette({ primary: { main: { foo: 'bar' } } })).to.throw(
144+
'`color.main` should be a string, but `{"foo":"bar"}` was provided instead.',
145+
);
152146
});
153147

154148
it('logs an error when the contrast ratio does not reach AA', () => {
@@ -164,11 +158,4 @@ describe('createPalette()', () => {
164158
);
165159
});
166160
});
167-
168-
it('should create a palette with unique object references', () => {
169-
const redPalette = createPalette({ background: { paper: 'red' } });
170-
const bluePalette = createPalette({ background: { paper: 'blue' } });
171-
expect(redPalette).to.not.equal(bluePalette);
172-
expect(redPalette.background).to.not.equal(bluePalette.background);
173-
});
174161
});

0 commit comments

Comments
 (0)