Skip to content

Instantly share code, notes, and snippets.

@selbekk
Created July 19, 2017 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selbekk/cb44976033b85140473e03870a7d3007 to your computer and use it in GitHub Desktop.
Save selbekk/cb44976033b85140473e03870a7d3007 to your computer and use it in GitHub Desktop.
HOC for fetching breakpoint
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