Created
July 19, 2017 11:54
-
-
Save selbekk/cb44976033b85140473e03870a7d3007 to your computer and use it in GitHub Desktop.
HOC for fetching breakpoint
This file contains 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
import React, { Component } from 'react'; | |
import debounce from 'lodash.debounce'; | |
const withBreakpoint = TargetComponent => class extends Component { | |
state = { | |
breakpoint: this.getBreakpoint(), | |
}; | |
componentDidMount() { | |
window.addEventListener('resize', this.updateBreakpoint); | |
} | |
componentWillUnmount() { | |
window.removeEventListener('resize', this.updateBreakpoint); | |
} | |
getBreakpoint() { | |
return window.getComputedStyle(document.body, ':before') | |
.getPropertyValue('content').replace(/"/g, ''); | |
}; | |
updateBreakpoint = debounce(() => | |
this.setState({ breakpoint: this.getBreakpoint() }) | |
, 100); | |
render() { | |
return ( | |
<TargetComponent {...this.props} {...this.state} /> | |
); | |
} | |
}; | |
export default withBreakpoint; | |
export const SMALL = 'small'; | |
export const MEDIUM = 'medium'; | |
export const LARGE = 'large'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment