# Angular QRCode Component

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.

## 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.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';
import { QRCodeModule } from '@exhaustive-ui/core/qrcode';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <p-qr-code [value]="value || '-'" />
            <div class="flex flex-col gap-2 w-full max-w-80">
                <label for="qrcode-basic-value">Value</label>
                <input id="qrcode-basic-value" pInputText [(ngModel)]="value" maxlength="200" />
            </div>
        </div>
    `,
    standalone: true,
    imports: [QRCodeModule, InputTextModule, FormsModule]
})
export class QrcodeBasicDemo {
    value = 'https://exhaustive-ui.com';
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { QRCodeErrorCorrectionLevel, QRCodeModule } from '@exhaustive-ui/core/qrcode';
import { SelectButtonModule } from '@exhaustive-ui/core/selectbutton';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <div class="flex flex-col items-center gap-2">
                <span id="qrcode-errorcorrection-label" class="font-medium">Error correction level</span>
                <p-select-button [options]="levels" [(ngModel)]="level" [allowEmpty]="false" ariaLabelledBy="qrcode-errorcorrection-label" />
            </div>
            <p-qr-code value="https://exhaustive-ui.com/qrcode/" [errorCorrectionLevel]="level" />
        </div>
    `,
    standalone: true,
    imports: [QRCodeModule, SelectButtonModule, FormsModule]
})
export class QrcodeErrorcorrectionDemo {
    level: QRCodeErrorCorrectionLevel = 'M';

    levels: QRCodeErrorCorrectionLevel[] = ['L', 'M', 'Q', 'H'];
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { QRCodeModule } from '@exhaustive-ui/core/qrcode';

@Component({
    template: `
        <div class="card flex flex-wrap justify-center gap-6">
            <p-qr-code value="https://exhaustive-ui.com" foreground="var(--p-primary-700)" />
            <p-qr-code value="https://exhaustive-ui.com" foreground="#1e3a8a" background="#dbeafe" />
        </div>
    `,
    standalone: true,
    imports: [QRCodeModule]
})
export class QrcodeColorsDemo {
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { QRCodeModule } from '@exhaustive-ui/core/qrcode';
import { SvgIcon } from '@exhaustive-ui/core/icon';
import { heartSolid } from '@exhaustive-ui/icons/round';

@Component({
    template: `
        <div class="card flex flex-wrap justify-center gap-6">
            <p-qr-code value="https://exhaustive-ui.com" icon="/logo-icon.svg" />
            <p-qr-code value="https://exhaustive-ui.com" [iconSize]="36">
                <ng-template #icon>
                    <svg pIcon [icon]="heartSolid" style="font-size: 2rem; color: #000000"></svg>
                </ng-template>
            </p-qr-code>
        </div>
    `,
    standalone: true,
    imports: [QRCodeModule, SvgIcon]
})
export class QrcodeIconDemo {
    heartSolid = heartSolid;
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { QRCodeModule } from '@exhaustive-ui/core/qrcode';

@Component({
    template: `
        <div class="card flex flex-wrap justify-center gap-6">
            <p-qr-code value="https://exhaustive-ui.com" moduleStyle="rounded" finderStyle="rounded" ariaLabel="QR code with rounded modules" />
            <p-qr-code value="https://exhaustive-ui.com" moduleStyle="dots" finderStyle="rounded" ariaLabel="QR code with dots" />
            <p-qr-code value="https://exhaustive-ui.com" moduleStyle="dots" type="canvas" ariaLabel="QR code drawn on a canvas" />
        </div>
    `,
    standalone: true,
    imports: [QRCodeModule]
})
export class QrcodeStylesDemo {
}
```

## 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.

```typescript
import { Component, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { QRCodeModule, QRCodeStatus } from '@exhaustive-ui/core/qrcode';
import { SelectButtonModule } from '@exhaustive-ui/core/selectbutton';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <div class="flex flex-col items-center gap-2">
                <span id="qrcode-status-label" class="font-medium">Status</span>
                <p-select-button [options]="statuses" [ngModel]="state()" (ngModelChange)="state.set($event)" [allowEmpty]="false" ariaLabelledBy="qrcode-status-label" />
            </div>
            <div class="flex flex-wrap justify-center gap-6">
                <p-qr-code [value]="value()" [status]="state()" (onRefresh)="refresh()" />
                <p-qr-code [value]="value()" [status]="state()" (onRefresh)="refresh()">
                    <ng-template #status let-status let-refresh="refresh">
                        @if (status === 'expired') {
                            <button type="button" class="p-2 rounded-border border border-surface bg-transparent cursor-pointer text-color" (click)="refresh($event)">Get a new code</button>
                        } @else {
                            <span class="capitalize">{{ status }}</span>
                        }
                    </ng-template>
                </p-qr-code>
            </div>
        </div>
    `,
    standalone: true,
    imports: [QRCodeModule, SelectButtonModule, FormsModule]
})
export class QrcodeStatusDemo {
    state = signal<QRCodeStatus>('expired');

    statuses: QRCodeStatus[] = ['active', 'loading', 'expired', 'scanned'];

    value = signal('https://exhaustive-ui.com/?session=1');

    private session = 1;

    refresh() {
        this.state.set('loading');
        setTimeout(() => {
            this.value.set(`https://exhaustive-ui.com/?session=${++this.session}`);
            this.state.set('active');
        }, 1200);
    }
}
```

## 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.

```typescript
import { Component } from '@angular/core';
import { ButtonModule } from '@exhaustive-ui/core/button';
import { QRCodeModule } from '@exhaustive-ui/core/qrcode';
import { download as downloadIcon } from '@exhaustive-ui/icons/round';

