Skip to content

[Typography] Add applyRobotoLetterSpacing option to fix letter spacing with CSS variable fontFamily#48474

Closed
starboyvarun wants to merge 1 commit into
mui:masterfrom
starboyvarun:47575-typography-roboto-letter-spacing
Closed

[Typography] Add applyRobotoLetterSpacing option to fix letter spacing with CSS variable fontFamily#48474
starboyvarun wants to merge 1 commit into
mui:masterfrom
starboyvarun:47575-typography-roboto-letter-spacing

Conversation

@starboyvarun
Copy link
Copy Markdown
Contributor

Fixes #47575

Problem

The Next.js font optimization guide recommends setting typography.fontFamily: 'var(--font-roboto)' in the theme. However, createTypography applies Roboto-specific letter spacing only when fontFamily === defaultFontFamily (strict string equality). A CSS variable reference never matches that string, so the letter spacing is silently dropped — even though the font in use is still Roboto.

This was identified by @Janpot in #47575. A previous PR (#47627) attempted heuristic string detection which was rightly rejected as too fragile.

Solution

Add an explicit applyRobotoLetterSpacing option to typography (option 2 from @Janpot's comment). It defaults to fontFamily === defaultFontFamily, preserving existing behaviour for all current users. Callers who reference Roboto via a CSS variable can opt in:

createTheme({
  typography: {
    fontFamily: 'var(--font-roboto)',
    applyRobotoLetterSpacing: true,
  },
})
  • No heuristics, no string matching
  • Fully backwards compatible — existing themes are unaffected
  • Explicit, documentable, and easy to understand

Changes

  • createTypography.js — extract applyRobotoLetterSpacing (default fontFamily === defaultFontFamily) and use it in buildVariant
  • createTypography.d.ts — add type with JSDoc
  • nextjs.md — add applyRobotoLetterSpacing: true to both App Router and Pages Router font optimization examples

@Janpot Janpot requested review from Copilot and siriwatknp and removed request for Copilot and siriwatknp May 4, 2026 17:47
@code-infra-dashboard
Copy link
Copy Markdown

Deploy preview

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+29B(+0.01%) 🔺+16B(+0.01%)
@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.

@Janpot Janpot added the type: new feature Expand the scope of the product to solve a new problem. label May 4, 2026
@Janpot Janpot added the customization: theme Higher level theming customizability. label May 4, 2026
@@ -29,6 +29,12 @@ export interface FontStyle {

export interface FontStyleOptions extends Partial<FontStyle> {
allVariants?: React.CSSProperties | undefined;
/**
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.

IMO this is too niche to justify adding to the API surface #47575 (comment)

@siriwatknp
Copy link
Copy Markdown
Member

siriwatknp commented May 5, 2026

I'm closing this PR. Would you mind creating a separate PR to update the docs instead?

Thank you for your effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

customization: theme Higher level theming customizability. type: new feature Expand the scope of the product to solve a new problem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

next/font recommendation breaks letter-spacing

4 participants