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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-simple-usb-scanner

v1.0.2

Published

A lightweight React library for capturing and handling the barcodes scanned by USB barcode scanners in React applications.

Readme

React Simple USB Scanner

A lightweight React library for capturing and handling the barcodes scanned by USB barcode scanners in React applications.

Installation

npm install react-simple-usb-scanner

or

yarn add react-simple-usb-scanner

Basic Usage

import { useBarcodeScanner } from 'react-simple-usb-scanner';

function App() {
  const { barcode, resetBarcode } = useBarcodeScanner({
    onBarcodeScanned: async (code) => {
      console.log('Barcode scanned:', code);
    },
    enabled: true,
  });

  const handleResetBarCode = () => {
    resetBarcode();
  };

  const handleShowBarcode = () => {
    alert(`Barcode scanned: ${barcode}`);
  };

  return (
    <div>
      <h1>Barcode Scanner</h1>
      <p>Current barcode: {barcode}</p>
      <button onClick={handleShowBarcode}>Show Barcode</button>
      <button onClick={handleResetBarCode}>Reset</button>
    </div>
  );
}

API Reference

useBarcodeScanner(options)

The main hook for detecting barcode scanner input.

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | onBarcodeScanned | (barcode: string) => void | undefined | Callback function that is executed when a valid barcode is scanned | | timeout | number | 50 | Time in milliseconds to wait before processing the code | | minLength | number | 4 | Minimum required length to consider a code as valid | | preventDefault | boolean | true | Whether to prevent the default behavior of keys. Prevents characters from appearing in focused inputs | | enabled | boolean | true | Allows dynamically enabling/disabling the scanner | | pauseOnInputFocus | boolean | true | Whether the scanner should ignore keyboard input when an input element (INPUT, TEXTAREA, SELECT) is focused |

Return Value

The hook returns an object with the following properties:

| Property | Type | Description | |----------|------|-------------| | barcode | string | The current scanned barcode value | | isScanning | boolean | Indicates whether a scan is currently in progress | | resetBarcode | () => void | Function to clear the current barcode and reset the scanner state |

Advanced Examples

With Custom Timeout and Minimum Length

const { barcode, isScanning } = useBarcodeScanner({
  onBarcodeScanned: (code) => {
    console.log('Scanned:', code);
  },
  timeout: 100,
  minLength: 8,
  preventDefault: true,
});

Conditional Scanner Activation

function App() {
  const [scannerEnabled, setScannerEnabled] = useState(false);

  const { barcode } = useBarcodeScanner({
    onBarcodeScanned: (code) => {
      console.log('Barcode:', code);
    },
    enabled: scannerEnabled,
  });

  return (
    <div>
      <button onClick={() => setScannerEnabled(!scannerEnabled)}>
        {scannerEnabled ? 'Disable' : 'Enable'} Scanner
      </button>
      <p>Scanned: {barcode}</p>
    </div>
  );
}

With Input Fields

The scanner automatically pauses when input fields are focused (when pauseOnInputFocus is true), allowing users to type normally:

function App() {
  const { barcode } = useBarcodeScanner({
    onBarcodeScanned: (code) => {
      console.log('Scanned:', code);
    },
    pauseOnInputFocus: true, // This is the default
  });

  return (
    <div>
      <input type="text" placeholder="Type here normally..." />
      <p>Last scanned barcode: {barcode}</p>
    </div>
  );
}

How It Works

USB barcode scanners typically act as keyboard input devices. When a barcode is scanned, the scanner rapidly inputs each character followed by an Enter key. This library:

  1. Listens for rapid keyboard input
  2. Buffers the characters
  3. Processes the buffer when input stops (after the specified timeout)
  4. Triggers the callback with the complete barcode
  5. Automatically pauses when the user is typing in input fields

Browser Compatibility

This library works in all modern browsers that support:

  • React 16.8+
  • Keyboard events
  • Document focus events

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development

This template uses Vite + React + TypeScript for fast development.

Available Scripts

  • npm run dev - Start development server
  • npm run build - Build for production
  • npm run lint - Run ESLint
  • npm run preview - Preview production build