Last active
April 14, 2016 09:02
-
-
Save junkycoder/9a37c5076b8d360155d630fdb9afd507 to your computer and use it in GitHub Desktop.
Material-UI's styleResizable mixin as a decorator
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
{ | |
"presets": ["es2015", "react"], | |
"plugins": [ | |
"transform-class-properties", | |
"transform-function-bind", | |
"transform-runtime", | |
"transform-object-rest-spread" | |
] | |
} |
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
import React, { Component } from 'react'; | |
import Events from 'material-ui/lib/utils/events'; | |
const Sizes = { | |
SMALL: 1, | |
MEDIUM: 2, | |
LARGE: 3, | |
}; | |
function deviceSize(ChildComponent) { | |
class DeviceSize extends Component { | |
state = { | |
deviceSize: Sizes.SMALL | |
} | |
componentDidMount() { | |
this.updateDeviceSize(); | |
this.bindResize(); | |
} | |
componentWillUnmount() { | |
this.unbindResize(); | |
} | |
isDeviceSize(desiredSize) { | |
return this.state.deviceSize >= desiredSize; | |
} | |
updateDeviceSize() { | |
const width = window.innerWidth; | |
if (width >= 992) { | |
this.setState({deviceSize: Sizes.LARGE}); | |
} else if (width >= 768) { | |
this.setState({deviceSize: Sizes.MEDIUM}); | |
} else { // width < 768 | |
this.setState({deviceSize: Sizes.SMALL}); | |
} | |
} | |
bindResize() { | |
Events.on(window, 'resize', ::this.updateDeviceSize); | |
} | |
unbindResize() { | |
Events.off(window, 'resize', ::this.updateDeviceSize); | |
} | |
render() { | |
return ( | |
<ChildComponent | |
{...this.props} | |
isSmallDevice={this.state.deviceSize === Sizes.SMALL} | |
isMediumDevice={this.state.deviceSize === Sizes.MEDIUM} | |
isLargeDevice={this.state.deviceSize === Sizes.LARGE} | |
/> | |
); | |
} | |
} | |
return DeviceSize; | |
} | |
export default deviceSize; |
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
import React, { Component } from 'react'; | |
import deviceSize from './deviceSize'; | |
const styles = { | |
content: { | |
margin: '48px 72px' | |
}, | |
contentSmall: { | |
margin: 24 | |
}, | |
}; | |
const m = (...s) => Object.assign({}, ...s); | |
class Master extends Component { | |
render() { | |
return ( | |
<div> | |
<div style={m( | |
styles.content, | |
this.props.isSmallDevice && styles.contentSmall | |
)}> | |
{this.props.children} | |
</div> | |
</div> | |
); | |
} | |
} | |
export default deviceSize(Master); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original mixin