Next.js 13 fonts with MUI components #45433
-
When using the new font system in Next.js 13, I had a problem with the imported fonts not being used in a few MUI components with "presentation" role (such as Dialogs, Menus, etc.). I was doing everything as it was written in documentation (https://nextjs.org/docs/basic-features/font-optimization) and I finally found out that those components are directly under body component, meaning they are above (or should i say outside of) main component. This means that the |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 8 replies
-
I found certain things weren't picked up when migrating to Nextjs 13's next/font module. I use a custom Theme.tsx from MUI, which was respecting some of the My solution was to bring the next/font import to the top of the Theme.tsx file - like so
|
Beta Was this translation helpful? Give feedback.
-
I did something like this to avoid the font being imported twice. This does not solve the case of needing different fonts in the component however.
|
Beta Was this translation helpful? Give feedback.
-
You could just specify the font to inherit from the parent, eventually inheriting the document base font.
Then set the font class on the body and provide the theme:
|
Beta Was this translation helpful? Give feedback.
-
for me this works: file : src/theme/localFonts.fonts.jsx
on my theme: file: src/theme/custom.theme.jsx
|
Beta Was this translation helpful? Give feedback.
-
This solution works for me
Please use |
Beta Was this translation helpful? Give feedback.
-
responsiveFont option not working |
Beta Was this translation helpful? Give feedback.
-
After a year I found a solution that I'm most happy with. @benwwalker 's solution is great, but works only if you want to use one font in the dialog and also you still cannot use the font variables ( My solution was to add the font variables to the
My
This will add the needed variables to the
which will successfully apply the |
Beta Was this translation helpful? Give feedback.
After a year I found a solution that I'm most happy with. @benwwalker 's solution is great, but works only if you want to use one font in the dialog and also you still cannot use the font variables (
somefont.variable
) ,as shown in the NextJS docs, because that variable is defined only inside themain
element.My solution was to add the font variables to the
MuiModal
andMuiMenu
elements the same way as I used it inside themain
element. So my MUI theme looks something like this: