qr-code
QR code generator web component with progressive enhancement. Shows text content before JS, then renders a scannable QR code.
Overview
The <qr-code> component generates a QR code from text or a URL. It uses progressive enhancement: the text content is visible before JavaScript loads, then replaced with a scannable QR code canvas.
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
data-value |
string | textContent | Text or URL to encode. Falls back to element text content. |
data-size |
number | 200 |
Canvas size in pixels. |
data-color |
CSS color | currentColor | Foreground (module) color. |
data-background |
CSS color | transparent | Background color. |
data-error-correction |
0–3 | 1 (M) |
Error correction level: 0=L, 1=M, 2=Q, 3=H. |
Custom Sizes
Custom Colors
Override the foreground and background colors to match your brand.
JavaScript API
| Method | Returns | Description |
|---|---|---|
toDataURL(type?) |
string | Get the QR code as a data URL for download or embedding. |
Progressive Enhancement
Without JavaScript, the text content inside the element remains visible as a readable fallback. This means the URL or text is always accessible, even if the QR code can’t render.
Accessibility
- The canvas element gets
role="img"with anaria-labeldescribing the QR content. - The original text serves as a fallback for non-visual users.