1.0.0 • Published 6 months ago

network-connectivity v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Network Connectivity

npm version License

A lightweight package for detecting and monitoring network connectivity in web applications.

Getting Started

Network Connectivity

Install the package:

Using npm:

$ npm install --save network-connectivity

Using Yarn:

$ yarn add network-connectivity

API Reference

ConnectionBanner Component

The ConnectionBanner component is designed to display online and offline status banners with customizable content. It accepts the following props:

Props

Prop NameTypeDefault ValueDescription
alwaysShowBannerbooleanfalseIf set to true, the banner will always be displayed, regardless of the online/offline status.
hideOnlineBannerDelaynumber2000The delay in milliseconds before hiding the online banner after a successful connection is detected. This delay applies when alwaysShowBanner is set to false
onlineBannerContentReact.ReactElement or string'Back online'The content to display in the online status banner. It can be a React element or a string.
offlineBannerContentReact.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'.
withPortalbooleantrueIf set to true, the ConnectionBanner will use a portal to render the banner.
classNamestringAdditional CSS class names to apply to the ConnectionBanner component.

CSS Class Names

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 the ConnectionBanner component.
  • connection-banner-online: Applied when the device is online.
  • connection-banner-offline: Applied when the device is offline.

Example Usage

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>
  )
}

useConnection Hook

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.

Example Usage

import { useConnection } from 'network-connectivity';

const MyComponent = () => {
  const { isOnline } = useConnection();

  return (
    <div>
      <p>Is online: {isOnline ? 'Yes' : 'No'}</p>
    </div>
  );
}

isOnline Function

The isOnline function is a utility function provided by the network-connectivity package. It allows you to programmatically check the network connectivity status.

Example Usage

import { isOnline } from 'network-connectivity';

if (isOnline()) {
  console.log('You are online!');
} else {
  console.log('You are offline.');
}
1.0.0

6 months ago

0.1.4

6 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.3

12 months ago

0.1.0

12 months ago