-
Notifications
You must be signed in to change notification settings - Fork 0
/
isMobileDecorator.jsx
executable file
·50 lines (43 loc) · 1.24 KB
/
isMobileDecorator.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React, { Component } from 'react';
export default function isMobile(breakpoint) {
const DEFAULT_BREAKPOINT = 768;
let resizeTimeout;
return function (WrappedComponent) {
class IsMobile extends Component {
state = {
isMobile: true,
};
componentDidMount() {
if (typeof(window) === 'object') {
window.addEventListener('resize', this.throttleResize, false);
}
this.actualResizeHandler(true);
}
componentWillUnmount() {
if (typeof(window) === 'object') {
window.removeEventListener('resize', this.throttleResize, false);
}
}
actualResizeHandler = (init) => {
const newIsMobile = window.innerWidth < (breakpoint || DEFAULT_BREAKPOINT);
if (init || this.state.isMobile !== newIsMobile) {
this.setState({
isMobile: newIsMobile,
});
}
}
throttleResize = () => {
if (!resizeTimeout) {
resizeTimeout = setTimeout(() => {
resizeTimeout = null;
this.actualResizeHandler();
}, 66);
}
};
render() {
return <WrappedComponent {...this.props} {...this.state} />;
}
}
return IsMobile;
};
}