Skip to content

Instantly share code, notes, and snippets.

@basilleaf
Last active August 13, 2019 23:54
Show Gist options
  • Save basilleaf/ff483f4a936d0d82df463b2cdaf33295 to your computer and use it in GitHub Desktop.
Save basilleaf/ff483f4a936d0d82df463b2cdaf33295 to your computer and use it in GitHub Desktop.
adjust window height to match mobile safari visible viewport height in React
# height adjust for mobile safari
# for when you sometimes need the window height to be the actual visible viewport
export default class MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
height_adjust: 0,
}
}
componentDidMount() {
if (!this.state.height_adjust === 0) {
const is_portrait = window.innerHeight > window.innerWidth
const is_min_height = window.innerHeight > 520
if (is_portrait && is_min_height && window.innerHeight < this.container.getBoundingClientRect().height) {
this.setState({ height_adjust: window.innerHeight })
}
}
}
mobile_window_height_adjust() {
if (!this.state.height_adjust) return
return {
height: this.state.height_adjust + "px",
}
}
render() {
return (
<div style={this.mobile_window_height_adjust()}>
...my stuff
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment