File tree 2 files changed +102
-0
lines changed
2 files changed +102
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments