Barcode Scanner
Reads barcodes and QR codes with the camera: a scan window, single or continuous scanning, a beep, the camera light, and a scan event with the value and the format.
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
formats | string[] | [] | The formats to read, as BarcodeDetector names them: qr_code, ean_13, ean_8, upc_a, upc_e, code_128,
code_39, code_93, codabar, itf, data_matrix, aztec, pdf417. As an attribute, separated by ;.
Empty reads every format the decoder knows. |
continuous | boolean | false | Keeps the camera open after a code is read and fires scan for each new one, instead of stopping at the first. |
repeat-delay | number | 1500 | In continuous mode, how long in milliseconds a code must be out of the picture before reading it again fires
scan again. A code that stays in the window fires once. |
beep | boolean | false | Sounds a short beep when a code is read. |
torch | boolean | false | The camera's light. It only turns on while the camera is open and has one; the light button toggles it. |
facing | CameraFacing | environment | The camera to open: environment (the back one, the default) or user (the front one). Switching camera updates it. |
autostart | boolean | false | Opens the camera as soon as the element is on the page, without waiting for the Start camera button. |
label | string | — | The accessible name of the component. Empty uses the built-in "Barcode scanner". |
Slots
| Name | Description |
|---|---|
idle | Replaces the screen shown before the camera starts (an icon, a sentence and the Start camera button). Call start() from your own button. |
error | Replaces the screen shown when the camera or the decoder cannot start. Read the reason from the camera-error event. |
window | Replaces the drawing of the scan window; the window's box (part="window") stays, and is the area that is read. |
done | Replaces the screen shown once a code is read (its value, its format and the Scan again button). Read the code from the scan event. |
torch-icon | Replaces the icon of the light button. |
switch-icon | Replaces the icon of the Switch camera button. |
actions | Action zone under the picture, for the app's own commands: type the code instead, read a picture of it (scanImage()), cancel. The events do not bubble: listen on the element itself. |
Events
| Name | Type | Description |
|---|---|---|
camera-start | CustomEvent<{ facing: 'user' | 'environment' }> | Fired when the camera shows its first picture. |
camera-error | CustomEvent<{ kind: 'denied' | 'not-found' | 'busy' | 'unsupported' | 'ended' | 'decoder' | 'unknown'; error: unknown }> | Fired when the camera cannot start or stops while open, or (decoder) when nothing here can read a barcode. |
scan | CustomEvent<BarcodeScanResult> | Fired when a code is read. detail.value is its text, detail.format its format (qr_code, ean_13…), detail.corners its corners in the camera frame when known. |
CSS parts
Shadow DOM styling hooks used with ::part(name). Prefer CSS custom properties when they cover the change.
| Name | Description |
|---|---|
stage | The dark box that holds the picture and the idle, error and done screens. |
video | The camera picture. |
window | The box of the scan window, wrapping the window slot and its default drawing; only its area is read. |
frame | The default drawing of the scan window. |
line | The line that sweeps the scan window. |
hint | The instruction over the picture. |
result | The last code read, over the picture, in continuous mode. |
controls | The bar under the picture. |
actions | The wrapper of the actions slot. |
torch | The button that turns the camera's light on and off, shown when the camera has one. It wraps the torch-icon slot: the built-in icon shows as the slot's fallback, and assigned content replaces it at the same size. |
switch | The button that switches camera, shown when the device has more than one. It wraps the switch-icon slot: the built-in icon shows as the slot's fallback, and assigned content replaces it at the same size. |
button | The Start camera, Try again and Scan again buttons. |
idle | The region of the idle screen, wrapping the idle slot and its default content. |
error | The region of the error screen, wrapping the error slot and its default content. |
done | The region of the done screen, wrapping the done slot and its default content. |
value | The value of the code on the done screen. |
format | The format of the code on the done screen. |
CSS custom properties
Grouped by semantic target and state from --c2-<component>__<target>__<state>--<property>. Click a group to collapse it; click a name to copy it.
| Name | Type | Default | Description |
|---|---|---|---|
--c2-barcode-scanner--width | length | 480px | Width of the component, never wider than its container; 100% fills it. |
--c2-barcode-scanner__stage--aspect-ratio | aspect-ratio | 4 / 3 | Shape of the picture; the camera is cropped to fill it. |
--c2-barcode-scanner__stage--background | background | #18181b | Behind the picture, and of the idle, error and done screens. Dark in both themes, like a viewfinder. |
--c2-barcode-scanner__stage--color | color | #fafafa | Text on the stage. |
--c2-barcode-scanner__stage--border-radius | border-radius | 8px | |
--c2-barcode-scanner__stage__muted--color | color | #a1a1aa | Secondary text on the stage. |
--c2-barcode-scanner__window--width | length | 70% | The most of the stage's width the scan window takes. The window keeps its shape and fits both this and the height. |
--c2-barcode-scanner__window--height | length | 70% | The most of the stage's height the scan window takes. |
--c2-barcode-scanner__window--aspect-ratio | number | 1 | Width over height, as a number: 1 suits a QR code, about 2.4 a 1D barcode. |
--c2-barcode-scanner__window--border-radius | border-radius | 14px | |
--c2-barcode-scanner__window--border-width | pixel | 3px | |
--c2-barcode-scanner__window--border-color | color | #ffffff | |
--c2-barcode-scanner__window__found--border-color | color | #16a34a | The window just after a code is read. |
--c2-barcode-scanner__window__scrim--color | color | rgba(0, 0, 0, 0.45) | Darkens the picture outside the window. |
--c2-barcode-scanner__line--color | color | #ef4444 | The line that sweeps the window. |
--c2-barcode-scanner__line--height | pixel | 2px | |
--c2-barcode-scanner__line--duration | time | 2s | One sweep down and back. The line stands still under reduced motion. |
--c2-barcode-scanner__line--display | display | block | none hides the line. |
--c2-barcode-scanner__hint--background | background | rgba(24, 24, 27, 0.72) | Of the instruction and of the last code read. |
--c2-barcode-scanner__hint--color | color | #fafafa | |
--c2-barcode-scanner__hint--font-size | font-size | 14px | |
--c2-barcode-scanner__hint--border-radius | border-radius | 999px | |
--c2-barcode-scanner__hint--display | display | block | none hides the instruction. |
--c2-barcode-scanner__result--background | background | rgba(21, 128, 61, 0.92) | The last code read, in continuous mode. |
--c2-barcode-scanner__screen--font-size | font-size | 14px | Text of the idle, starting and error screens. |
--c2-barcode-scanner__value--font-size | font-size | 18px | The value on the done screen. |
--c2-barcode-scanner__value--font-weight | font-weight | 600 | |
--c2-barcode-scanner__value--font-family | font-family | ui-monospace, SFMono-Regular, Menlo, Consolas, monospace | |
--c2-barcode-scanner__done__icon--color | color | #4ade80 | The check of the done screen. |
--c2-barcode-scanner__controls--padding | length | 12px 0 0 | |
--c2-barcode-scanner__controls--gap | length | 12px | |
--c2-barcode-scanner__switch--size | pixel | 40px | The light and Switch camera buttons. |
--c2-barcode-scanner__torch__on--background | background | #fde047 | The light button while the light is on. |
--c2-barcode-scanner__torch__on--color | color | #18181b | |
--c2-barcode-scanner__button--background | background | #ffffff | |
--c2-barcode-scanner__button--color | color | #18181b | |
--c2-barcode-scanner__button--border | border | 1px solid #bcbcc6 | |
--c2-barcode-scanner__button--border-radius | border-radius | 6px | |
--c2-barcode-scanner__button--height | pixel | 36px | |
--c2-barcode-scanner__button--font-size | font-size | 14px | |
--c2-barcode-scanner__button--font-weight | font-weight | 500 | |
--c2-barcode-scanner__button__hover--background | background | #f4f4f5 | |
--c2-barcode-scanner__accept--background | background | rgb(2, 101, 220) | The Start camera and Scan again buttons. |
--c2-barcode-scanner__accept--color | color | #ffffff | |
--c2-barcode-scanner__accept__hover--background | background | rgb(1, 84, 184) | |
--c2-barcode-scanner__focus--outline | outline | 2px solid rgba(2, 101, 220, 0.4) | |
Text and languages
The text the component shows, in English. It shows another language when the app adds one with addTranslations, under barcode-scanner, and the nearest lang asks for it; any message not translated stays in English. {0}, {1}… stand for the values a message carries. Languages guide.
| Key | English | Values |
|---|---|---|
label | Barcode scanner | — |
idle | Scan a barcode or a QR code with your camera. | — |
start | Start camera | — |
starting | Starting the camera… | — |
hint | Point the camera at a code. | — |
scanned | Scanned: {0} | {0} value |
scanAgain | Scan again | — |
torch | Light | — |
switchCamera | Switch camera | — |
errorDenied | Camera access was denied. Allow it in the browser settings, then try again. | — |
errorNotFound | No camera was found on this device. | — |
errorBusy | The camera is in use by another application. | — |
errorUnsupported | This browser cannot use a camera on this page. | — |
errorEnded | The camera stopped. Check that it is connected, then try again. | — |
errorDecoder | Barcodes cannot be read in this browser. | — |
errorUnknown | The camera could not start. | — |
retry | Try again | — |