QR tool: choose the centre, type any link, pick the colours

The centre can be nothing, the NGU dove, or any SVG dropped into
src/assets/qr, embedded in the code so downloads carry it. "A link I
type" takes a site path (sent to the live site), a full http(s)
address, or a bare domain. The code, background and dove colours can
be changed, with a warning under 4:1 contrast or for an inverted
code, which many scanners can't read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-27 23:20:55 -05:00
parent 9075cfb908
commit 9eeb13c5ec
5 changed files with 279 additions and 51 deletions

View file

@ -1,16 +1,25 @@
/* ═══════════════════════════════════════════════════════════════
QR CODE
A QR code as an <svg>, sized by its container. With `dove`, the
NGU dove sits on a white badge over the centre; the code switches
to high error correction so the modules under the badge are
recovered when it's scanned (see lib/qr.ts). The badge covers
under 6% of the code, well inside what 'H' repairs, and never
reaches the three corner finder patterns.
A QR code as an <svg>, sized by its container.
Everything is drawn in the SVG itself (no CSS, no external
references), so the element serialises to a file that looks the
same outside the page. `ref` reaches the <svg> for exactly that.
`centre` puts something on a badge over the middle: the NGU dove,
or an image (an SVG from src/assets/qr, passed as a data: URL).
With anything there, the code switches to high error correction
so the modules under the badge are recovered when it's scanned
(see lib/qr.ts). The badge covers under 6% of the code, well
inside what 'H' repairs, and never reaches the three corner
finder patterns.
`ink` is the modules and the badge's outline, `paper` the
background and the badge. A code should stay dark on light with
strong contrast to scan reliably; QrCodeMaker warns when it
doesn't, this just draws what it's given.
Everything is drawn in the SVG itself (no CSS, and an image is
embedded rather than linked), so the element serialises to a
file that looks the same outside the page. `ref` reaches the
<svg> for exactly that.
═══════════════════════════════════════════════════════════════ */
import { useMemo, type Ref } from 'react'
@ -23,8 +32,9 @@ import { qrMatrix, qrPath } from '../lib/qr.ts'
const MARGIN = 4
/* Dark enough to scan as black; the site's deep teal. */
const INK = '#04262e'
const TEAL = '#138ba0'
export const QR_INK = '#04262e'
export const QR_PAPER = '#ffffff'
export const QR_DOVE = '#138ba0'
/* Badge side as a share of the code's width. */
const BADGE = 0.24
@ -32,21 +42,28 @@ const BADGE = 0.24
/* DoveMark's own viewBox is 72.87 × 48.57. */
const DOVE_ASPECT = 72.867699 / 48.568241
export type QrCentre = { kind: 'dove'; color?: string } | { kind: 'image'; src: string } | null
export default function QrCode({
value,
dove = false,
centre = null,
ink = QR_INK,
paper = QR_PAPER,
label,
ref,
className,
}: {
value: string
dove?: boolean
centre?: QrCentre
ink?: string
paper?: string
/** Accessible name for the image, e.g. "QR code for /retreats". */
label: string
ref?: Ref<SVGSVGElement>
className?: string
}) {
const matrix = useMemo(() => qrMatrix(value, dove ? 'H' : 'M'), [value, dove])
const level = centre ? 'H' : 'M'
const matrix = useMemo(() => qrMatrix(value, level), [value, level])
const path = useMemo(() => qrPath(matrix, MARGIN), [matrix])
const total = matrix.size + MARGIN * 2
@ -54,6 +71,7 @@ export default function QrCode({
const badgeAt = (total - badge) / 2
const doveWidth = badge * 0.78
const doveHeight = doveWidth / DOVE_ASPECT
const imageSize = badge * 0.8
return (
<svg
@ -65,10 +83,10 @@ export default function QrCode({
className={className}
shapeRendering="crispEdges"
>
<rect width={total} height={total} fill="#ffffff" />
<path d={path} fill={INK} />
<rect width={total} height={total} fill={paper} />
<path d={path} fill={ink} />
{dove && (
{centre && (
<g shapeRendering="auto">
<rect
x={badgeAt}
@ -76,17 +94,28 @@ export default function QrCode({
width={badge}
height={badge}
rx={badge * 0.22}
fill="#ffffff"
stroke={INK}
fill={paper}
stroke={ink}
strokeWidth={0.35}
/>
<DoveMark
x={(total - doveWidth) / 2}
y={(total - doveHeight) / 2}
width={doveWidth}
height={doveHeight}
style={{ color: TEAL }}
/>
{centre.kind === 'dove' ? (
<DoveMark
x={(total - doveWidth) / 2}
y={(total - doveHeight) / 2}
width={doveWidth}
height={doveHeight}
style={{ color: centre.color ?? QR_DOVE }}
/>
) : (
<image
href={centre.src}
x={(total - imageSize) / 2}
y={(total - imageSize) / 2}
width={imageSize}
height={imageSize}
preserveAspectRatio="xMidYMid meet"
/>
)}
</g>
)}
</svg>