@Component({
    template: `
        <div class="card flex flex-col items-center gap-4">
            <p-qr-code #qrcode value="https://exhaustive-ui.com" icon="/logo-icon.svg" moduleStyle="rounded" finderStyle="rounded" />
            <div class="flex flex-wrap justify-center gap-2">
                <p-button label="PNG" [icon]="downloadIcon" (onClick)="qrcode.download('exhaustive-ui', 'png', 512)" />
                <p-button label="SVG" [icon]="downloadIcon" severity="secondary" (onClick)="qrcode.download('exhaustive-ui', 'svg')" />
            </div>
        </div>
    `,
    standalone: true,
    imports: [QRCodeModule, ButtonModule]
})
export class QrcodeDownloadDemo {
    downloadIcon = downloadIcon;
}
```

## 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.

## Pass Through Options

| Name | Type | Description |
|------|------|-------------|
| host | PassThroughOption<HTMLElement, I> | Used to pass attributes to the host's DOM element. |
| root | PassThroughOption<HTMLElement, I> | Used to pass attributes to the root's DOM element, the host. |
| svg | PassThroughOption<SVGElement, I> | Used to pass attributes to the SVG element that draws the code, with the img role. |
| canvas | PassThroughOption<HTMLCanvasElement, I> | Used to pass attributes to the canvas element that draws the code in canvas mode, with the img role. |
| background | PassThroughOption<SVGRectElement, I> | Used to pass attributes to the background rectangle of the SVG. |
| modules | PassThroughOption<SVGPathElement, I> | Used to pass attributes to the path of the data modules. |
| finders | PassThroughOption<SVGPathElement, I> | Used to pass attributes to the path of the three finder patterns. |
| image | PassThroughOption<SVGImageElement, I> | Used to pass attributes to the SVG image element of the icon, when the icon is an URL. |
| icon | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the element that holds the icon template. |
| overlay | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the status overlay's DOM element. |
| statusIcon | PassThroughOption<SVGElement, I> | Used to pass attributes to the status icon's DOM element. |
| statusText | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the status text's DOM element. |
| pcRefreshButton | ButtonPassThrough | Used to pass attributes to the refresh Button component. |
| liveRegion | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the live region that announces the status. |

## Theming

### CSS Classes

| Class | Description |
|-------|-------------|
| p-qrcode | Class name of the root element |
| p-qrcode-svg | Class name of the SVG element |
| p-qrcode-canvas | Class name of the canvas element |
| p-qrcode-background | Class name of the background rectangle |
| p-qrcode-modules | Class name of the data modules path |
| p-qrcode-finders | Class name of the finder patterns path |
| p-qrcode-image | Class name of the SVG image of the icon |
| p-qrcode-icon | Class name of the icon template container |
| p-qrcode-overlay | Class name of the status overlay |
| p-qrcode-status-icon | Class name of the status icon |
| p-qrcode-status-text | Class name of the status text |
| p-qrcode-refresh-button | Class name of the refresh button |

### Design Tokens

| Token | CSS Variable | Description |
|-------|--------------|-------------|
| qrcode.foreground | --p-qrcode-foreground | Color of the dark modules of root |
| qrcode.background | --p-qrcode-background | Color of the light modules and the quiet zone of root |
| qrcode.border.radius | --p-qrcode-border-radius | Border radius of root |
| qrcode.transition.duration | --p-qrcode-transition-duration | Transition duration of root |
| qrcode.icon.border.radius | --p-qrcode-icon-border-radius | Border radius of icon |
| qrcode.overlay.background | --p-qrcode-overlay-background | Background of overlay |
| qrcode.overlay.color | --p-qrcode-overlay-color | Color of overlay |
| qrcode.overlay.gap | --p-qrcode-overlay-gap | Gap of overlay |
| qrcode.overlay.padding | --p-qrcode-overlay-padding | Padding of overlay |
| qrcode.overlay.font.size | --p-qrcode-overlay-font-size | Font size of overlay |
| qrcode.overlay.font.weight | --p-qrcode-overlay-font-weight | Font weight of overlay |
| qrcode.status.icon.size | --p-qrcode-status-icon-size | Size of status icon |
| qrcode.status.icon.color | --p-qrcode-status-icon-color | Color of status icon |
| qrcode.status.icon.scanned.color | --p-qrcode-status-icon-scanned-color | Scanned color of status icon |

