forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Take ownership on the next.js example (mui#7703)
- Loading branch information
1 parent
dba3ef6
commit 3d7df36
Showing
15 changed files
with
347 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,21 @@ | ||
# Create React App example | ||
|
||
This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app). | ||
## How to use | ||
|
||
Download the example [or clone the repo](https://github.com/callemall/material-ui): | ||
|
||
```bash | ||
curl https://codeload.github.com/callemall/material-ui/tar.gz/v1-beta | tar -xz --strip=2 next.js-master/examples/create-react-app | ||
cd create-react-app | ||
``` | ||
|
||
Install it and run: | ||
|
||
```bash | ||
npm install | ||
npm run start | ||
``` | ||
|
||
## The idea behind the example | ||
|
||
[Create React App](https://github.com/facebookincubator/create-react-app) with no build configuration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
[](https://deploy.now.sh/?repo=https://github.com/callemall/material-ui/tree/v1-beta/examples/nextjs) | ||
# Next.js example | ||
|
||
The example is [hosted on their repository](https://github.com/zeit/next.js/tree/v3-beta/examples/with-material-ui-next). | ||
## How to use | ||
|
||
Download the example [or clone the repo](https://github.com/callemall/material-ui): | ||
|
||
```bash | ||
curl https://codeload.github.com/callemall/material-ui/tar.gz/v1-beta | tar -xz --strip=2 next.js-master/examples/nextjs | ||
cd nextjs | ||
``` | ||
|
||
Install it and run: | ||
|
||
```bash | ||
npm install | ||
npm run dev | ||
``` | ||
|
||
## The idea behind the example | ||
|
||
[Next.js](https://github.com/zeit/next.js) is a framework for server-rendered React apps. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @flow weak | ||
|
||
import React, { Component } from 'react'; | ||
import { JssProvider } from 'react-jss'; | ||
import { withStyles, createStyleSheet, MuiThemeProvider } from 'material-ui/styles'; | ||
import { getContext, setContext } from '../styles/context'; | ||
|
||
// Apply some reset | ||
const styleSheet = createStyleSheet(theme => ({ | ||
'@global': { | ||
html: { | ||
background: theme.palette.background.default, | ||
WebkitFontSmoothing: 'antialiased', // Antialiasing. | ||
MozOsxFontSmoothing: 'grayscale', // Antialiasing. | ||
}, | ||
body: { | ||
margin: 0, | ||
}, | ||
}, | ||
})); | ||
|
||
let AppWrapper = props => props.children; | ||
|
||
AppWrapper = withStyles(styleSheet)(AppWrapper); | ||
|
||
function withRoot(BaseComponent) { | ||
class WithRoot extends Component { | ||
static getInitialProps(ctx) { | ||
// Reset the context for handling a new request. | ||
setContext(); | ||
|
||
if (BaseComponent.getInitialProps) { | ||
return BaseComponent.getInitialProps(ctx); | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
componentDidMount() { | ||
// Remove the server-side injected CSS. | ||
const jssStyles = document.querySelector('#jss-server-side'); | ||
if (jssStyles && jssStyles.parentNode) { | ||
jssStyles.parentNode.removeChild(jssStyles); | ||
} | ||
} | ||
|
||
render() { | ||
const context = getContext(); | ||
|
||
if (process.browser) { | ||
return ( | ||
<MuiThemeProvider theme={context.theme}> | ||
<AppWrapper> | ||
<BaseComponent {...this.props} /> | ||
</AppWrapper> | ||
</MuiThemeProvider> | ||
); | ||
} | ||
|
||
return ( | ||
<JssProvider registry={context.sheetsRegistry} jss={context.jss}> | ||
<MuiThemeProvider theme={context.theme} sheetsManager={context.sheetsManager}> | ||
<AppWrapper> | ||
<BaseComponent {...this.props} /> | ||
</AppWrapper> | ||
</MuiThemeProvider> | ||
</JssProvider> | ||
); | ||
} | ||
} | ||
|
||
WithRoot.displayName = `withRoot(${BaseComponent.displayName})`; | ||
|
||
return WithRoot; | ||
} | ||
|
||
export default withRoot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "next.js", | ||
"version": "1.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"material-ui": "next", | ||
"next": "latest", | ||
"react": "latest", | ||
"react-dom": "latest" | ||
}, | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* eslint-disable flowtype/require-valid-file-annotation */ | ||
|
||
import React from 'react'; | ||
import Document, { Head, Main, NextScript } from 'next/document'; | ||
import { getContext } from '../styles/context'; | ||
|
||
export default class MyDocument extends Document { | ||
static getInitialProps(ctx) { | ||
const page = ctx.renderPage(); | ||
// Get the context with the collected side effects. | ||
const context = getContext(); | ||
return { | ||
...page, | ||
styles: ( | ||
<style | ||
id="jss-server-side" | ||
// eslint-disable-next-line react/no-danger | ||
dangerouslySetInnerHTML={{ __html: context.sheetsRegistry.toString() }} | ||
/> | ||
), | ||
}; | ||
} | ||
|
||
render() { | ||
const context = getContext(); | ||
return ( | ||
<html lang="en"> | ||
<Head> | ||
<title>My page</title> | ||
<meta charSet="utf-8" /> | ||
{/* Use minimum-scale=1 to enable GPU rasterization */} | ||
<meta | ||
name="viewport" | ||
content={ | ||
'user-scalable=0, initial-scale=1, ' + | ||
'minimum-scale=1, width=device-width, height=device-height' | ||
} | ||
/> | ||
{/* | ||
manifest.json provides metadata used when your web app is added to the | ||
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/ | ||
*/} | ||
<link rel="manifest" href="/static/manifest.json" /> | ||
{/* PWA primary color */} | ||
<meta name="theme-color" content={context.theme.palette.primary[500]} /> | ||
<link | ||
rel="stylesheet" | ||
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" | ||
/> | ||
</Head> | ||
<body> | ||
<Main /> | ||
<NextScript /> | ||
</body> | ||
</html> | ||
); | ||
} | ||
} |
Oops, something went wrong.