Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #615 from gufranmirza/network-status-indicator
Network status indicator
- Loading branch information
Showing
4 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// @flow | ||
import * as React from 'react'; | ||
import * as styles from './styles'; | ||
|
||
type Props = {}; | ||
type State = { | ||
online: boolean | ||
}; | ||
|
||
class NetworkStatus extends React.Component<Props, State> { | ||
state = { | ||
online: true | ||
}; | ||
|
||
componentDidMount() { | ||
if (typeof window !== 'undefined') { | ||
window.addEventListener('online', this.updateOnlineStatus); | ||
window.addEventListener('offline', this.updateOnlineStatus); | ||
} | ||
} | ||
|
||
updateOnlineStatus = () => { | ||
const condition = navigator.onLine ? 'online' : 'offline'; | ||
if (condition === 'offline') { | ||
this.setState({ online: false }); | ||
} else if (condition === 'online') { | ||
this.setState({ online: true }); | ||
} | ||
}; | ||
|
||
render() { | ||
return ( | ||
<div> | ||
{!this.state.online && ( | ||
<styles.Root> | ||
<styles.Label> Offline </styles.Label> | ||
</styles.Root> | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default NetworkStatus; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import styled from 'styled-components'; | ||
|
||
const Root = styled.div` | ||
position: fixed; | ||
left: 30px; | ||
bottom: 30px; | ||
`; | ||
|
||
const Label = styled.div` | ||
background: red; | ||
color: #fff; | ||
font-weight: bold; | ||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5); | ||
padding: 10px; | ||
`; | ||
|
||
export { Root, Label }; |
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
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