Skip to content

Commit

Permalink
[system] Support CSS grey color in sx (mui#34548)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked authored and alexfauquette committed Oct 14, 2022
1 parent 8ea23dd commit fadabf6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/mui-system/src/palette.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import style from './style';
import compose from './compose';

function transform(value, userValue) {
if (userValue === 'grey') {
return userValue;
}
return value;
}

export const color = style({
prop: 'color',
themeKey: 'palette',
transform,
});

export const bgcolor = style({
prop: 'bgcolor',
cssProperty: 'backgroundColor',
themeKey: 'palette',
transform,
});

export const backgroundColor = style({
prop: 'backgroundColor',
themeKey: 'palette',
transform,
});

const palette = compose(color, bgcolor, backgroundColor);
Expand Down
30 changes: 30 additions & 0 deletions packages/mui-system/src/palette.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from 'chai';
import palette from './palette';

const theme = {
palette: {
grey: { 100: '#f5f5f5' },
},
};

describe('palette', () => {
it('should treat grey as CSS color', () => {
const output = palette({
theme,
backgroundColor: 'grey',
});
expect(output).to.deep.equal({
backgroundColor: 'grey',
});
});

it('should treat grey.100 as theme color', () => {
const output = palette({
theme,
backgroundColor: 'grey.100',
});
expect(output).to.deep.equal({
backgroundColor: '#f5f5f5',
});
});
});
5 changes: 4 additions & 1 deletion packages/mui-system/src/style.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export interface StyleOptions<PropKey> {
* dot access in `Theme`
*/
themeKey?: string;
transform?: (cssValue: unknown) => number | string | React.CSSProperties | CSSObject;
transform?: (
cssValue: unknown,
userValue: unknown,
) => number | string | React.CSSProperties | CSSObject;
}
export function style<PropKey extends string, Theme extends object>(
options: StyleOptions<PropKey>,
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-system/src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function getValue(themeMapping, transform, propValueFinal, userValue = propValue
}

if (transform) {
value = transform(value);
value = transform(value, userValue);
}

return value;
Expand Down

0 comments on commit fadabf6

Please sign in to comment.