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

@programic/vue-barcode-detector

v1.0.0

Published

A barcode device detector for JavaScript/TypeScript and VueJS.

Downloads

73

Readme

Vue3 Barcode Detector

Programic npm version Vue 3 TypeScript TypeScript

This package contains Programic's Vue Barcode Detector for Vue 3, which can be used for detecting the input of physical USB barcode scanners. These devices often fire keyboard events on scanning a barcode, which can be detected and compiled by the Vue composable included in this package.

It's very easy to use! Take a look at this simple implementation in a Vue component:

<script setup lang="ts">
import useBarcodeDetector, { ScannedBarcodeData } from '@programic/vue-barcode-detector';

const barcodeDetector = useBarcodeDetector();

barcodeDetector.listen((barcodeData: ScannedBarcodeData): void => {
    console.log(barcodeData);
    // Logs: { 'timestamp': <SCANNED_TIME_TIMESTAMP>, 'value': <SCANNED_BARCODE_VALUE> }
});
</script>

For example: The barcode contains a value like 9146791273. Barcode scanner devices will trigger each separate character like a keyboard input event, which is followed by an Enter key. Because it fires these key events quite rapidly, the detector will listen to a very short timeout before resetting so that it doesn't trigger on normal keyboard inputs.

Installation

Install @programic/vue-barcode-detector in your project with NPM or Yarn:

npm install @programic/vue-barcode-detector
yarn add @programic/vue-barcode-detector

Usage

The useBarcodeDetector() function returns a composable containing three items:

The 'listen' function

Can be used to start listening to barcode scanner devices on mounting a component. The function will register several event listeners, and its only parameter is a callback function that is called after scanning a barcode. The callback injects an object with a timestamp and the barcode value.

import useBarcodeDetector, { ScannedBarcodeData } from '@programic/vue-barcode-detector';

const barcodeDetector = useBarcodeDetector();

barcodeDetector.listen((barcodeData: ScannedBarcodeData): void => {
// barcodeData = { 'timestamp': <SCANNED_TIME_TIMESTAMP>, 'value': <SCANNED_BARCODE_VALUE> }
});

The 'stop' function

Can be used to manually stop listening to the barcode scanner input.

import useBarcodeDetector from '@programic/vue-barcode-detector';

const barcodeDetector = useBarcodeDetector();

barcodeDetector.stopListening();

The composable will automatically call this function on unMount(), so that the event listeners will be removed from the browser's runtime. This will avoid duplicate event listeners from being registered.

The 'barcode' ref

After scanning a barcode, its value will be stored in a ref that is defined inside the composable. The listen function will use this ref's value to return in the listen() callback, but it may also be read directly for reactive triggers.

<script setup lang="ts">
import useBarcodeDetector from '@programic/vue-barcode-detector';

const barcodeDetector = useBarcodeDetector();
</script>

<template>
    <p>
        {{ barcodeDetector.barcode }}
    </p>
</template>

TypeScript interfaces

This package also exports some interfaces that can be used for TypeScript implementations:

export interface ScannedBarcodeData {
  timestamp: number;
  value: string;
}

export interface BarcodeScannerListenerCallback {
  (barcodeData: ScannedBarcodeData): void;
}

Requirements

| Dependency | minimal version | |-------------------------|-----------------| | Vue (Composition API) | ^3.4.38 |