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>;}

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

qrcode anatomy
Anatomy perspectiveFlat
Layers
M
qrcode.root

The code itself; the frame, pattern and any overlay go inside it.

  • qrcode.root is a <div> with role="img". It holds the value and the recovery level.
  • qrcode.frame is a <div> around the code.
  • qrcode.patternsvg is the <svg>. One unit of its viewBox is one square of the code.
  • qrcode.patternpath is one <path> that draws every dark square.
  • qrcode.overlay is 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.