-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flatten the |
||
*/ | ||
|
||
// Add support for the sx prop for consistency with the other branches. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
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 }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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 }} | ||
/> | ||
|
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 }); | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix regression, it was removed in #29974. |
||
}, | ||
"dependencies": { | ||
"@emotion/cache": "latest", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -19,7 +19,6 @@ export default function MyApp(props: MyAppProps) { | |
return ( | ||
<CacheProvider value={emotionCache}> | ||
<Head> | ||
<title>Change title in _app.tsx</title> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}> | ||
|
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() { | ||
|
@@ -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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, it should fix the issue in #29648. Thanks! |
||
</Head> | ||
<body> | ||
<Main /> | ||
|
@@ -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, | ||
}; | ||
}; |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
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 }); | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
reactStrictMode: true, | ||
}; | ||
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
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.