A lightweight package for detecting and monitoring network connectivity in web applications.
Using npm:
$ npm install --save network-connectivity
Using Yarn:
$ yarn add network-connectivity
The ConnectionBanner
component is designed to display online and offline status banners with customizable content. It accepts the following props:
Prop Name | Type | Default Value | Description |
---|---|---|---|
alwaysShowBanner |
boolean |
false |
If set to true , the banner will always be displayed, regardless of the online/offline status. |
hideOnlineBannerDelay |
number |
2000 |
The delay in milliseconds before hiding the online banner after a successful connection is detected. This delay applies when alwaysShowBanner is set to false |
onlineBannerContent |
React.ReactElement or string |
'Back online' |
The content to display in the online status banner. It can be a React element or a string. |
offlineBannerContent |
React.ReactElement or string |
'No connection' |
The content to display in the offline status banner. It can be a React element or a string. |
placement |
'top' or 'bottom' |
'top' |
The placement of the online status banner within the application UI. Can be set to 'top' or 'bottom' . |
withPortal |
boolean |
true |
If set to true , the ConnectionBanner will use a portal to render the banner. |
className |
string |
Additional CSS class names to apply to the ConnectionBanner component. |
The ConnectionBanner
component supports custom styling through CSS class names. You can use the className
prop to apply additional class names to the component. The component itself applies the following class names:
connection-banner
: The base class name for theConnectionBanner
component.connection-banner-online
: Applied when the device is online.connection-banner-offline
: Applied when the device is offline.
Here's an example of how you can use the ConnectionBanner
component with some custom props:
import { ConnectionBanner } from 'network-connectivity';
const MyComponent = () => {
return (
<div>
<ConnectionBanner alwaysShowBanner={true} />
</div>
)
}
The useConnection
hook allows you to access the network connectivity status within your React components. It returns an object with the following properties:
isOnline
: A boolean value indicating whether the device is currently online.
import { useConnection } from 'network-connectivity';
const MyComponent = () => {
const { isOnline } = useConnection();
return (
<div>
<p>Is online: {isOnline ? 'Yes' : 'No'}</p>
</div>
);
}
The isOnline
function is a utility function provided by the network-connectivity
package. It allows you to programmatically check the network connectivity status.
import { isOnline } from 'network-connectivity';
if (isOnline()) {
console.log('You are online!');
} else {
console.log('You are offline.');
}