-
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.
- Loading branch information
Alex Alexeev
committed
Aug 1, 2017
1 parent
d19d998
commit 738a654
Showing
9 changed files
with
501 additions
and
26 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
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,69 @@ | ||
import React, { Component } from 'react'; | ||
import { JssProvider } from 'react-jss'; | ||
import { | ||
withStyles, | ||
createStyleSheet, | ||
MuiThemeProvider | ||
} from 'material-ui/styles'; | ||
import { getContext } 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) { | ||
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(); | ||
|
||
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
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,57 @@ | ||
import React from 'react'; | ||
import Document, { Head, Main, NextScript } from 'next/document'; | ||
import { getContext, setContext } from '../styles/context'; | ||
|
||
export default class MyDocument extends Document { | ||
static async getInitialProps(ctx) { | ||
// Reset the context for handling a new request. | ||
setContext(); | ||
const page = ctx.renderPage(); | ||
// Get the context with the collected side effects. | ||
const context = getContext(); | ||
return { | ||
...page, | ||
styles: ( | ||
<style | ||
id="jss-server-side" | ||
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, maximum-scale=1, ' + | ||
'minimum-scale=1, width=device-width, height=device-height' | ||
} | ||
/> | ||
{/* 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> | ||
); | ||
} | ||
} |
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,6 @@ | ||
import App from '../components/App'; | ||
import withRoot from '../components/withRoot'; | ||
|
||
export default () => <App />; | ||
const Index = () => <App />; | ||
|
||
export default withRoot(Index); |
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,49 @@ | ||
import { create } from 'jss'; | ||
import preset from 'jss-preset-default'; | ||
import { SheetsRegistry } from 'react-jss'; | ||
import createPalette from 'material-ui/styles/palette'; | ||
import createMuiTheme from 'material-ui/styles/theme'; | ||
import { purple, green } from 'material-ui/colors'; | ||
import createGenerateClassName from 'material-ui/styles/createGenerateClassName'; | ||
|
||
const theme = createMuiTheme({ | ||
palette: createPalette({ | ||
primary: purple, | ||
accent: green | ||
}) | ||
}); | ||
|
||
// Configure JSS | ||
const jss = create(preset()); | ||
jss.options.createGenerateClassName = createGenerateClassName; | ||
|
||
function createContext() { | ||
return { | ||
jss, | ||
theme, | ||
// This is needed in order to deduplicate the injection of CSS in the page. | ||
sheetsManager: new WeakMap(), | ||
// This is needed in order to inject the critical CSS. | ||
sheetsRegistry: new SheetsRegistry() | ||
}; | ||
} | ||
|
||
export function setContext() { | ||
// Singleton hack as there is no way to pass variables from _document.js to pages yet. | ||
global.__INIT_MATERIAL_UI__ = createContext(); | ||
} | ||
|
||
export function getContext() { | ||
// Make sure to create a new store for every server-side request so that data | ||
// isn't shared between connections (which would be bad) | ||
if (!process.browser) { | ||
return global.__INIT_MATERIAL_UI__; | ||
} | ||
|
||
// Reuse context on the client-side | ||
if (!global.__INIT_MATERIAL_UI__) { | ||
global.__INIT_MATERIAL_UI__ = createContext(); | ||
} | ||
|
||
return global.__INIT_MATERIAL_UI__; | ||
} |
Oops, something went wrong.