Last active
November 12, 2015 22:56
-
-
Save dralletje/19fed5ce23590ed3cfc1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| '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