From 7caef3556f86893fbe7bdabe445597a9b25a5246 Mon Sep 17 00:00:00 2001 From: Hunter Johnston <64506580+huntabyte@users.noreply.github.com> Date: Wed, 11 Dec 2024 16:44:31 -0500 Subject: [PATCH] feat: expose `*ref` props to access underlying elements (#5) --- .changeset/fresh-numbers-travel.md | 5 ++ .../components/demos/barqode-dropzone.svelte | 39 ++++--------- .../components/demos/barqode-stream.svelte | 35 +++--------- .../src/lib/components/demos/full-demo.svelte | 54 ++++++++---------- .../lib/components/barqode-dropzone.svelte | 9 +-- .../src/lib/components/barqode-stream.svelte | 55 +++++++++++-------- packages/barqode/src/lib/components/types.ts | 48 +++++++++++++++- 7 files changed, 131 insertions(+), 114 deletions(-) create mode 100644 .changeset/fresh-numbers-travel.md diff --git a/.changeset/fresh-numbers-travel.md b/.changeset/fresh-numbers-travel.md new file mode 100644 index 0000000..faee536 --- /dev/null +++ b/.changeset/fresh-numbers-travel.md @@ -0,0 +1,5 @@ +--- +"barqode": patch +--- + +expose `*ref` props for access to the underlying elements diff --git a/docs/src/lib/components/demos/barqode-dropzone.svelte b/docs/src/lib/components/demos/barqode-dropzone.svelte index 0089ee5..1dc1a40 100644 --- a/docs/src/lib/components/demos/barqode-dropzone.svelte +++ b/docs/src/lib/components/demos/barqode-dropzone.svelte @@ -1,6 +1,6 @@ - -
Detecting QR-codes
+ +
Detecting QR-codes
-
+
-
+

Click to upload or drop an image here

-
+
Last detected: {result}
- - diff --git a/docs/src/lib/components/demos/barqode-stream.svelte b/docs/src/lib/components/demos/barqode-stream.svelte index 309667f..f467bde 100644 --- a/docs/src/lib/components/demos/barqode-stream.svelte +++ b/docs/src/lib/components/demos/barqode-stream.svelte @@ -1,9 +1,10 @@ - -
Detecting QR-codes
+ +
Detecting QR-codes
-
+
{#if loading} -
Loading...
+
+ +
{/if}
-
+
Last detected: {result}
- - diff --git a/docs/src/lib/components/demos/full-demo.svelte b/docs/src/lib/components/demos/full-demo.svelte index dbb4ec9..df23ed3 100644 --- a/docs/src/lib/components/demos/full-demo.svelte +++ b/docs/src/lib/components/demos/full-demo.svelte @@ -1,6 +1,6 @@ - -
- - +
-
- - +
- -
- -
+
+ Barcode formats: +
{#each Object.keys(barcodeFormats) as option} {@const barcodeOption = option as BarcodeFormat} -
- - +
+ +
{/each}
-
+
{#if error} -
{error}
+
{error}
{/if}
@@ -247,10 +246,3 @@ Last detected: {result}
- - diff --git a/packages/barqode/src/lib/components/barqode-dropzone.svelte b/packages/barqode/src/lib/components/barqode-dropzone.svelte index c08b78e..799e4f6 100644 --- a/packages/barqode/src/lib/components/barqode-dropzone.svelte +++ b/packages/barqode/src/lib/components/barqode-dropzone.svelte @@ -8,11 +8,11 @@ onDragover, onError, children, + inputRef = $bindable(null), + wrapperRef = $bindable(null), ...restProps }: DropzoneProps = $props(); - let input: HTMLInputElement = $state()!; - async function onDetectFile(promise: Promise) { try { const detectedCodes = await promise; @@ -76,7 +76,7 @@ } function onClick() { - input.click(); + inputRef?.click(); } @@ -91,6 +91,7 @@ ondragover={handleDragOver} onclick={onClick} onkeydown={onClick} + bind:this={wrapperRef} > {@render children?.()} diff --git a/packages/barqode/src/lib/components/barqode-stream.svelte b/packages/barqode/src/lib/components/barqode-stream.svelte index 2a9ceba..d409713 100644 --- a/packages/barqode/src/lib/components/barqode-stream.svelte +++ b/packages/barqode/src/lib/components/barqode-stream.svelte @@ -16,12 +16,13 @@ onCameraOff, onDetect, children, + videoRef = $bindable(null), + pauseRef = $bindable(null), + trackingRef = $bindable(null), + wrapperRef = $bindable(null), }: StreamProps = $props(); // state - let video: HTMLVideoElement = $state()!; - let pauseFrame: HTMLCanvasElement = $state()!; - let trackingLayer: HTMLCanvasElement = $state()!; let cameraActive = $state(false); let isMounted = $state(false); @@ -37,12 +38,13 @@ () => cameraSettings, () => { const settings = cameraSettings; - const ctx = pauseFrame.getContext("2d")!; + + const ctx = pauseRef!.getContext("2d")!; if (!settings.shouldStream) { - pauseFrame.width = video.videoWidth; - pauseFrame.height = video.videoHeight; - ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight); + pauseRef!.width = videoRef!.videoWidth; + pauseRef!.height = videoRef!.videoHeight; + ctx.drawImage(videoRef!, 0, 0, videoRef!.videoWidth, videoRef!.videoHeight); cameraController.stop(); cameraActive = false; @@ -55,7 +57,7 @@ try { cameraController - .start(video, settings) + .start(videoRef!, settings) .then((capabilities) => { if (isMounted) { cameraActive = true; @@ -88,12 +90,12 @@ () => shouldScan, () => { if (shouldScan) { - clearCanvas(pauseFrame); - clearCanvas(trackingLayer); + clearCanvas(pauseRef!); + clearCanvas(trackingRef!); const scanInterval = track === undefined ? 500 : 40; - keepScanning(video, { + keepScanning(videoRef!, { detectHandler: (detectedCodes: DetectedBarcode[]) => onDetect?.(detectedCodes), formats: formats, locateHandler: onLocate, @@ -124,12 +126,12 @@ */ function onLocate(detectedCodes: DetectedBarcode[]) { if (detectedCodes.length === 0 || track === undefined) { - clearCanvas(trackingLayer); + clearCanvas(trackingRef!); } else { - const displayWidth = video.offsetWidth; - const displayHeight = video.offsetHeight; - const resolutionWidth = video.videoWidth; - const resolutionHeight = video.videoHeight; + const displayWidth = videoRef!.offsetWidth; + const displayHeight = videoRef!.offsetHeight; + const resolutionWidth = videoRef!.videoWidth; + const resolutionHeight = videoRef!.videoHeight; const largerRatio = Math.max( displayWidth / resolutionWidth, @@ -173,9 +175,9 @@ }; }); - trackingLayer.width = video.offsetWidth; - trackingLayer.height = video.offsetHeight; - const ctx = trackingLayer.getContext("2d") as CanvasRenderingContext2D; + trackingRef!.width = videoRef!.offsetWidth; + trackingRef!.height = videoRef!.offsetHeight; + const ctx = trackingRef!.getContext("2d") as CanvasRenderingContext2D; track?.(adjustedCodes, ctx); } } @@ -189,19 +191,26 @@ }); -
-