mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
examples(material-ui): change the wrapping strategy (#2648)
By creating an intermediary component (with the HOC) we save some React rendering logic. That's also addressing an issue with JssProvider (react-jss) that generate a new index at each render.
This commit is contained in:
parent
051ee314e9
commit
58c2d138d0
|
@ -1,53 +0,0 @@
|
||||||
import React, { Component } from 'react'
|
|
||||||
import PropTypes from 'prop-types'
|
|
||||||
import { JssProvider } from 'react-jss'
|
|
||||||
import { withStyles, createStyleSheet, MuiThemeProvider } from 'material-ui/styles'
|
|
||||||
import { getContext } from '../styles/context'
|
|
||||||
|
|
||||||
// Apply some reset
|
|
||||||
const styleSheet = createStyleSheet('App', 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)
|
|
||||||
|
|
||||||
class App extends Component {
|
|
||||||
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>
|
|
||||||
{this.props.children}
|
|
||||||
</AppWrapper>
|
|
||||||
</MuiThemeProvider>
|
|
||||||
</JssProvider>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
App.propTypes = {
|
|
||||||
children: PropTypes.node.isRequired
|
|
||||||
}
|
|
||||||
|
|
||||||
export default App
|
|
62
examples/with-material-ui-next/components/withRoot.js
Normal file
62
examples/with-material-ui-next/components/withRoot.js
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
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 />
|
||||||
|
</AppWrapper>
|
||||||
|
</MuiThemeProvider>
|
||||||
|
</JssProvider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WithRoot.displayName = `withRoot(${BaseComponent.displayName})`
|
||||||
|
|
||||||
|
return WithRoot
|
||||||
|
}
|
||||||
|
|
||||||
|
export default withRoot
|
|
@ -7,7 +7,7 @@ import Dialog, {
|
||||||
DialogActions
|
DialogActions
|
||||||
} from 'material-ui/Dialog'
|
} from 'material-ui/Dialog'
|
||||||
import Typography from 'material-ui/Typography'
|
import Typography from 'material-ui/Typography'
|
||||||
import App from '../components/App'
|
import withRoot from '../components/withRoot'
|
||||||
|
|
||||||
const styles = {
|
const styles = {
|
||||||
container: {
|
container: {
|
||||||
|
@ -35,7 +35,6 @@ class Index extends Component {
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
return (
|
return (
|
||||||
<App>
|
|
||||||
<div style={styles.container}>
|
<div style={styles.container}>
|
||||||
<Dialog open={this.state.open} onRequestClose={this.handleRequestClose}>
|
<Dialog open={this.state.open} onRequestClose={this.handleRequestClose}>
|
||||||
<DialogTitle>Super Secret Password</DialogTitle>
|
<DialogTitle>Super Secret Password</DialogTitle>
|
||||||
|
@ -52,9 +51,8 @@ class Index extends Component {
|
||||||
Super Secret Password
|
Super Secret Password
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</App>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Index
|
export default withRoot(Index)
|
||||||
|
|
Loading…
Reference in a new issue