QR Code¶
A QR code of a short text, such as a URL or a short config file, drawn as SVG on the surface in a rounded style: a round dot in the text color for each dark module, each small alignment square drawn solid in the text color, and the three corner eyes as rounded squares in the accent color. "Copy image" and "Download" give it as a PNG, and the text sits under it as a Copy Field.
Decision¶
| When | Use |
|---|---|
| Handing a link or a short config to a phone, such as an invite, a pairing address, or a VPN profile | QR Code. |
| A secret | Not the QR Code. Anyone who sees the screen can scan it. |
Usage¶
import { createQrCode } from './configurationpage?name=myplugin_stolon.js';
var qr = createQrCode({ value: inviteUrl, label: 'QR code for the invite link' });
view.querySelector('#qrHost').appendChild(qr.element);
The handle is { element, getValue, setValue, toBlob, destroy }. toBlob() resolves to a PNG of the code, or null.
States and Modifiers¶
- The encoder is part of Stolon, so it needs no library. It uses byte mode at error correction level M, versions 1 to 15, which hold up to 412 bytes, enough for a URL or a short config file.
- A longer text shows "This text is too long for a QR code." under the frame, which
tooLongTextreplaces, and hides the code. - Copy Image copies a PNG to the clipboard where the browser allows it, and its label shows "Copied" or "Copy failed." for
feedbackMs, 1500 milliseconds by default.copyImageText,downloadText,copiedText, andcopyFailedTextreplace the labels. Download savesfileName("qr-code.png"). - On a wide line the code sits on the left, with Copy Image and Download as two equal buttons over the Copy Field filling the space beside it, level with the code's bottom edge. When the component is narrower than 480px, it stacks: the code the full width, then the buttons and the text under it.
layout: 'inline'draws only the text as a Copy Field withwrap, so a long link shows whole over as many lines as it needs, with Copy and a QR button level with its first line. The button, named byshowText("Show QR code"), opens the full code with Copy Image and Download in a small dialog titleddialogTitle("QR code").open()does the same from code. A text too long to encode disables the button and shows the message under the field.- The PNG takes the shapes and colors the code shows on the page, drawn at 12 pixels per module so the dots stay smooth.
- The eyes take
--stolon-accent. A page can restyle them with.stolon-qr-code-eyes { fill: ... }, and the PNG follows. showValue: falsehides the Copy Field.- The frame is an image named by
label, or "QR code for" and the text.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createQrCode¶
function createQrCode(options?: QrCodeOptions): QrCodeHandle
With layout 'inline' it draws only the text as a wrapped Copy Field that shows all of it, with a QR button beside Copy that opens the full code, with its Copy image and Download, in a dialog. A text too long to encode disables that button and shows the message.
| Option | Type | What it does |
|---|---|---|
value |
string |
the text the code holds, such as a URL [optional] |
layout |
'full' | 'inline' |
'full', the code with its buttons and text, or 'inline', the text with a QR button that opens the code in a dialog (default 'full') |
showText |
string |
the inline QR button's title and aria-label (default 'Show QR code') |
dialogTitle |
string |
the inline dialog's title (default 'QR code') |
label |
string |
the code's accessible name (default 'QR code for' and the text) |
showValue |
boolean |
false hides the Copy Field under the code (default true) |
showActions |
boolean |
false hides Copy image and Download, for a page that offers its own download (default true) |
fileName |
string |
the name of the downloaded PNG (default 'qr-code.png') |
copyImageText |
string |
the copy button's label (default 'Copy image') |
downloadText |
string |
the download button's label (default 'Download') |
copiedText |
string |
the feedback after a copy (default 'Copied') |
copyFailedText |
string |
the feedback when the browser refused the copy (default 'Copy failed.') |
tooLongText |
string |
the message for a text too long to encode (default 'This text is too long for a QR code.') |
feedbackMs |
number |
how long the feedback stays (default 1500) |
| Handle member | Type |
|---|---|
getValue |
(): string |
open |
(): DialogHandle | null |
setValue |
(value: string | null): void |
toBlob |
(): Promise<Blob | null> |
destroy |
(): void |
| Class | What it looks like |
|---|---|
stolon-qr-code.stolon-compact |
A narrow QR code stacks: the code the full width, then the buttons and the text under it at the same width |
stolon-qr-code.stolon-qr-code-dialog |
The code in its dialog: stacked, a code-sized column centered in the dialog |
stolon-qr-code.stolon-inline |
An inline QR code: its whole text as a wrapped Copy Field with a QR button beside Copy that opens the code in a dialog |
stolon-qr-code-actions |
The Copy Image and Download buttons, two equal halves of a button row the code's width, a field tall like the Copy Field under them |
Example¶
Example Code¶
<!-- title: QR Code -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
document.getElementById('mount').appendChild(base.createQrCode({ value: 'https://jellyfin.example.com/web/#/details?id=4b1e0c2a', label: 'QR code for the invite link' }).element);
const inline = document.createElement('div');
inline.style.marginTop = '24px';
document.getElementById('mount').after(inline);
inline.appendChild(base.createQrCode({ value: 'https://jellyfin.example.com/web/#/details?id=4b1e0c2a', label: 'Invite link', layout: 'inline' }).element);
</script>