Skip to content

Instantly share code, notes, and snippets.

@srph
Created June 20, 2019 10:04
Show Gist options
  • Save srph/eec70a007d2b1df0f483d29b0ab5fbff to your computer and use it in GitHub Desktop.
Save srph/eec70a007d2b1df0f483d29b0ab5fbff to your computer and use it in GitHub Desktop.
React: 100vh css variable workaround for mobile (https://css-tricks.com/the-trick-to-viewport-units-on-mobile/)
import { useEffect } from 'react'
import useWindowSize from 'react-use/lib/useWindowSize'
/**
* @NOTE Don't mount this twice; only on the root node.
*/
function WindowVhSetter() {
const {height} = useWindowSize()
useEffect(() => {
document.body.style.setProperty('--window-vh', `${height}px`)
}, [height])
return null
}
export default WindowVhSetter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment