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

[examples] Fix CSS modules integration #30381

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions docs/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ function AppWrapper(props) {
return (
<React.Fragment>
<NextHead>
<meta name="viewport" content="initial-scale=1, width=device-width" />
{fonts.map((font) => (
<link rel="stylesheet" href={font} key={font} />
))}
Expand Down
1 change: 0 additions & 1 deletion docs/src/modules/components/Head.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default function Head(props) {
const preview = card.startsWith('http') ? card : `${HOST}${card}`;
return (
<NextHead>
<meta name="viewport" content="initial-scale=1, width=device-width" />
Copy link
Member Author

Choose a reason for hiding this comment

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

It's stable, no need to set it on each page. _app should do it.

<title>{title}</title>
<meta name="description" content={description} />
{/* Twitter */}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useUserLanguage } from 'docs/src/modules/utils/i18n';
*
* - /docs/src/modules/components/Link.tsx
* - /examples/nextjs/src/Link.tsx
* - /examples/nextjs-with-typescript/src/components/Link.tsx
* - /examples/nextjs-with-typescript/src/Link.tsx
Copy link
Member Author

@oliviertassinari oliviertassinari Dec 23, 2021

Choose a reason for hiding this comment

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

Flatten the src folder to make it easier to discover for new users (less overwhelming).

*/

// Add support for the sx prop for consistency with the other branches.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/guides/routing/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const LinkBehavior = React.forwardRef((props, ref) => (
Next.js has [a custom Link component](https://nextjs.org/docs/api-reference/next/link).
The [example folder](https://github.com/mui-org/material-ui/tree/HEAD/examples/nextjs-with-typescript) provides adapters for usage with MUI.

- The first version of the adapter is the [`NextLinkComposed`](https://github.com/mui-org/material-ui/blob/HEAD/examples/nextjs-with-typescript/src/components/Link.tsx) component.
- The first version of the adapter is the [`NextLinkComposed`](https://github.com/mui-org/material-ui/blob/HEAD/examples/nextjs-with-typescript/src/Link.tsx) component.
This component is unstyled and only responsible for handling the navigation.
The prop `href` was renamed `to` to avoid a naming conflict.
This is similar to react-router's Link component.
Expand Down
4 changes: 4 additions & 0 deletions examples/create-react-app-with-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ or:

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/create-react-app-with-typescript)

<!-- #default-branch-switch -->

[![Edit on StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/mui-org/material-ui/tree/master/examples/create-react-app-with-typescript)
Comment on lines +27 to +29
Copy link
Member Author

Choose a reason for hiding this comment

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

An alternative


## The idea behind the example

This example demonstrates how you can use [Create React App](https://github.com/facebookincubator/create-react-app) with [TypeScript](https://github.com/Microsoft/TypeScript).
Expand Down
4 changes: 4 additions & 0 deletions examples/create-react-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ or:

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/create-react-app)

<!-- #default-branch-switch -->

[![Edit on StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/mui-org/material-ui/tree/master/examples/create-react-app)

## The idea behind the example

<!-- #default-branch-switch -->
Expand Down
6 changes: 0 additions & 6 deletions examples/gatsby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ npm install
npm run develop
```

or:

<!-- #default-branch-switch -->

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/gatsby)
Comment on lines -21 to -25
Copy link
Member Author

Choose a reason for hiding this comment

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

It's not working and Gatsby's popularity is low, trending downward, so probably not worth trying to fix this.


## The idea behind the example

The project uses [Gatsby](https://github.com/gatsbyjs/gatsby), which is a static site generator for React.
Expand Down
18 changes: 16 additions & 2 deletions examples/gatsby/plugins/gatsby-plugin-mui-emotion/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
/* eslint-disable import/prefer-default-export */
import * as React from 'react';
import { CacheProvider } from '@emotion/react';
import createEmotionServer from '@emotion/server/create-instance';
import { renderToString } from 'react-dom/server';
import getEmotionCache from './getEmotionCache';

// Inject MUI styles first to match with the prepend: true configuration.
export const onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
Copy link
Member Author

@oliviertassinari oliviertassinari Dec 23, 2021

Choose a reason for hiding this comment

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

This fixes the initial HTML output (before JS hydration), #30358 was not enough.

const headComponents = getHeadComponents();
headComponents.sort((x, y) => {
if (x.key === 'emotion-css-global' || x.key === 'emotion-css') {
return -1;
}
if (y.key === 'style') {
return 1;
}
return 0;
});
replaceHeadComponents(headComponents);
};

export const replaceRenderer = ({ bodyComponent, setHeadComponents, replaceBodyHTMLString }) => {
const cache = getEmotionCache();
const { extractCriticalToChunks } = createEmotionServer(cache);
Expand All @@ -17,7 +31,7 @@ export const replaceRenderer = ({ bodyComponent, setHeadComponents, replaceBodyH
emotionStyles.styles.map((style) => (
<style
data-emotion={`${style.key} ${style.ids.join(' ')}`}
key={style.key}
key={`emotion-${style.key}`}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: style.css }}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import createCache from '@emotion/cache';

// prepend: true moves MUI styles to the top of the <head> so they're loaded first.
// It allows developers to easily override MUI styles with other styling solutions, like CSS modules.
export default function getEmotionCache() {
return createCache({ key: 'css', prepend: true });
}
5 changes: 0 additions & 5 deletions examples/gatsby/sandbox.config.json

This file was deleted.

2 changes: 2 additions & 0 deletions examples/joy-cra-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ or:

<!-- #default-branch-switch -->

[![Edit on StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/mui-org/material-ui/tree/master/examples/joy-cra-typescript)

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/joy-cra-typescript)
23 changes: 0 additions & 23 deletions examples/nextjs-with-typescript/.editorconfig

This file was deleted.

5 changes: 0 additions & 5 deletions examples/nextjs-with-typescript/.eslintrc.json

This file was deleted.

2 changes: 2 additions & 0 deletions examples/nextjs-with-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ or:

<!-- #default-branch-switch -->

[![Edit on StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/mui-org/material-ui/tree/master/examples/nextjs-with-typescript)

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/nextjs-with-typescript)

## The idea behind the example
Expand Down
1 change: 0 additions & 1 deletion examples/nextjs-with-typescript/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs-with-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"post-update": "echo \"codesandbox preview only, need an update\" && yarn upgrade --latest"
Copy link
Member Author

Choose a reason for hiding this comment

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

Fix regression, it was removed in #29974.

},
"dependencies": {
"@emotion/cache": "latest",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { AppProps } from 'next/app';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { CacheProvider, EmotionCache } from '@emotion/react';
import theme from '../styles/theme';
import createEmotionCache from '../utils/createEmotionCache';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache();
Expand All @@ -19,7 +19,6 @@ export default function MyApp(props: MyAppProps) {
return (
<CacheProvider value={emotionCache}>
<Head>
<title>Change title in _app.tsx</title>
Copy link
Member Author

Choose a reason for hiding this comment

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

Should be set on each page, not once for all.

<meta name="viewport" content="initial-scale=1, width=device-width" />
</Head>
<ThemeProvider theme={theme}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import theme from '../styles/theme';
import createEmotionCache from '../utils/createEmotionCache';
import theme from '../src/theme';
import createEmotionCache from '../src/createEmotionCache';

export default class MyDocument extends Document {
render() {
Expand All @@ -11,11 +11,13 @@ export default class MyDocument extends Document {
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/static/favicon.ico" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
{/* Inject MUI styles first to match with the prepend: true configuration. */}
{(this.props as any).emotionStyleTags}
Copy link
Member Author

Choose a reason for hiding this comment

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

This fixes the initial HTML output (before JS hydration). Improve support of CSS modules and Tailwind CSS

Copy link
Member

Choose a reason for hiding this comment

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

Yep, it should fix the issue in #29648. Thanks!

</Head>
<body>
<Main />
Expand Down Expand Up @@ -81,7 +83,6 @@ MyDocument.getInitialProps = async (ctx) => {

return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [...emotionStyleTags, ...React.Children.toArray(initialProps.styles)],
emotionStyleTags,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Container from '@mui/material/Container';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Link from '../components/Link';
import ProTip from '../components/ProTip';
import Copyright from '../components/Copyright';
import Link from '../src/Link';
import ProTip from '../src/ProTip';
import Copyright from '../src/Copyright';

const About: NextPage = () => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type { NextPage } from 'next';
import Container from '@mui/material/Container';
import Typography from '@mui/material/Typography';
import Box from '@mui/material/Box';
import Link from '../components/Link';
import ProTip from '../components/ProTip';
import Copyright from '../components/Copyright';
import Link from '../src/Link';
import ProTip from '../src/ProTip';
import Copyright from '../src/Copyright';

const Home: NextPage = () => {
return (
Expand Down
4 changes: 0 additions & 4 deletions examples/nextjs-with-typescript/public/vercel.svg

This file was deleted.

5 changes: 0 additions & 5 deletions examples/nextjs-with-typescript/sandbox.config.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ export type LinkProps = {
activeClassName?: string;
as?: NextLinkProps['as'];
href: NextLinkProps['href'];
linkAs?: NextLinkProps['as']; // Useful when the as prop is shallow by styled().
noLinkStyle?: boolean;
} & Omit<NextLinkComposedProps, 'to' | 'linkAs' | 'href'> &
Omit<MuiLinkProps, 'href'>;

// A styled version of the Next.js Link component:
// https://nextjs.org/docs/#with-link
// https://nextjs.org/docs/api-reference/next/link
Copy link
Member Author

Choose a reason for hiding this comment

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

Regression introduced in #29974

const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(function Link(props, ref) {
const {
activeClassName = 'active',
Expand Down
7 changes: 7 additions & 0 deletions examples/nextjs-with-typescript/src/createEmotionCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import createCache from '@emotion/cache';

// prepend: true moves MUI styles to the top of the <head> so they're loaded first.
// It allows developers to easily override MUI styles with other styling solutions, like CSS modules.
export default function createEmotionCache() {
return createCache({ key: 'css', prepend: true });
}
10 changes: 0 additions & 10 deletions examples/nextjs-with-typescript/src/pages/api/hello.ts

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions examples/nextjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ or:

<!-- #default-branch-switch -->

[![Edit on StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/mui-org/material-ui/tree/master/examples/nextjs)

[![Edit on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/mui-org/material-ui/tree/master/examples/nextjs)

## The idea behind the example
Expand Down
3 changes: 3 additions & 0 deletions examples/nextjs/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
reactStrictMode: true,
};
Comment on lines +1 to +3
Copy link
Member Author

Choose a reason for hiding this comment

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

A healthy default for any new project. It was added in #29974 for TypeScript but we forgot about the JS version.

19 changes: 12 additions & 7 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@
"name": "nextjs",
"version": "5.0.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"post-update": "echo \"codesandbox preview only, need an update\" && yarn upgrade --latest"
},
"dependencies": {
"@emotion/cache": "latest",
"@emotion/react": "latest",
"@emotion/styled": "latest",
"@emotion/server": "latest",
"@emotion/styled": "latest",
"@mui/icons-material": "latest",
"@mui/material": "latest",
"clsx": "latest",
Copy link
Member Author

Choose a reason for hiding this comment

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

No longer needed with the styled() API

"next": "latest",
"prop-types": "latest",
"react": "latest",
"react-dom": "latest"
},
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"post-update": "echo \"codesandbox preview only, need an update\" && yarn upgrade --latest"
"devDependencies": {
"eslint": "latest",
"eslint-config-next": "latest"
}
}
1 change: 0 additions & 1 deletion examples/nextjs/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default function MyApp(props) {
return (
<CacheProvider value={emotionCache}>
<Head>
<title>My page</title>
<meta name="viewport" content="initial-scale=1, width=device-width" />
</Head>
<ThemeProvider theme={theme}>
Expand Down
6 changes: 4 additions & 2 deletions examples/nextjs/pages/_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ export default class MyDocument extends Document {
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
<link rel="shortcut icon" href="/static/favicon.ico" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"
/>
{/* Inject MUI styles first to match with the prepend: true configuration. */}
{this.props.emotionStyleTags}
</Head>
<body>
<Main />
Expand Down Expand Up @@ -80,7 +83,6 @@ MyDocument.getInitialProps = async (ctx) => {

return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [...emotionStyleTags, ...React.Children.toArray(initialProps.styles)],
emotionStyleTags,
};
};
Binary file added examples/nextjs/public/favicon.ico
Binary file not shown.
5 changes: 0 additions & 5 deletions examples/nextjs/sandbox.config.json

This file was deleted.

Loading