Skip to content

Instantly share code, notes, and snippets.

@dralletje
Last active November 12, 2015 22:56
Show Gist options
  • Select an option

  • Save dralletje/19fed5ce23590ed3cfc1 to your computer and use it in GitHub Desktop.

Select an option

Save dralletje/19fed5ce23590ed3cfc1 to your computer and use it in GitHub Desktop.
'use strict';
import React from 'react'
import Events from '../utils/events'
const Sizes = {
SMALL: 1,
MEDIUM: 2,
LARGE: 3
};
const DeviceSizeComponent = WrapComponent =>
class DeviceWidth extends React.Component {
constructor(props) {
this.state = {
deviceSize: Sizes.SMALL,
};
}
componentDidMount() {
const updateDeviceSize = () => {
var width = window.innerWidth;
if (width >= 992) {
this.setState({ deviceSize: Sizes.LARGE });
} else if (width >= 768) {
this.setState({ deviceSize: Sizes.MEDIUM });
} else {
this.setState({ deviceSize: Sizes.SMALL }); // width < 768
}
}
updateDeviceSize();
window.addEventListener('resize', updateDeviceSize);
this.unbind = () => {
window.removeEventListener('resize', updateDeviceSize)
}
}
componentWillUnmount() {
this.unbind()
}
render() {
const {deviceSize} = this.state
const isDeviceSize = desiredSize => {
return deviceSize >= desiredSize;
}
return <WrapComponent deviceSize={deviceSize} isDeviceSize={isDeviceSize} />
}
};
DeviceSizeComponent.Sizes = Size
export default DeviceSizeComponent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment