Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[theme] Allow palette tonalOffset light and dark values #20567

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/src/pages/customization/palette/palette.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ As in the example above, if the intention object contains custom colors using an
according to the "contrastThreshold" value.

Both the "tonalOffset" and "contrastThreshold" values may be customized as needed.
The "tonalOffset" value can either be a number between 0 and 1, which will apply to both light and dark variants, or an object with light and dark variants specified by the following TypeScript type:

```ts
type PaletteTonalOffset = number | {
light: number;
dark: number;
};
```

A higher value for "tonalOffset" will make calculated values for "light" lighter, and "dark" darker.
A higher value for "contrastThreshold" increases the point at which a background color is considered
light, and given a dark "contrastText".
Expand Down
11 changes: 9 additions & 2 deletions packages/material-ui/src/styles/createPalette.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,21 @@ export interface TypeObject {
background: TypeBackground;
}

export type PaletteTonalOffset =
| number
| {
light: number;
dark: number;
};

export const light: TypeObject;
export const dark: TypeObject;

export interface Palette {
common: CommonColors;
type: PaletteType;
contrastThreshold: number;
tonalOffset: number;
tonalOffset: PaletteTonalOffset;
primary: PaletteColor;
secondary: PaletteColor;
error: PaletteColor;
Expand Down Expand Up @@ -102,7 +109,7 @@ export interface PaletteOptions {
info?: PaletteColorOptions;
success?: PaletteColorOptions;
type?: PaletteType;
tonalOffset?: number;
tonalOffset?: PaletteTonalOffset;
contrastThreshold?: number;
common?: Partial<CommonColors>;
grey?: ColorPartial;
Expand Down
7 changes: 5 additions & 2 deletions packages/material-ui/src/styles/createPalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ export const dark = {
};

function addLightOrDark(intent, direction, shade, tonalOffset) {
const tonalOffsetLight = tonalOffset.light || tonalOffset;
const tonalOffsetDark = tonalOffset.dark || tonalOffset * 1.5;

if (!intent[direction]) {
if (intent.hasOwnProperty(shade)) {
intent[direction] = intent[shade];
} else if (direction === 'light') {
intent.light = lighten(intent.main, tonalOffset);
intent.light = lighten(intent.main, tonalOffsetLight);
} else if (direction === 'dark') {
intent.dark = darken(intent.main, tonalOffset * 1.5);
intent.dark = darken(intent.main, tonalOffsetDark);
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion packages/material-ui/src/styles/createPalette.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('createPalette()', () => {
});
});

it('should calculate light and dark colors using the provided tonalOffset', () => {
it('should calculate light and dark colors using a simple tonalOffset number value', () => {
const palette = createPalette({
primary: { main: deepOrange[500] },
tonalOffset: 0.1,
Expand All @@ -56,6 +56,22 @@ describe('createPalette()', () => {
});
});

it('should calculate light and dark colors using a custom tonalOffset object value', () => {
const palette = createPalette({
primary: { main: deepOrange[500] },
tonalOffset: {
light: 0.8,
dark: 0.5,
},
});

expect(palette.primary).to.deep.include({
main: deepOrange[500],
light: lighten(deepOrange[500], 0.8),
dark: darken(deepOrange[500], 0.5),
});
});

it('should calculate contrastText using the provided contrastThreshold', () => {
const palette = createPalette({ contrastThreshold: 7 });
expect(
Expand Down