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

[Typography] Apply font properties to typography inherit variant #33621

Merged
merged 10 commits into from
Mar 14, 2023
2 changes: 1 addition & 1 deletion packages/mui-material/src/styles/CssVarsProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ describe('[Material UI] CssVarsProvider', () => {
);

expect(container.firstChild?.textContent).to.equal(
'htmlFontSize,pxToRem,fontFamily,fontSize,fontWeightLight,fontWeightRegular,fontWeightMedium,fontWeightBold,h1,h2,h3,h4,h5,h6,subtitle1,subtitle2,body1,body2,button,caption,overline',
'htmlFontSize,pxToRem,fontFamily,fontSize,fontWeightLight,fontWeightRegular,fontWeightMedium,fontWeightBold,h1,h2,h3,h4,h5,h6,subtitle1,subtitle2,body1,body2,button,caption,overline,inherit',
);
});
});
Expand Down
7 changes: 7 additions & 0 deletions packages/mui-material/src/styles/createTypography.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export default function createTypography(palette, typography) {
button: buildVariant(fontWeightMedium, 14, 1.75, 0.4, caseAllCaps),
caption: buildVariant(fontWeightRegular, 12, 1.66, 0.4),
overline: buildVariant(fontWeightRegular, 12, 2.66, 1, caseAllCaps),
inherit: {
fontFamily: 'inherit',
fontWeight: 'inherit',
fontSize: 'inherit',
lineHeight: 'inherit',
letterSpacing: 'inherit',
},
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
};

return deepmerge(
Expand Down
9 changes: 9 additions & 0 deletions packages/mui-material/src/styles/createTypography.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ describe('createTypography', () => {
);
});

it('should apply font CSS properties to inherit variant', () => {
const typography = createTypography(palette, {});
const fontProperties = ['fontFamily', 'fontWeight', 'fontSize', 'lineHeight', 'letterSpacing'];

fontProperties.forEach((prop) => {
expect(typography.inherit[prop]).to.equal('inherit');
});
});

describe('warnings', () => {
it('logs an error if `fontSize` is not of type number', () => {
expect(() => {
Expand Down
36 changes: 36 additions & 0 deletions packages/mui-system/src/styleFunctionSx/defaultSxConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
import { unstable_capitalize as capitalize } from '@mui/utils';
import { padding, margin } from '../spacing';
import { handleBreakpoints } from '../breakpoints';
import { borderRadius, borderTransform } from '../borders';
import { gap, rowGap, columnGap } from '../cssGrid';
import { paletteTransform } from '../palette';
import { maxWidth, sizingTransform } from '../sizing';

const createFontStyleFunction = (prop) => {
return (props) => {
if (props[prop] !== undefined && props[prop] !== null) {
const styleFromPropValue = (propValue) => {
let value =
props.theme.typography?.[
`${prop}${
props[prop] === 'default' || props[prop] === prop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, what is this props[prop] === prop for? CSS property's value being equal to it's own property?

? ''
: capitalize(props[prop]?.toString())
}`
];

if (!value) {
value = props.theme.typography?.[propValue]?.[prop];
}

if (!value) {
value = propValue;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've duplicated if statements here and on line 22

}

return {
[prop]: value,
};
};
return handleBreakpoints(props, props[prop], styleFromPropValue);
}
return null;
};
};

const defaultSxConfig = {
// borders
border: {
Expand Down Expand Up @@ -283,15 +316,18 @@ const defaultSxConfig = {
// typography
fontFamily: {
themeKey: 'typography',
style: createFontStyleFunction('fontFamily'),
},
fontSize: {
themeKey: 'typography',
style: createFontStyleFunction('fontSize'),
},
fontStyle: {
themeKey: 'typography',
},
fontWeight: {
themeKey: 'typography',
style: createFontStyleFunction('fontWeight'),
},
letterSpacing: {},
textTransform: {},
Expand Down
22 changes: 22 additions & 0 deletions packages/mui-system/src/styleFunctionSx/styleFunctionSx.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import createMixins from '@mui/material/styles/createMixins';
import createTypography from '@mui/material/styles/createTypography';
import createBreakpoints from '../createTheme/createBreakpoints';
import styleFunctionSx from './styleFunctionSx';

Expand Down Expand Up @@ -419,4 +420,25 @@ describe('styleFunctionSx', () => {
).not.to.throw();
});
});

it('resolves inherit typography properties', () => {
const result = styleFunctionSx({
theme: { typography: createTypography({}, {}) },
sx: {
fontFamily: 'inherit',
fontWeight: 'inherit',
fontSize: 'inherit',
lineHeight: 'inherit',
letterSpacing: 'inherit',
},
});

expect(result).deep.equal({
fontFamily: 'inherit',
fontWeight: 'inherit',
fontSize: 'inherit',
lineHeight: 'inherit',
letterSpacing: 'inherit',
});
});
});