Skip to content

[Typography] Apply fontFamily to custom typography variants#48501

Open
starboyvarun wants to merge 1 commit into
mui:masterfrom
starboyvarun:35939-typography-custom-variant-fontfamily
Open

[Typography] Apply fontFamily to custom typography variants#48501
starboyvarun wants to merge 1 commit into
mui:masterfrom
starboyvarun:35939-typography-custom-variant-fontfamily

Conversation

@starboyvarun
Copy link
Copy Markdown
Contributor

Summary

Fixes #35939.

Custom typography variants added via createTheme({ typography: { myVariant: { color: 'red' } } }) did not inherit the theme fontFamily (or allVariants CSS). They ended up with the browser's default font because they were placed in other and deep-merged as-is, bypassing the buildVariant call that injects fontFamily for the 13 built-in variants (h1overline, etc.).

Fix: In createTypography, iterate over other and for each object-valued key inject fontFamily and allVariants before the merge, matching buildVariant behavior. Explicit per-variant fontFamily inside other is still respected because the user's value wins: { fontFamily, ...allVariants, ...value }.

Test plan

  • New test: "should apply fontFamily to custom typography variants"
  • New test: "should apply allVariants to custom typography variants"
  • New test: "custom variant explicit fontFamily should override the global fontFamily"
  • Existing typography tests pass

Custom variants added via `createTheme({ typography: { myVariant: { ... } } })`
were placed in the `other` object and deep-merged as-is, without the
`fontFamily` (or `allVariants`) injection that `buildVariant` applies to
the 13 built-in variants. This caused custom variants to always render
with the browser default font regardless of the theme fontFamily.

Iterate over `other` and inject `fontFamily` and `allVariants` into each
value that is an object, matching the behavior of `buildVariant`. Explicit
per-variant `fontFamily` overrides in `other` are preserved because the
spread is `{ fontFamily, ...allVariants, ...value }`.
@code-infra-dashboard
Copy link
Copy Markdown

Deploy preview

https://deploy-preview-48501--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+113B(+0.02%) 🔺+42B(+0.03%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

},
};

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

@zannager zannager added the scope: typography Changes related to typography. label May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: typography Changes related to typography.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global fonts not being applied on custom variants

3 participants