This repository was archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathindex.js
66 lines (54 loc) · 1.61 KB
/
index.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { createElement, Component } from 'react'
import getDisplayName from 'react-display-name'
const isOnlineStatusSupported = global.navigator && typeof global.navigator.onLine !== 'undefined'
const withOnlineStatusProps = (mapStatusToProps) => (Target) => {
if (!isOnlineStatusSupported) {
return Target
}
class WithOnlineStatusProps extends Component {
constructor (props, context) {
super(props, context)
this.state = mapStatusToProps({
isOnline: global.navigator.onLine,
isOffline: !global.navigator.onLine
})
this.handleOnline = this.handleOnline.bind(this)
this.handleOffline = this.handleOffline.bind(this)
}
componentDidMount () {
global.addEventListener('online', this.handleOnline, false)
global.addEventListener('offline', this.handleOffline, false)
}
componentWillUnmount () {
global.removeEventListener('online', this.handleOnline)
global.removeEventListener('offline', this.handleOffline)
}
handleOnline () {
this.setState(
mapStatusToProps({
isOnline: true,
isOffline: false
})
)
}
handleOffline () {
this.setState(
mapStatusToProps({
isOnline: false,
isOffline: true
})
)
}
render () {
return createElement(Target, {
...this.props,
...this.state
})
}
}
if (process.env.NODE_ENV !== 'production') {
WithOnlineStatusProps.displayName = `withOnlineStatusProps(${getDisplayName(Target)})`
}
return WithOnlineStatusProps
}
export default withOnlineStatusProps