2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-03-25 03:08:43 +00:00
|
|
|
import { getWindowDimension } from '../../../utils/is_mobile'
|
|
|
|
|
|
|
|
const initialState = getWindowDimension()
|
|
|
|
|
2020-08-17 23:06:22 +01:00
|
|
|
class Responsive extends React.PureComponent {
|
2020-03-25 03:08:43 +00:00
|
|
|
|
|
|
|
state = {
|
|
|
|
width: initialState.width,
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-05-06 05:33:54 +01:00
|
|
|
this.handleResize()
|
2020-03-25 03:08:43 +00:00
|
|
|
window.addEventListener('resize', this.handleResize, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.handleResize, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
handleResize = () => {
|
|
|
|
const { width } = getWindowDimension()
|
|
|
|
|
|
|
|
this.setState({ width })
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldRender = (min, max, width) => {
|
|
|
|
return width > min && width < max
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { children, min, max } = this.props
|
|
|
|
const { width } = this.state
|
|
|
|
|
|
|
|
const shouldRender = this.shouldRender(min, max, width)
|
|
|
|
|
|
|
|
return shouldRender ? children : null
|
|
|
|
}
|
2020-05-06 05:33:54 +01:00
|
|
|
|
2020-08-17 23:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Responsive.propTypes = {
|
|
|
|
min: PropTypes.number,
|
|
|
|
max: PropTypes.number,
|
|
|
|
}
|
|
|
|
|
|
|
|
Responsive.defaultProps = {
|
|
|
|
min: 0,
|
|
|
|
max: Infinity,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Responsive
|