Skip to content

Instantly share code, notes, and snippets.

@jurgob
Last active August 30, 2019 15:30
Show Gist options
  • Save jurgob/abb64c1e4832b0a7fcab73f67218de78 to your computer and use it in GitHub Desktop.
Save jurgob/abb64c1e4832b0a7fcab73f67218de78 to your computer and use it in GitHub Desktop.
import React from 'react'
function defaultGetWidth () {
return window.innerWidth;
}
function defaultGetHeight () {
return window.innerHeight;
}
export function withWindowDimensions({ getHeight, getWidth } = {}) {
return (ComposedComponent) => {
if(!getHeight) getHeight = defaultGetHeight;
if(!getWidth) getWidth = defaultGetWidth;
return class DimensionsHOC extends React.Component {
constructor(props){
super(props)
this.state = {}
this.onResize = this.onResize.bind(this);
this.updateDimensions = this.updateDimensions.bind(this);
}
updateDimensions(){
this.setState({
windowWidth: getWidth(),
windowHeight: getHeight()
})
}
onResize (){
if (this.rqf) return
if( typeof window !== 'undefined' )
this.rqf = window.requestAnimationFrame(() => {
this.rqf = null
this.updateDimensions()
})
}
componentDidMount () {
this.updateDimensions()
if( typeof window !== 'undefined' )
window.addEventListener('resize', this.onResize, false)
}
componentWillUnmount () {
if( typeof window !== 'undefined' )
window.removeEventListener('resize', this.onResize)
}
render () {
return (
<span>
{(this.state.windowWidth || this.state.windowHeight) &&
<ComposedComponent {...this.state} {...this.props} updateDimensions={this.updateDimensions}/>}
</span>
)
}
}
}
}
export const withWindowDimensionsDOM = withWindowDimensions();
/**
* example:
import {withWindowDimensionsDOM} from '../components/withWindowDimensions';
const MyComponent = ({windowWidth,windowHeight }) => <div> windows dimensions w: {windowWidth} , h: {windowHeight} </div>
const MyComponentDecorated = withWindowDimensionsDOM(MyComponent);
<MyComponentDecorated />
* example 2 (usefull for SSR):
import {withWindowDimensions} from '../components/withWindowDimensions';
const MyComponent = ({windowWidth,windowHeight }) => <div> windows dimensions setted w: {windowWidth} , h: {windowHeight} </div>
const MyComponentDecorated = withWindowDimensions({
getWidth: ()=> 1980,
getHeight: ()=> 1024
})(MyComponent);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment