Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 40 additions & 51 deletions src/components/Scanner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useRef, useMemo, ReactNode } from 'react';
import React, { useState, useEffect, useRef, useMemo, ReactNode, forwardRef } from 'react';

import type { BarcodeFormat } from 'barcode-detector';

Expand All @@ -8,14 +8,8 @@ import useScanner from '../hooks/useScanner';

import deepEqual from '../utilities/deepEqual';
import { defaultComponents, defaultConstraints, defaultStyles } from '../misc';
import {
IDetectedBarcode,
IPoint,
IScannerClassNames,
IScannerComponents,
IScannerStyles,
TrackFunction
} from '../types';
import { IDetectedBarcode, IPoint, IScannerClassNames, IScannerComponents, IScannerStyles, TrackFunction } from '../types';
import { mergeRefs } from '../utilities/mergeRefs';

export interface IScannerProps {
onScan: (detectedCodes: IDetectedBarcode[]) => void;
Expand Down Expand Up @@ -125,21 +119,8 @@ function onFound(detectedCodes: IDetectedBarcode[], videoEl?: HTMLVideoElement |
}
}

export function Scanner(props: IScannerProps) {
const {
onScan,
constraints,
formats = ['qr_code'],
paused = false,
components,
children,
styles,
classNames,
allowMultiple,
scanDelay,
onError,
sound
} = props;
export const Scanner = forwardRef<HTMLVideoElement, IScannerProps>((props, ref) => {
const { onScan, constraints, formats = ['qr_code'], paused = false, components, children, styles, classNames, allowMultiple, scanDelay, onError, sound } = props;

const videoRef = useRef<HTMLVideoElement>(null);
const pauseFrameRef = useRef<HTMLCanvasElement>(null);
Expand Down Expand Up @@ -284,12 +265,18 @@ export function Scanner(props: IScannerProps) {

return (
<div style={{ ...defaultStyles.container, ...styles?.container }} className={classNames?.container}>
<video ref={videoRef}
style={{
...defaultStyles.video, ...styles?.video,
visibility: paused ? 'hidden' : 'visible'
}}
className={classNames?.video} autoPlay muted playsInline />
<video
ref={mergeRefs(ref, videoRef)}
Copy link

Copilot AI Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current mergeRefs signature (Array<RefObject | RefCallback ...>), videoRef (MutableRefObject<HTMLVideoElement | null>) is not assignable, producing a type error. This will be resolved by updating mergeRefs to accept React.Ref (or include MutableRefObject<T | null>) as suggested.

Copilot uses AI. Check for mistakes.

style={{
...defaultStyles.video,
...styles?.video,
visibility: paused ? 'hidden' : 'visible'
}}
className={classNames?.video}
autoPlay
muted
playsInline
/>
<canvas
ref={pauseFrameRef}
style={{
Expand All @@ -309,31 +296,31 @@ export function Scanner(props: IScannerProps) {
zoom={
mergedComponents.zoom && camera.settings.zoom
? {
value: camera.settings.zoom,
onChange: async (value) => {
const newConstraints = {
...constraintsCached,
advanced: [{ zoom: value }]
};

await camera.updateConstraints(newConstraints);
}
}
value: camera.settings.zoom,
onChange: async (value) => {
const newConstraints = {
...constraintsCached,
advanced: [{ zoom: value }]
};

await camera.updateConstraints(newConstraints);
}
}
: undefined
}
torch={
mergedComponents.torch
? {
status: camera.settings.torch ?? false,
toggle: async (value) => {
const newConstraints = {
...constraintsCached,
advanced: [{ torch: value }]
};

await camera.updateConstraints(newConstraints);
}
}
status: camera.settings.torch ?? false,
toggle: async (value) => {
const newConstraints = {
...constraintsCached,
advanced: [{ torch: value }]
};

await camera.updateConstraints(newConstraints);
}
}
: undefined
}
startScanning={async () => await onCameraChange()}
Expand All @@ -349,4 +336,6 @@ export function Scanner(props: IScannerProps) {
</div>
</div>
);
}
});

Scanner.displayName = 'Scanner';
18 changes: 18 additions & 0 deletions src/utilities/mergeRefs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { RefCallback, RefObject } from 'react';

export function mergeRefs<T>(...refs: Array<RefObject<T> | RefCallback<T> | null | undefined>): RefCallback<T> {
Copy link

Copilot AI Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature excludes MutableRefObject and uses RefObject, which makes passing useRef refs (e.g., MutableRefObject<HTMLVideoElement | null>) a type error. Switch to React.Ref (or include MutableRefObject<T | null>) so both object and callback refs are accepted.

Copilot uses AI. Check for mistakes.

return (value) => {
for (const ref of refs) {
if (ref == null) {
continue;
}

if (typeof ref === 'function') {
ref(value);
continue;
}

(ref as RefObject<T | null>).current = value;
Copy link

Copilot AI Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RefObject has a readonly current; assigning to .current here is a type error. Cast to MutableRefObject<T | null> (or narrow with an appropriate type guard) before assignment: (ref as MutableRefObject<T | null>).current = value.

Copilot uses AI. Check for mistakes.

}
};
}