Skip to content

Commit bb808a1

Browse files
committedMay 28, 2018
material-ui step 2
1 parent efb4afb commit bb808a1

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
 

‎libs/getPageContext.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* eslint-disable no-underscore-dangle */
2+
3+
import { SheetsRegistry } from 'jss';
4+
import { createMuiTheme, createGenerateClassName } from '@material-ui/core/styles';
5+
import purple from '@material-ui/core/colors/purple';
6+
import green from '@material-ui/core/colors/green';
7+
8+
// A theme with custom primary and secondary color.
9+
// It's optional.
10+
const theme = createMuiTheme({
11+
palette: {
12+
primary: {
13+
light: purple[300],
14+
main: purple[500],
15+
dark: purple[700],
16+
},
17+
secondary: {
18+
light: green[300],
19+
main: green[500],
20+
dark: green[700],
21+
},
22+
},
23+
});
24+
25+
function createPageContext() {
26+
return {
27+
theme,
28+
// This is needed in order to deduplicate the injection of CSS in the page.
29+
sheetsManager: new Map(),
30+
// This is needed in order to inject the critical CSS.
31+
sheetsRegistry: new SheetsRegistry(),
32+
// The standard class name generator.
33+
generateClassName: createGenerateClassName(),
34+
};
35+
}
36+
37+
export default function getPageContext() {
38+
// Make sure to create a new context for every server-side request so that data
39+
// isn't shared between connections (which would be bad).
40+
if (!process.browser) {
41+
return createPageContext();
42+
}
43+
44+
// Reuse context on the client-side.
45+
if (!global.__INIT_MATERIAL_UI__) {
46+
global.__INIT_MATERIAL_UI__ = createPageContext();
47+
}
48+
49+
return global.__INIT_MATERIAL_UI__;
50+
}

‎libs/withRoot.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { MuiThemeProvider } from '@material-ui/core/styles';
4+
import CssBaseline from '@material-ui/core/CssBaseline';
5+
import getPageContext from './getPageContext';
6+
7+
function withRoot(Component) {
8+
class WithRoot extends React.Component {
9+
constructor(props, context) {
10+
super(props, context);
11+
12+
this.pageContext = this.props.pageContext || getPageContext();
13+
}
14+
15+
componentDidMount() {
16+
// Remove the server-side injected CSS.
17+
const jssStyles = document.querySelector('#jss-server-side');
18+
if (jssStyles && jssStyles.parentNode) {
19+
jssStyles.parentNode.removeChild(jssStyles);
20+
}
21+
}
22+
23+
pageContext = null;
24+
25+
render() {
26+
// MuiThemeProvider makes the theme available down the React tree thanks to React context.
27+
return (
28+
<MuiThemeProvider theme={this.pageContext.theme} sheetsManager={this.pageContext.sheetsManager}>
29+
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
30+
<CssBaseline />
31+
<Component {...this.props} />
32+
</MuiThemeProvider>
33+
);
34+
}
35+
}
36+
37+
WithRoot.propTypes = {
38+
pageContext: PropTypes.object,
39+
};
40+
41+
WithRoot.getInitialProps = ctx => {
42+
if (Component.getInitialProps) {
43+
return Component.getInitialProps(ctx);
44+
}
45+
46+
return {};
47+
};
48+
49+
return WithRoot;
50+
}
51+
52+
export default withRoot;

0 commit comments

Comments
 (0)
Please sign in to comment.