qrcode
import { lucide, qrcode } from '@markless/ui';export default function QrcodeBasic() @{ const logo = false; <qrcode.root class="qr" value="https://markless.dev" aria-label="Scan to open markless.dev"> <qrcode.frame class="qr-frame"> <qrcode.patternsvg class="qr-pattern"> <qrcode.patternpath fill="currentColor" /> </qrcode.patternsvg> </qrcode.frame> <qrcode.overlay class="qr-logo" hidden={!logo}> <lucide.coffee aria-hidden="true" /> </qrcode.overlay> </qrcode.root>;}
.qr { position: relative; width: min(12rem, 100%);}.qr-frame { padding: 1rem; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink);}.qr-pattern { display: block; width: 100%; height: auto; aspect-ratio: 1; shape-rendering: crispEdges;}.qr-logo { position: absolute; inset: 50% auto auto 50%; display: grid; place-items: center; width: 2.6rem; height: 2.6rem; translate: -50% -50%; border: 1px solid var(--ink); border-radius: 999px; background: color-mix(in srgb, var(--purple) 26%, var(--cream)); color: var(--ink); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.qr-logo[hidden] { display: none;}.qr-logo svg { width: 1.3rem; height: 1.3rem;}
qrcode draws a string as a QR code. Use it for a link a phone can scan, a pairing code, or a
two-factor setup secret.
import { qrcode } from '@markless/ui';
export default function Scan() @{
<qrcode.root value="https://markless.dev" aria-label="Scan to open the site">
<qrcode.frame>
<qrcode.patternsvg width="160" height="160">
<qrcode.patternpath fill="currentColor" />
</qrcode.patternsvg>
</qrcode.frame>
</qrcode.root>
}The family encodes the code while it renders. So HTML from a server already holds the finished
path. If value changes later, the pattern changes with it.
Anatomy
The code itself; the frame, pattern and any overlay go inside it.
qrcode.rootis a<div>withrole="img". It holds the value and the recovery level.qrcode.frameis a<div>around the code.qrcode.patternsvgis the<svg>. One unit of itsviewBoxis one square of the code.qrcode.patternpathis one<path>that draws every dark square.qrcode.overlayis a<div>for a logo in the middle of the code.
This family writes no ui-* attributes. Nothing about a code changes while a person uses the page.
Size and colour
The whole code is one <path>, so fill sets its colour. The width and height you give
qrcode.patternsvg, or your CSS, set its size. The viewBox holds the squares alone, with no
margin.
A scanner needs a light border around the code to find it. Give qrcode.frame light padding of
about four squares on each side.
.frame {
padding: 1rem;
background: white;
color: black;
}Name it for what scanning does
The root writes no aria-label of its own. Write one that says what scanning does, such as "Scan
to open the site". Do not name it after the string it holds. For a two-factor secret, that name
reads the secret out loud. The tests check that the value never shows up in the code's own elements.
role="img" makes everything inside the root decoration to a screen reader. So put nothing a
person needs to hear in qrcode.overlay.
Props on qrcode.root
| Prop | Type | What it does |
|---|---|---|
value |
string |
The text the code carries, often a URL. Required. |
recovery |
'low' | 'medium' | 'quartile' | 'high' |
How much of the code can be covered and still scan: 7%, 15%, 25% or 30%. |
recovery defaults to 'medium'. A string too long for the largest QR code throws an error. The
family does not cut it short. The root passes anything a <div> accepts to its element.