QRCode
QRCode turns a text or an URL into a QR code, as an SVG rendered on the server or on a canvas, with an optional logo in the center, rounded or dotted modules, a status overlay for expired codes and PNG or SVG downloads. It has no dependency.
Import #
import { QRCode } from '@exhaustive-ui/core/qrcode';Basic #
QRCode encodes its value, a text or an URL, into a QR code. The encoder picks the most compact mode, numeric, alphanumeric or UTF-8 bytes, and the smallest version that fits. The code is an inline SVG by default, crisp at any zoom and rendered on the server; type set to canvas draws it on a canvas in the browser instead. size is the width in pixels and margin the quiet zone around the code, in modules.
Error Correction #
errorCorrectionLevel is the share of the code that can be damaged, dirty or hidden and still be read: L about 7%, M 15%, the default, Q 25% and H 30%. A higher level makes a denser code with smaller modules. When the data leaves room in the same version, the encoder raises the level for free.
Colors #
foreground colors the dark modules and background the light modules and the quiet zone; both accept any CSS color, design token variables included. They default to the qrcode.foreground and qrcode.background design tokens.
The default stays dark modules on a light background in dark mode too: a dark code on a white square. Many scanners, and the camera apps of older phones, cannot read an inverted code with light modules on a dark background. Keep a strong contrast between the two colors, the foreground the darker one, when you change them.
Icon #
icon displays an image, such as a logo, in the center of the code. The modules behind it are removed rather than covered, and the error correction level is always H with an icon: the hidden part is rebuilt from the redundancy of the code. iconSize sets its width in pixels, 22% of the size by default and 30% at most, so that the code stays readable.
An icon template replaces the image with any content, for example an svg icon. It is displayed over the code, but not included in the exported images.
Styles #
moduleStyle shapes the data modules: square, the default, rounded, which rounds the corners that touch no other module, or dots. finderStyle set to rounded rounds the three finder patterns in the corners. Square modules are drawn with crisp edges.
Status #
status covers the code with an overlay: loading with a spinner, expired with a refresh button that emits onRefresh, and scanned with a check mark. active, the default, shows the code. A status template replaces the content of the overlay and receives the status and a refresh function.
Download #
download(filename, type, size) saves the code as a PNG or SVG file and toDataURL(type, size) returns it as a data URL; both return a promise. The PNG is size pixels wide, the size of the component by default. The files have the colors displayed on the page and the icon image when its server allows it, the same origin or CORS; the icon template is not exported. They are only available in the browser.
Accessibility #
Screen Reader
The code, an SVG or a canvas, has the img role and the accessible name "QR code", the qrcode text of labels. ariaLabel replaces it, for example with what the code is for, and ariaLabelledBy refers to a visible caption. includeValueInLabel adds the encoded value to the name, "QR code: https://exhaustive-ui.com", so that a screen reader user gets the link without scanning; leave it off for a secret such as an authenticator key, or for a long value.
The status overlay is announced: a polite live region reads "Loading", "QR code expired" or "Scanned" when the status changes, the code describes itself with the same text through aria-describedby and is marked aria-busy while loading. The refresh button of an expired code is labelled by its visible text. When it disappears with the status, the focus moves to the code instead of being lost. The icon and the spinner are hidden from assistive technologies. Every text can be translated through labels.
The code keeps dark modules on a light background in dark mode, the contrast scanners need; the overlay follows the color scheme. Its fade in is disabled when the user prefers reduced motion.
<p-qr-code value="https://exhaustive-ui.com" [includeValueInLabel]="true" />
<p-qr-code [value]="otpauthUrl" ariaLabel="Scan with your authenticator app" />
<p-qr-code [value]="url" status="expired" [labels]="{ qrcode: 'Code QR', expired: 'Code expiré', refresh: 'Actualiser' }" />Keyboard Support
The code itself is not focusable. The refresh button of an expired code is in the tab order.
| Key | Function |
|---|---|
| tab | Moves focus to the refresh button of an expired code. |
| enter / space | Activates the refresh button, which emits onRefresh. |