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-browser-api

v1.0.7

Published

A powerful and simple-to-use React hooks library for accessing browser APIs like **Geolocation** and **Clipboard**. With this library, you can effortlessly integrate real-time location tracking and clipboard functionality into your React applications.

Downloads

451

Readme

📦 react-browser-api

A powerful and simple-to-use React hooks library for accessing browser APIs like Geolocation and Clipboard. With this library, you can effortlessly integrate real-time location tracking and clipboard functionality into your React applications.


🚀 Installation

npm install react-browser-api

💾 useLocalStorage

A React hook for interacting with localStorage. It supports multiple keys, initialization with default values, and dynamic updates to localStorage.

API:

storedValues: An object holding all key-value pairs from localStorage.

error: Any error that occurs during reading/writing to localStorage.

setValue(key, value): Sets a value for a given key.

clear(key): Clears a specific key from localStorage and state.

addKey(key, value): Adds a new key-value pair to localStorage and state.

deleteKey(key): Deletes a specific key from localStorage and state.

import { useLocalStorage } from './useLocalStorage';

const MyComponent = () => {
  // Initialize with no keys or with keys
  const { storedValues, addKey, deleteKey, getKey, error } = useLocalStorage([], {});

  const handleAddKey = () => {
    addKey('user', 'John Doe');
  };

  const handleDeleteKey = (key: string) => {
    deleteKey(key);
  };

};

⌛ useSessionStorage

A React hook for interacting with sessionStorage. It functions similarly to useLocalStorage but persists data only for the duration of the page session.

API:

storedValues: An object holding all key-value pairs from sessionStorage.

error: Any error that occurs during reading/writing to sessionStorage.

setValue(key, value): Sets a value for a given key.

clear(key): Clears a specific key from sessionStorage and state.

addKey(key, value): Adds a new key-value pair to sessionStorage and state.

deleteKey(key): Deletes a specific key from sessionStorage and state.

getKey(key): Retrieves the value of a specific key.

import { useSessionStorage } from './useSessionStorage';

const MyComponent = () => {
  // Initialize with no keys
  const { storedValues, addKey, deleteKey, getKey, error } = useSessionStorage([], {});

  const handleAddKey = () => {
    addKey('theme', 'dark');
  };

  const handleDeleteKey = (key: string) => {
    deleteKey(key);
  };
};

🧭 useGeolocation

The useGeolocation hook allows you to fetch the user's current location and track it in real-time (if desired). It provides latitude, longitude, and accuracy values, along with error handling.

📌 Usage

import React from "react";
import { useGeolocation } from "react-browser-api";

function LocationComponent() {
  const { position, error } = useGeolocation({ watch: true, enableHighAccuracy: true });

  return (
    <div>
      <h2>Current Location</h2>
      {position ? (
        <p>Latitude: {position.latitude}, Longitude: {position.longitude}</p>
      ) : (
        <p>{error ? `Error: ${error}` : "Fetching location..."}</p>
      )}
    </div>
  );
}

⚙️ Options

The useGeolocation hook accepts an optional configuration object:

enableHighAccuracy: boolean (default: false) – Request high accuracy location.

timeout: number (default: 10000) – Maximum time (in ms) allowed for obtaining location.

maximumAge: number (default: 0) – Maximum age (in ms) of a possible cached position.

watch: boolean (default: false) – Continuously track location updates.

🧾 Return Values

position: { latitude: number, longitude: number, accuracy: number } | null – The latest geolocation data. error: string | null – Error message if the location could not be retrieved.

📋 useClipboard

The useClipboard hook enables seamless clipboard interactions by providing methods to copy to and read from the clipboard, along with error handling.

📌 Usage

import React from "react";
import { useClipboard } from "react-browser-api";

function ClipboardComponent() {
  const { clipboardContent, error, copyToClipboard, readFromClipboard } = useClipboard();

  return (
    <div>
      <h2>Clipboard Example</h2>
      <button onClick={() => copyToClipboard("Hello World!")}>Copy Text</button>
      <button onClick={readFromClipboard}>Read Clipboard</button>
      {clipboardContent && <p>Clipboard Content: {clipboardContent}</p>}
      {error && <p style={{ color: 'red' }}>Error: {error}</p>}
    </div>
  );
}

🧾 Return Values

clipboardContent: string | null – The current content of the clipboard. error: string | null – Error message if there was an issue with clipboard access. copyToClipboard: (text: string) => Promise – Copies the specified text to the clipboard. readFromClipboard: () => Promise – Reads text from the clipboard.

❤️ Developed by

Aftab
🌐 Website