Skip to content
Open
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
14 changes: 13 additions & 1 deletion packages/mui-material/src/styles/createTypography.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ export default function createTypography(palette, typography) {
},
};

// Inject fontFamily and allVariants into any custom typography variant objects in `other`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change would incorrectly treat "inherit" as a custom variant

createTheme({
  typography: {
    fontFamily: 'Inter',
    inherit: { fontSize: 'inherit' },
  },
})

// so they behave consistently with the built-in variants produced by buildVariant.
const processedOther = {};
Object.keys(other).forEach((key) => {
const value = other[key];
if (value !== null && typeof value === 'object') {
processedOther[key] = { fontFamily, ...allVariants, ...value };
} else {
processedOther[key] = value;
}
});

return deepmerge(
{
htmlFontSize,
Expand All @@ -94,7 +106,7 @@ export default function createTypography(palette, typography) {
fontWeightBold,
...variants,
},
other,
processedOther,
{
clone: false, // No need to clone deep
},
Expand Down
30 changes: 30 additions & 0 deletions packages/mui-material/src/styles/createTypography.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,36 @@ describe('createTypography', () => {
});
});

it('should apply fontFamily to custom typography variants', () => {
const typography = createTypography(palette, {
fontFamily: 'Times New Roman',
custom: {
color: '#FF0008',
textDecoration: 'underline',
},
});
expect(typography.custom.fontFamily).to.equal('Times New Roman');
});

it('should apply allVariants to custom typography variants', () => {
const typography = createTypography(palette, {
allVariants: { marginBottom: 8 },
myVariant: { fontSize: 14 },
});
expect(typography.myVariant.marginBottom).to.equal(8);
});

it('custom variant explicit fontFamily should override the global fontFamily', () => {
const typography = createTypography(palette, {
fontFamily: 'Times New Roman',
custom: {
fontFamily: 'Arial',
fontSize: 14,
},
});
expect(typography.custom.fontFamily).to.equal('Arial');
});

describe('warnings', () => {
it('logs an error if `fontSize` is not of type number', () => {
expect(() => {
Expand Down
Loading