Skip to content

Commit dcc1703

Browse files
authored
[docs] Add Next.js font optimization section to Pigment CSS migration (#43631)
1 parent efc1396 commit dcc1703

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

docs/data/material/migration/upgrade-to-v6/migrating-to-pigment-css.md

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,45 @@ yarn dev
212212

213213
Open the browser and navigate to the localhost URL, you should see the app running with Pigment CSS.
214214

215+
### Next.js font optimization
216+
217+
If you are using `next/font` to optimize font loading, pass a CSS variable name to the `variable` property of the font configuration and use it in the body className:
218+
219+
```diff title="app/layout.tsx"
220+
import { Roboto } from 'next/font/google';
221+
222+
const roboto = Roboto({
223+
weight: ['300', '400', '500', '700'],
224+
subsets: ['latin'],
225+
display: 'swap',
226+
+ variable: '--my-font-family',
227+
});
228+
229+
export default function RootLayout(props) {
230+
const { children } = props;
231+
return (
232+
<html lang="en">
233+
+ <body className={roboto.variable}>
234+
{children}
235+
</body>
236+
</html>
237+
);
238+
}
239+
```
240+
241+
Finally, update the `typography.fontFamily` value with the variable created in the previous step:
242+
243+
```diff title="next.config.mjs"
244+
const pigmentConfig = {
245+
transformLibraries: ['@mui/material'],
246+
theme: createTheme({
247+
+ typography: {
248+
+ fontFamily: 'var(--my-font-family)',
249+
+ },
250+
}),
251+
};
252+
```
253+
215254
### Typescript
216255

217256
If you are using TypeScript, you need to extend the Pigment CSS theme types with Material UI `Theme`.
@@ -274,7 +313,7 @@ We recommend reading the rest of the guide below to learn about the new styling
274313
Since Pigment CSS is a build-time extraction tool, it does not support owner state through callbacks. Here is an example that will throw an error at build time:
275314

276315
```js
277-
const theme = extendTheme({
316+
const theme = createTheme({
278317
components: {
279318
MuiCard: {
280319
styleOverrides: {
@@ -307,7 +346,7 @@ If you have a dynamic color based on the theme palette, you can use the `variant
307346
<codeblock>
308347

309348
```js before
310-
const theme = extendTheme({
349+
const theme = createTheme({
311350
components: {
312351
MuiCard: {
313352
styleOverrides: {
@@ -321,7 +360,7 @@ const theme = extendTheme({
321360
```
322361
323362
```js after
324-
const theme = extendTheme({
363+
const theme = createTheme({
325364
components: {
326365
MuiCard: {
327366
styleOverrides: {
@@ -352,9 +391,9 @@ Use `DefaultPropsProvider` in your main application file and move all the compon
352391
<codeblock>
353392
354393
```diff next.config|vite.config
355-
import { extendTheme } from '@mui/material';
394+
import { createTheme } from '@mui/material';
356395

357-
const customTheme = extendTheme({
396+
const customTheme = createTheme({
358397
// ...other tokens.
359398
components: {
360399
MuiButtonBase: {
@@ -639,7 +678,7 @@ Update the config file with the following code to enable right-to-left support:
639678
640679
```diff
641680
const pigmentConfig = {
642-
theme: extendTheme(),
681+
theme: createTheme(),
643682
+ css: {
644683
+ // Specify your default CSS authoring direction
645684
+ defaultDirection: 'ltr',

0 commit comments

Comments
 (0)