Skip to content

Commit

Permalink
[Typography] Apply font properties to typography inherit variant (#33621
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oyar99 committed Mar 14, 2023
1 parent 914ece7 commit c1a5203
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
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',
},
};

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
? ''
: capitalize(props[prop]?.toString())
}`
];

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

if (!value) {
value = propValue;
}

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',
});
});
});

0 comments on commit c1a5203

Please sign in to comment.