Created
February 21, 2019 22:34
-
-
Save thclark/df7c424c9fb9ff6ee0f5fea81b072493 to your computer and use it in GitHub Desktop.
React HOC that places children in a div the size of the remaining window space. Updates on window resize. More info at https://stackoverflow.com/a/54112855/3556110
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 PropTypes from 'prop-types' | |
class FullArea extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
width: 0, | |
height: 0, | |
} | |
this.getStyles = this.getStyles.bind(this) | |
this.updateWindowDimensions = this.updateWindowDimensions.bind(this) | |
} | |
componentDidMount() { | |
this.updateWindowDimensions() | |
window.addEventListener('resize', this.updateWindowDimensions) | |
} | |
componentWillUnmount() { | |
window.removeEventListener('resize', this.updateWindowDimensions) | |
} | |
getStyles(vertical, horizontal) { | |
const styles = {} | |
if (vertical) { | |
styles.height = `${this.state.height}px` | |
} | |
if (horizontal) { | |
styles.width = `${this.state.width}px` | |
} | |
return styles | |
} | |
updateWindowDimensions() { | |
this.setState({ width: window.innerWidth, height: window.innerHeight }) | |
} | |
render() { | |
const { vertical, horizontal } = this.props | |
return ( | |
<div style={this.getStyles(vertical, horizontal)} > | |
{this.props.children} | |
</div> | |
) | |
} | |
} | |
FullArea.defaultProps = { | |
horizontal: false, | |
vertical: false, | |
} | |
FullArea.propTypes = { | |
horizontal: PropTypes.bool, | |
vertical: PropTypes.bool, | |
} | |
export default FullArea |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment