npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-hooks-simplified

v1.0.3

Published

A collection of React hooks for interacting with various browser APIs. This library simplifies the use of modern browser features in your React applications by providing easy-to-use hooks.

Downloads

7

Readme

React Browser Hooks 📦

A collection of React hooks for interacting with various browser APIs. This library simplifies the use of modern browser features in your React applications by providing easy-to-use hooks.

Installation

You can install the package via npm:

npm install react-hooks-simplified


## Available Hooks

- [`useBattery`](#usebattery)
- [`useGeolocation`](#usegeolocation)
- [`useClipboard`](#useclipboard)
- [`useNetwork`](#usenetwork)
- [`useMediaDevices`](#usemediadevices)
- [`useFullscreen`](#usefullscreen)
- [`useSpeechSynthesis`](#usespeechsynthesis)
- [`useLocalStorage`](#uselocalstorage)
- [`useIntersectionObserver`](#useintersectionobserver)
- [`useIdle`](#useidle)
- [`useNotifications`](#usenotifications)
- [`useWindowSize`](#usewindowsize)
- [`useDarkMode`](#usedarkmode)
- [`useMediaQuery`](#usemediaquery)

## `useBattery` Hook

The `useBattery` hook provides information about the battery status of the device.

### Example

```jsx
import { useBattery } from 'react-hooks-simplified';

const BatteryStatus = () => {
  const { battery, charging, level } = useBattery();

  return (
    <div>
      <p>Battery Level: {level * 100}%</p>
      <p>Charging: {charging ? 'Yes' : 'No'}</p>
    </div>
  );
};

useGeolocation Hook

The useGeolocation hook provides information about the user's geographical location.

Example

import { useGeolocation } from 'react-hooks-simplified';

const LocationDisplay = () => {
  const { position, error } = useGeolocation();

  if (error) {
    return <p>Error: {error}</p>;
  }

  return (
    <div>
      {position ? (
        <p>
          Latitude: {position.latitude}, Longitude: {position.longitude}
        </p>
      ) : (
        <p>Fetching location...</p>
      )}
    </div>
  );
};

useClipboard Hook

The useClipboard hook provides methods for interacting with the clipboard.

Example

import { useClipboard } from 'react-hooks-simplified';

const ClipboardComponent = () => {
  const { clipboardData, copyToClipboard, readClipboard, error } = useClipboard();

  return (
    <div>
      <button onClick={() => copyToClipboard('Hello, World!')}>Copy to Clipboard</button>
      <button onClick={readClipboard}>Read from Clipboard</button>
      {error && <p>Error: {error}</p>}
      {clipboardData && <p>Clipboard Data: {clipboardData}</p>}
    </div>
  );
};

useNetwork Hook

The useNetwork hook provides information about the network status of the browser.

Example

import { useNetwork } from 'react-hooks-simplified';

const NetworkStatus = () => {
  const { networkInfo, isOnline } = useNetwork();

  return (
    <div>
      <p>Online: {isOnline ? 'Yes' : 'No'}</p>
      {networkInfo && (
        <div>
          <p>Effective Connection Type: {networkInfo.effectiveType}</p>
          <p>Downlink: {networkInfo.downlink} Mbps</p>
        </div>
      )}
    </div>
  );
};

useMediaDevices Hook

The useMediaDevices hook provides access to the media devices available on the user's device.

Example

import { useMediaDevices } from 'react-hooks-simplified';

const MediaDevicesList = () => {
  const { devices, error } = useMediaDevices();

  return (
    <div>
      {error && <p>Error: {error}</p>}
      <ul>
        {devices.map((device) => (
          <li key={device.deviceId}>{device.label}</li>
        ))}
      </ul>
    </div>
  );
};

useFullscreen Hook

The useFullscreen hook provides methods for handling fullscreen mode.

Example

import { useFullscreen } from 'react-hooks-simplified';
import { useRef } from 'react';

const FullscreenComponent = () => {
  const ref = useRef(null);
  const { isFullscreen, enterFullscreen, exitFullscreen } = useFullscreen(ref);

  return (
    <div>
      <div ref={ref}>
        <p>Content to display in fullscreen</p>
      </div>
      <button onClick={isFullscreen ? exitFullscreen : enterFullscreen}>
        {isFullscreen ? 'Exit Fullscreen' : 'Enter Fullscreen'}
      </button>
    </div>
  );
};

useSpeechSynthesis Hook

The useSpeechSynthesis hook provides methods for controlling the speech synthesis API.

Example

import { useSpeechSynthesis } from 'react-hooks-simplified';

const SpeakButton = () => {
  const { speak, cancel, speaking } = useSpeechSynthesis();

  return (
    <div>
      <button onClick={() => speak('Hello, World!')}>
        Speak
      </button>
      <button onClick={cancel} disabled={!speaking}>
        Stop Speaking
      </button>
    </div>
  );
};

useLocalStorage Hook

The useLocalStorage hook provides a way to interact with the browser's local storage.

Example

import { useLocalStorage } from 'react-hooks-simplified';

const NameSaver = () => {
  const [name, setName] = useLocalStorage('name', '');

  return (
    <div>
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
      />
      <p>Stored Name: {name}</p>
    </div>
  );
};

useIntersectionObserver Hook

The useIntersectionObserver hook provides information about the visibility of an element.

Example

import { useRef } from 'react';
import { useIntersectionObserver } from 'react-hooks-simplified';

const IntersectionComponent = () => {
  const ref = useRef(null);
  const isIntersecting = useIntersectionObserver(ref);

  return (
    <div ref={ref}>
      <p>{isIntersecting ? 'In view' : 'Out of view'}</p>
    </div>
  );
};

useIdle Hook

The useIdle hook determines if the user is idle or active.

Example

import { useIdle } from 'react-hooks-simplified';

const IdleComponent = () => {
  const isIdle = useIdle(5000); // User is idle after 5 seconds of inactivity

  return <p>{isIdle ? 'User is idle' : 'User is active'}</p>;
};

useNotifications Hook

The useNotifications hook provides methods for showing notifications and requesting permission.

Example

import { useNotifications } from 'react-hooks-simplified';

const NotificationsComponent = () => {
  const { permission, requestPermission, showNotification } = useNotifications();

  return (
    <div>
      <button onClick={requestPermission}>
        Request Notification Permission
      </button>
      <button
        onClick={() => showNotification('Hello!', { body: 'This is a notification.' })}
        disabled={permission !== 'granted'}
      >
        Show Notification
      </button>
    </div>
  );
};

useWindowSize Hook

The useWindowSize hook provides information about the current window size.

Example

import { useWindowSize } from 'react-hooks-simplified';

const WindowSizeComponent = () => {
  const { width, height } = useWindowSize();

  return (
    <div>
      <p>Width: {width}px</p>
      <p>Height: {height}px</p>
    </div>
  );
};

useDarkMode Hook

The useDarkMode hook provides methods for toggling between dark and light mode.

Example

import { useDarkMode } from 'react-hooks-simplified';

const DarkModeComponent = () => {
  const { isDarkMode, toggleDarkMode } = useDarkMode();

  return (
    <div>
      <button onClick={toggleDarkMode}>
        Switch to {isDarkMode ? 'Light' : 'Dark'} Mode
      </button>
    </div>
  );
};

useMediaQuery Hook

The useMediaQuery hook provides a way to detect media query matches.

Example

import { useMediaQuery } from 'react-hooks-simplified';

const MediaQueryComponent = () => {
  const isLargeScreen = useMediaQuery('(min-width: 1024px)');

  return <p>{isLargeScreen ? 'Large screen' : 'Small screen'}</p>;
};