Skip to content

Customisation

Every colour, radius and dimension is a CSS custom property, the reticle and the messages can be replaced with render fragments, extra content can be added to the control bar and over the video, and every text the scanner shows or announces can be translated. Nothing needs a selector override.

Live
Ready

Theme

Render fragments

The Overlay fragment replaces the reticle entirely and receives the scanner itself, so a custom overlay can read the status, the open camera and the current zoom without extra plumbing - the instruction overlay shows the live status.

MessageTemplate renders the status and error messages - "Requesting camera access...", a failure - from the text it is given. ControlsContent is appended to the built-in control bar, so it inherits its layout; give its buttons the bit-bbscanner__button class to match. ChildContent is drawn on top of everything else.

Language

Every label, status, notice, error message and screen reader announcement comes from Texts, a BbScannerTexts; anything a translation leaves out stays English. The layout follows the document's direction, so a right-to-left language needs nothing more than dir="rtl" on the page or an ancestor - as the wrapper here does for Persian.

Generated CSS

What the theme controls set, passed as the scanner's style attribute. The same declarations work in a stylesheet rule for the class given in CssClass.

--bit-bbscanner-accent:#4fa3f4;
--bit-bbscanner-reticle-color:#4fa3f4;
--bit-bbscanner-radius:0.75rem;
--bit-bbscanner-reticle-thickness:3px;
--bit-bbscanner-reticle-length:14%;
--bit-bbscanner-overlay:rgba(6,8,12,0.55)

Every custom property

PropertyDefaultControls
--bit-bbscanner-radius0.75remCorner radius of the viewport
--bit-bbscanner-bg#0b0d12Backdrop behind the video
--bit-bbscanner-fg#f5f7faText colour
--bit-bbscanner-muted#9aa4b2Secondary text and idle indicator
--bit-bbscanner-accent#4f8cffActive controls and starting state
--bit-bbscanner-danger#ff5f57Faulted state
--bit-bbscanner-success#35c77cScanning state and result highlight
--bit-bbscanner-overlayrgba(6,8,12,0.55)Dimming outside the scan region
--bit-bbscanner-reticle-color#ffffffReticle corners
--bit-bbscanner-reticle-thickness3pxReticle stroke width
--bit-bbscanner-reticle-length14%Reticle corner arm length
--bit-bbscanner-reticle-radius0.5remReticle corner rounding
--bit-bbscanner-control-bgrgba(18,22,30,0.72)Control background
--bit-bbscanner-control-fg#f5f7faControl text
--bit-bbscanner-control-hover-bgrgba(40,48,62,0.9)Control hover background
--bit-bbscanner-focus-ring0 0 0 3px …Keyboard focus indicator
--bit-bbscanner-fontsystem-ui, …Font stack
--bit-bbscanner-gap0.5remSpacing inside the control bars
--bit-bbscanner-transition160ms easeTransition timing

The defaults are declared in a zero-specificity :where(.bit-bbscanner) block, so any rule an application writes wins, whatever its order. This site tints the accent, the focus ring, the radius and the font of its scanners with one rule on .bit-bbscanner; the style attribute of the scanner above outranks that rule in turn. To theme one scanner from a stylesheet:

/* CssClass="my-scanner" on the component */
.my-scanner {
    --bit-bbscanner-accent: #35c77c;
    --bit-bbscanner-reticle-color: #35c77c;
    --bit-bbscanner-radius: 0;
}

Parts and states

For anything the custom properties do not reach, every part has a stable class, and the root element says what state it is in. CssClass adds classes to the root, and any other attribute - style, id, data-* - is passed through to it, a class or style merged with the component's own.

SelectorWhat it is
.bit-bbscannerThe root: a region named by AriaLabel, keeping the camera's aspect ratio unless Fill is set
[data-status]On the root: idle, starting, scanning, paused, stopped or faulted
[data-fill]On the root: contain under Fill, aspect otherwise
[data-drop="true"]On the root while an image file is dragged over it
.bit-bbscanner__videoThe camera preview; [data-mirrored="true"] for a front camera
.bit-bbscanner__maskThe dimmed surround outside the region
.bit-bbscanner__reticle, __cornerThe aiming frame and its four corners (--tl, --tr, --bl, --br)
.bit-bbscanner__hitThe outline round a decoded symbol
.bit-bbscanner__status, __dotThe status bar and its coloured state dot
.bit-bbscanner__messageA status or error message; --error for a failure
.bit-bbscanner__controlsThe control bar, holding __button, __select (camera) and __range (zoom)

A translation

Set the texts a language needs and leave the rest; one instance per language can be shared by every scanner. Texts that carry a value are functions, so word order, number formatting and plurals stay the translator's - UnnamedCamera, ImageTooLarge, ErrorMessage, which usually switches on the error's Kind, and ScanAnnouncement, what a screen reader says for a scan. The strings can as well come from an IStringLocalizer.

<BbScanner Texts="German" />

@code {
    private static readonly BbScannerTexts German = new()
    {
        StartButton = "Starten",
        StopButton = "Beenden",
        StatusScanning = "Scannen",
        NoBarcodeFound = "Kein Barcode im Bild gefunden.",
        ErrorMessage = e => e.Kind switch
        {
            BbScannerErrorKind.PermissionDenied => "Der Zugriff auf die Kamera wurde verweigert.",
            _ => "Die Kamera konnte nicht gestartet werden.",
        },
        ScanAnnouncement = r => $"{r.Format} gelesen: {r.Text}",
    };
}

Accessibility and direction

The scanner is a region named by AriaLabel. Its controls are real buttons, a select and a range input, reachable and operable from the keyboard with a visible focus ring; the torch button reports its pressed state, and Start and Stop are one button, so stopping from the keyboard leaves focus where it was. A polite live region announces the camera opening, every reported scan and the outcome of a still image; an alert announces a failure. Frames are never announced, and colour is never the only sign of a state: the status bar spells it out. The layout uses logical properties throughout, so a right-to-left document needs no extra work. Animation is suppressed under prefers-reduced-motion, and the reticle and controls switch to system colours under forced-colors.

An unhandled error has occurred. Reload 🗙