Skip to content

Commit 500e0e4

Browse files
committed
fix(QrcodeStream): styling sometimes missing
For some users the external style.css is not automatically imported. As a quick hotfix: now inline all CSS to avoid external CSS files in general. See #360
1 parent b6a23cf commit 500e0e4

File tree

3 files changed

+54
-40
lines changed

3 files changed

+54
-40
lines changed

shell.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ pkgs.mkShell {
66
nodejs_18
77
nodePackages.pnpm
88
nodePackages.typescript-language-server
9-
nodePackages.vls
109
];
1110

1211
}

src/components/QrcodeStream.vue

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
<template>
2-
<div class="qrcode-stream-wrapper">
3-
<!--
4-
Note, the following DOM elements are not styled with z-index.
5-
If z-index is not defined, elements are stacked in the order they appear in the DOM.
6-
The first element is at the very bottom and subsequent elements are added on top.
7-
-->
2+
<div :style="wrapperStyle">
3+
<!--
4+
All immediate children of the wrapper div are stacked upon each other.
5+
The z-index is implicitly given by the (inverse) element order.
6+
7+
The video element is at the very bottom, the pause frame canvas is above it,
8+
the tracking layer is yet above and finally at the very top is the slot
9+
overlay.
10+
-->
11+
812
<video
913
ref="videoRef"
10-
:class="{ 'qrcode-stream-camera--hidden': !shouldScan }"
11-
class="qrcode-stream-camera"
14+
:style="videoElStyle"
1215
autoplay
1316
muted
1417
playsinline
1518
/>
1619

17-
<canvas ref="pauseFrameRef" v-show="!shouldScan" class="qrcode-stream-camera" />
20+
<canvas id="qrcode-stream-pause-frame" ref="pauseFrameRef" v-show="!shouldScan" :style="cameraStyle" />
1821

19-
<canvas ref="trackingLayerRef" class="qrcode-stream-overlay" />
22+
<canvas id="qrcode-stream-tracking-layer" ref="trackingLayerRef" :style="overlayStyle" />
2023

21-
<div class="qrcode-stream-overlay">
24+
<div :style="overlayStyle">
2225
<slot />
2326
</div>
2427
</div>
2528
</template>
2629

2730
<script setup lang="ts">
2831
import type { DetectedBarcode, BarcodeFormat } from '@sec-ant/barcode-detector/pure'
29-
import { onUnmounted, computed, onMounted, ref, toRefs, watch, type PropType } from 'vue'
32+
import { onUnmounted, computed, onMounted, ref, toRefs, watch, type PropType, type CSSProperties } from 'vue'
3033
3134
import { keepScanning, setScanningFormats } from '../misc/scanner'
3235
import * as cameraController from '../misc/camera'
@@ -261,33 +264,37 @@ const onLocate = (detectedCodes: DetectedBarcode[]) => {
261264
props.track(adjustedCodes, ctx)
262265
}
263266
}
264-
</script>
265267
266-
<style lang="css" scoped>
267-
.qrcode-stream-wrapper {
268-
width: 100%;
269-
height: 100%;
268+
/**
269+
* Styling is all inline to avoid generating an external style.css file.
270+
* Component users shouldn't have to figure out how to setup their bundler to
271+
* import external CSS.
272+
*/
270273
271-
position: relative;
272-
z-index: 0;
274+
const wrapperStyle : CSSProperties = {
275+
"width": "100%",
276+
"height": "100%",
277+
"position": "relative",
278+
// notice that we use z-index only once for the wrapper div.
279+
// If z-index is not defined, elements are stacked in the order they appear in the DOM.
280+
// The first element is at the very bottom and subsequent elements are added on top.
281+
"z-index": "0",
273282
}
274283
275-
.qrcode-stream-overlay {
276-
width: 100%;
277-
height: 100%;
278-
279-
position: absolute;
280-
top: 0;
281-
left: 0;
284+
const overlayStyle : CSSProperties = {
285+
"width": "100%",
286+
"height": "100%",
287+
"position": "absolute",
288+
"top": "0",
289+
"left": "0",
282290
}
283291
284-
.qrcode-stream-camera {
285-
width: 100%;
286-
height: 100%;
287-
288-
display: block;
289-
object-fit: cover;
292+
const cameraStyle : CSSProperties = {
293+
"width": "100%",
294+
"height": "100%",
295+
"object-fit": "cover"
290296
}
297+
291298
/* When a camera stream is loaded, we assign the stream to the `video`
292299
* element via `video.srcObject`. At this point the element used to be
293300
* hidden with `v-show="false"` aka. `display: none`. We do that because
@@ -300,8 +307,17 @@ const onLocate = (detectedCodes: DetectedBarcode[]) => {
300307
* element was hidden with `display: none`. Using `visibility: hidden`
301308
* instead seems to have fixed the problem though.
302309
*/
303-
.qrcode-stream-camera--hidden {
304-
visibility: hidden;
305-
position: absolute;
306-
}
307-
</style>
310+
const videoElStyle = computed<CSSProperties>(() => {
311+
if (shouldScan.value) {
312+
return cameraStyle
313+
} else {
314+
return {
315+
...cameraStyle,
316+
317+
"visibility": "hidden",
318+
"position": "absolute",
319+
}
320+
}
321+
})
322+
323+
</script>

src/misc/scanner.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { type DetectedBarcode, type BarcodeFormat, BarcodeDetector } from '@sec-ant/barcode-detector/pure'
1+
import { type DetectedBarcode, type BarcodeFormat, BarcodeDetector } from '@sec-ant/barcode-detector/pure'
22
import { eventOn } from './callforth'
33
import { DropImageFetchError } from './errors'
44

5-
65
let barcodeDetector: BarcodeDetector
76
export const setScanningFormats = (formats: BarcodeFormat[]) => {
87
barcodeDetector = new BarcodeDetector({ formats })

0 commit comments

Comments
 (0)