0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
QR code generator web component. Draws a black-on-white code with a quiet zone from text or a URL, keeping the element's children as the readable fallback and caption.
Overview
The <qr-code> component encodes text or a URL as a QR code on a canvas. Without JavaScript, its children (plain text or a link to the same URL) are what readers see. After upgrade those children stay in the element as the code’s caption.
Basic QR code
123456
<qr-codevalue="https://vanilla-breeze.com"data-caption="visible"><ahref="https://vanilla-breeze.com">vanilla-breeze.com</a></qr-code><!-- No value attribute: the text content is encoded --><qr-codedata-caption="visible">Hello, World!</qr-code>
The text/URL to encode. Falls back to the element's original text content. Reactive; removing it falls back to the text again.
size
number
200
Edge of the drawn box in CSS pixels, quiet zone included. Modules are a whole number of device pixels. Reactive.
color
string
#000
Module colour (any CSS colour). Opt-in; logs a console warning when the pair is inverted, low-contrast or translucent. Reactive.
background
string
#fff
Background colour, always painted, including the 4-module quiet zone. Reactive.
error-correction
"l", "m", "q", "h", "0", "1", "2", "3"
m
Error-correction level: L (7%), M (15%, default), Q (25%), H (30%), or 0-3. Letters are case-insensitive (listed lower-case because html-validate matches enums lower-cased); other values warn and use M. Reactive.
data-caption
"visible"
—
Show the caption (the element's children) under the code. By default it is visually hidden but read by screen readers, and shown while a link inside it has focus. A plain-text caption that only repeats the value is hidden from screen readers while the canvas name carries the value.
data-label-prefix
string
—
Accessible name of the canvas (role=img). Without it the name is "QR code: " followed by the encoded value (cut to 200 characters with an ellipsis when longer). With it, this text is the name and the encoded value is the canvas's aria-description.
data-label-copied
string
QR code copied
Screen-reader announcement after copy() succeeds.
data-error (reflected)
"capacity", "encoding"
—
Set when no code could be drawn (the text is longer than the level holds, or cannot be encoded). The caption is then shown.
Sizes
size sets the edge of the whole box in CSS pixels, with the quiet zone included. Each module is drawn as a whole number of device pixels, and the canvas is scaled for devicePixelRatio. Any pixels left over widen the white margin. For phone scanning, keep the code at least about 2 cm across in print and 100 px on screen.
By default every code is black modules on an opaque white background with a four-module quiet zone. The theme, dark mode and forced colours do not change it, because inverted (light-on-dark) or low-contrast codes fail on many scanners. In forced-colours mode the canvas keeps its pixels and gets a CanvasText border so the edge of the box stays visible.
color and background are explicit opt-ins and accept any CSS colour. Keep the modules darker than the background. When a custom pair is inverted, below about 4:1 contrast, or translucent, the element logs a console warning.
error-correction accepts L, M, Q or H (case-insensitive), or 0–3. The default is M. Higher levels let a damaged or partly covered code still scan, but they hold less data, so the code needs more modules for the same text. The level you set is the level that is used.
Level
Recovers up to
Max bytes (UTF-8)
Max alphanumeric
Max digits
L / 0
~7%
2953
4296
7089
M / 1 (default)
~15%
2331
3391
5596
Q / 2
~25%
1663
2420
3993
H / 3
~30%
1273
1852
3057
These are the limits of the largest code (version 40, 177 modules per side). In practice keep URLs short: a code above about version 10 needs a large size to scan reliably. Alphanumeric means digits, upper-case A–Z, space and $%*+-./:. Any other text is encoded as UTF-8 bytes.
When the text does not fit, no code is drawn, so there is never a truncated or unreadable one. Instead the element sets data-error="capacity", shows its caption, and fires qr-code:error.
Error correction levels
1234567891011
<qr-codevalue="https://example.com"error-correction="L">L</qr-code><qr-codevalue="https://example.com"error-correction="H">H</qr-code><qr-codeid="qr"error-correction="H"><ahref="https://example.com/a-very-long-link">The link, as text</a></qr-code><scripttype="module">const qr = document.getElementById('qr'); qr.addEventListener('qr-code:error', (e) =>console.log(e.detail.reason)); // "capacity" qr.value = 'x'.repeat(1300); // more than H holds</script>
JavaScript API
Member
Returns
Description
value
string
The encoded text. Setting it sets the value attribute and redraws. Setting '' removes the attribute, and the element falls back to its original text content.
toDataURL(type?)
string
The code as a data URL (default image/png), opaque and with the quiet zone. Returns '' when no code is drawn.
copy(options?)
Promise<boolean>
Copies the code to the clipboard as the encoded text (text/plain) and a PNG (image/png), so it pastes as text in text fields and as an image in design and chat tools. Browsers without ClipboardItem get text only. options.button gets data-state="copied" and a copy:done event (data-state="failed" and copy:error on failure). Screen readers hear data-label-copied. Resolves false when no code is drawn or the write fails.
Copy and export
1234567891011121314
<qr-codeid="qr"value="https://vanilla-breeze.com"><ahref="https://vanilla-breeze.com">vanilla-breeze.com</a></qr-code><buttontype="button"id="copy-btn">Copy QR code</button><scripttype="module">const qr = document.getElementById('qr');const button = document.getElementById('copy-btn'); button.addEventListener('click', () => qr.copy({ button }));// Or export itconst img = newImage(); img.src = qr.toDataURL('image/png');</script>
Fired when the value cannot be encoded. No code is drawn and the caption (fallback) stays visible.
qr-code:upgraded
—
Fired once the element has upgraded (canvas and caption in place).
Progressive Enhancement
Without JavaScript, the element’s children are shown as they are. A link to the encoded URL is the best fallback, because it does what scanning would do. After upgrade the canvas is added before the children, and the children are not removed.
Accessibility
The canvas has role="img". A screen reader user cannot scan the code, so its name includes the encoded value: “QR code: https://example.com/tickets”. A value longer than 200 characters is cut there with an ellipsis. The name follows the value when it changes.
Set data-label-prefix to give the code a name of your own, for example “Ticket QR code”. The encoded value is then the canvas’s description (aria-description).
The children (the caption) are visually hidden by default and read after the image. Add data-caption="visible" to show them under the code. A plain-text caption that only repeats the encoded value is hidden from screen readers while the name carries the value, so it is read once.
A link in a hidden caption is still keyboard-reachable, and the caption is shown while the link has focus.
When no code can be drawn, the caption is shown so the content is never lost.
Colours are fixed black on white, with an opaque quiet zone, in every theme, in dark mode and in forced colours.