Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui] Add new Next.js integration package #39947

Merged
merged 39 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
59c2e07
Add next.js as a dependency
siriwatknp Nov 21, 2023
8a13561
add appRouter and pagesRouter
siriwatknp Nov 21, 2023
261032a
switch folder to nextjs13
siriwatknp Nov 22, 2023
c1d34cb
update appRouter
siriwatknp Nov 22, 2023
28a5345
move files to separate package
siriwatknp Nov 23, 2023
968c577
Revert "Add next.js as a dependency"
siriwatknp Nov 23, 2023
29ffa24
adopt in mui docs
siriwatknp Nov 23, 2023
ae6c266
mark emotion server and cache as optional
siriwatknp Nov 23, 2023
a7a43a3
add document
siriwatknp Nov 23, 2023
c089f60
update docs
siriwatknp Nov 23, 2023
113f142
fix build
siriwatknp Nov 23, 2023
672785a
run docs:i18n
siriwatknp Nov 23, 2023
1f22c56
update material ui nextjs alias
siriwatknp Nov 23, 2023
bf26d99
add material ui nextjs to codesandbox build
siriwatknp Nov 23, 2023
c8f4c39
fix markdown lint
siriwatknp Nov 23, 2023
30ce24e
rename folders to use nextjs version at the beginning
siriwatknp Nov 23, 2023
cb20154
update docs
siriwatknp Nov 24, 2023
1f26cd1
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp Nov 27, 2023
ce51139
add DocumentHeadTags
siriwatknp Nov 27, 2023
2fc39bb
update docs
siriwatknp Nov 28, 2023
4dfcc6a
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp Nov 28, 2023
5c39893
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp Nov 30, 2023
bcfb7d8
update yarn.lock
siriwatknp Nov 30, 2023
13db137
fix as suggested
siriwatknp Nov 30, 2023
233e4d5
redirect old page to the new one
siriwatknp Dec 1, 2023
25a370b
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp Dec 1, 2023
e2a479f
run docs:i18n
siriwatknp Dec 1, 2023
e67d68a
Apply suggestions from code review
siriwatknp Dec 4, 2023
374e5b4
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp Dec 4, 2023
2a110c7
reexport v14 using v13
siriwatknp Dec 4, 2023
2052bc2
add v14 comment
siriwatknp Dec 4, 2023
0ebe960
Apply suggestions from code review
siriwatknp Dec 6, 2023
f943604
quick writing edits
danilo-leal Dec 6, 2023
f4858d5
Merge branch 'master' into feat/nextjs-integration
siriwatknp Dec 7, 2023
d72e882
Merge branch 'master' of https://github.com/mui/material-ui into feat…
siriwatknp Dec 11, 2023
2c8e2a9
revert yarn.lock
siriwatknp Dec 11, 2023
3ebef69
fix material-nextjs package.json
siriwatknp Dec 11, 2023
dc1c3b6
replace tslint with tsc
siriwatknp Dec 11, 2023
b63e72a
try with node script
siriwatknp Dec 11, 2023
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
2 changes: 2 additions & 0 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"packages/mui-styled-engine",
"packages/mui-styled-engine-sc",
"packages/mui-material-next",
"packages/mui-material-nextjs",
"packages/mui-joy"
],
"publishDirectory": {
Expand All @@ -32,6 +33,7 @@
"@mui/utils": "packages/mui-utils/build",
"@mui/base": "packages/mui-base/build",
"@mui/material-next": "packages/mui-material-next/build",
"@mui/material-nextjs": "packages/mui-material-nextjs/build",
"@mui/joy": "packages/mui-joy/build"
},
"sandboxes": [
Expand Down
1 change: 1 addition & 0 deletions docs/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const alias = {
'@mui/utils': '../packages/mui-utils/src',
'@mui/base': '../packages/mui-base/src',
'@mui/material-next': '../packages/mui-material-next/src',
'@mui/material-nextjs': '../packages/mui-material-nextjs/src',
'@mui/joy': '../packages/mui-joy/src',
docs: './',
modules: '../modules',
Expand Down
227 changes: 0 additions & 227 deletions docs/data/material/guides/next-js-app-router/next-js-app-router.md

This file was deleted.

142 changes: 142 additions & 0 deletions docs/data/material/guides/nextjs/nextjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Next.js Integration
Copy link
Member

Choose a reason for hiding this comment

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

Very clean guide 👌


<p class="description">Learn how to use Material UI with Next.js</p>

## App Router

This section walks through the Material UI integration with the Next.js [App Router](https://nextjs.org/docs/app), an evolution of the [Pages Router](#pages-router), and, currently, the recommended way of building new Next.js applications starting from version 13.

### Intalling the dependencies

Start by ensuring that you already have `@mui/material` and `next` installed.
Then, run one of the following commands to install the dependencies:

<codeblock storageKey="package-manager">

```bash npm
npm install @mui/material-nextjs @emotion/cache
```

```bash yarn
yarn add @mui/material-nextjs @emotion/cache
```

```bash pnpm
pnpm add @mui/material-nextjs @emotion/cache
```

</codeblock>

### Configuration

Inside `app/layout.tsx`, import the `AppRouterCacheProvider` and wrap all elements under the `<body>` with it:

```diff
// app/layout.tsx
+ import { AppRouterCacheProvider } from '@mui/material-nextjs/v13-appRouter'; // or `v14-appRouter` if you are using Next.js v14

export default function RootLayout(props) {
const { children } = props;
return (
<html lang="en">
<body>
+ <AppRouterCacheProvider>{children}</AppRouterCacheProvider>
</body>
</html>
);
}
```

### Using other styling solutions

If you are using a styling solution other than Emotion to customize Material UI components, set `enableCssLayer: true` in the `options` prop:

```js
<AppRouterCacheProvider options={{ enableCssLayer: true }}>
```

This option ensures that the styles generated by Material UI will be wrapped in a CSS `@layer mui` rule, which is overridden by anonymous layer styles when using Material UI with CSS modules, Tailwind CSS, or even plain CSS without using `@layer`.

Check warning on line 58 in docs/data/material/guides/nextjs/nextjs.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "docs/data/material/guides/nextjs/nextjs.md", "range": {"start": {"line": 58, "column": 62}}}, "severity": "WARNING"}

To learn more about it, see [the MDN CSS layer documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/@layer).

## Pages Router

This section walks through the Material UI integration with the Next.js [Pages Router](https://nextjs.org/docs/pages/building-your-application), for both [Server Side Rendering](https://nextjs.org/docs/pages/building-your-application/rendering/server-side-rendering) (SSR) and [Static Site Generation](https://nextjs.org/docs/pages/building-your-application/rendering/static-site-generation) (SSG).

### Intalling the dependencies

Start by ensuring that you already have `@mui/material` and `next` installed.
Then, run one of the following commands to install the dependencies:

<codeblock storageKey="package-manager">

```bash npm
npm install @mui/material-nextjs @emotion/cache @emotion/server
```

```bash yarn
yarn add @mui/material-nextjs @emotion/cache @emotion/server
```

```bash pnpm
pnpm add @mui/material-nextjs @emotion/cache @emotion/server
```

</codeblock>

### Configuration

Inside the `pages/_document.tsx` file:

- Import `documentGetInitialProps` and use it as the Document's `getInitialProps`.
- Import `DocumentHeadTags` and render it inside the `<Head>`.

```diff
+ import { DocumentHeadTags, documentGetInitialProps } from '@mui/material-nextjs/v13-pagesRouter'; // or `v14-pagesRouter` if you are using Next.js v14

export default function MyDocument(props) {
return (
<Html lang="en">
<Head>
+ <DocumentHeadTags {...props} />
...
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}

+ MyDocument.getInitialProps = documentGetInitialProps;
```

Then, inside `pages/_app.tsx`, import the `AppCacheProvider` component and render it as the root element:

```diff
+ import { AppCacheProvider } from '@mui/material-nextjs/v13-pagesRouter'; // or `v14-pages` if you are using Next.js v14

export default function MyApp(props) {
return (
+ <AppCacheProvider {...props}>
<Head>
...
</Head>
...
+ </AppCacheProvider>
);
}
```

### TypeScript

If you are using TypeScript, add `DocumentHeadTagsProps` to the Document's props interface:

```diff
+ import type { DocumentHeadTagsProps } from '@mui/material-nextjs/v13-pagesRouter'; // or `v14-pagesRouter` if you are using Next.js v14

+ export default function MyDocument(props: DocumentProps & DocumentHeadTagsProps) {
...
}
```
Loading