Skip to content

Commit

Permalink
[palette] Fix error color defaults (#10058)
Browse files Browse the repository at this point in the history
* Fix error default color augmentation, add tests

* Add documentation

* More docs fixes
  • Loading branch information
pelotom authored and oliviertassinari committed Jan 27, 2018
1 parent 9be7dfc commit 3655d4e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
9 changes: 5 additions & 4 deletions docs/src/pages/customization/themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ palette: {
contrastText: getContrastText(secondaryColor.A400),
},
error: {
light: errorColor[300],
main: errorColor[500],
dark: errorColor[700],
contrastText: getContrastText(errorColor[500]),
},
},
```
Expand All @@ -93,13 +96,11 @@ const theme = createMuiTheme({
palette: {
primary: indigo,
secondary: pink,
error: {
main: red[500],
},
error: red,
// Used by `getContrastText()` to maximize the contrast between the background and
// the text.
contrastThreshold: 3,
// Used by the functions below to shift a color's luminance by approximately
// Used to shift a color's luminance by approximately
// two indexes within its tonal palette.
// E.g., shift from Red 500 to Red 300 or Red 700.
tonalOffset: 0.2,
Expand Down
2 changes: 2 additions & 0 deletions src/styles/createPalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export default function createPalette(palette: Object) {
dark: pink.A700,
},
error = {
light: red[300],
main: red[500],
dark: red[700],
},
type = 'light',
contrastThreshold = 3,
Expand Down
54 changes: 54 additions & 0 deletions src/styles/createPalette.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ describe('createPalette()', () => {
red[500],
'should use red[500] as the default error main color',
);
assert.strictEqual(
palette.error.light,
red[300],
'should use red[300] as the default error light color',
);
assert.strictEqual(
palette.error.dark,
red[700],
'should use red[700] as the default error dark color',
);
assert.strictEqual(
palette.text,
light.text,
Expand Down Expand Up @@ -110,6 +120,16 @@ describe('createPalette()', () => {
pink[500],
'should use pink[500] as the error main color',
);
assert.strictEqual(
palette.error.light,
pink[300],
'should use pink[300] as the error light color',
);
assert.strictEqual(
palette.error.dark,
pink[700],
'should use pink[700] as the error dark color',
);
assert.strictEqual(palette.text, light.text, 'should use light theme text');
});

Expand All @@ -129,6 +149,9 @@ describe('createPalette()', () => {
},
error: {
main: pink[500],
light: pink[300],
dark: pink[700],
contrastText: '#00ff00',
},
});
assert.strictEqual(
Expand Down Expand Up @@ -176,13 +199,29 @@ describe('createPalette()', () => {
pink[500],
'should use pink[500] as the error main color',
);
assert.strictEqual(
palette.error.light,
pink[300],
'should use pink[300] as the error light color',
);
assert.strictEqual(
palette.error.dark,
pink[700],
'should use pink[700] as the error dark color',
);
assert.strictEqual(
palette.error.contrastText,
'#00ff00',
'should use #00ff00 as the error contrastText color',
);
assert.strictEqual(palette.text, light.text, 'should use light theme text');
});

it('should calculate light and dark colors if not provided', () => {
const palette = createPalette({
primary: { main: deepOrange[500] },
secondary: { main: green.A400 },
error: { main: pink[500] },
});
assert.strictEqual(
palette.primary.main,
Expand Down Expand Up @@ -214,6 +253,21 @@ describe('createPalette()', () => {
darken(green.A400, 0.3),
'should use darken(green.A400, 0.3) as the secondary dark color',
);
assert.strictEqual(
palette.error.main,
pink[500],
'should use pink[500] as the error main color',
);
assert.strictEqual(
palette.error.light,
lighten(pink[500], 0.2),
'should use lighten(pink[500], 0.2) as the error light color',
);
assert.strictEqual(
palette.error.dark,
darken(pink[500], 0.3),
'should use darken(pink[500], 0.3) as the error dark color',
);
});

it('should calculate light and dark colors using the provided tonalOffset', () => {
Expand Down

0 comments on commit 3655d4e

Please sign in to comment.