Tally UI

ScannerViewfinder

ScannerViewfinder is the web camera view that decodes barcodes from a live video stream and reports each code and its status.

Import

import { ScannerViewfinder } from '@tallyui/components';
import type { CameraDecoder, ViewfinderStatus } from '@tallyui/pos';

Usage

<ScannerViewfinder
  onDetected={(code, symbology) => offer(code, symbology)}
  onStatus={(status) => setStatus(status)}
/>

CameraScannerPanel renders it by default, so an app rarely uses it directly. On the web, where navigator.mediaDevices.getUserMedia exists, it opens the camera (the rear one where it can) into a muted, inline <video> that fills the view and decodes it with startDecodeLoop from @tallyui/pos. On unmount it stops the decode loop and the camera tracks, including a stream that arrives after unmount.

Anywhere else, including native, it reports 'camera-unavailable' and renders an empty view. A native app passes its own viewfinder with the same props to CameraScannerPanel (for example one built on expo-camera).

Props

PropTypeDefaultDescription
onDetected(code: string, symbology?: string) => voidrequiredCalled with each decoded code
onStatus(status: ViewfinderStatus) => voidrequiredCalled as the camera state changes
decoderCameraDecoderthe BarcodeDetector globalAn object with the same detect method, such as a ponyfill

When no decoder is passed and the browser has no BarcodeDetector, the status becomes 'decoder-error' once the camera is open.

ViewfinderStatus

The props in the table above are exported as the type ViewfinderProps, the shape a replacement viewfinder takes. ViewfinderStatus is the union passed to onStatus:

type ViewfinderStatus = 'initializing' | 'scanning' | 'camera-denied' | 'camera-unavailable' | 'decoder-error';

A camera error reports 'camera-denied' when access is refused (a NotAllowedError) and 'camera-unavailable' otherwise. 'decoder-error' also follows five decode failures in a row, and 'scanning' returns on the next successful decode.