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>
123 lines
3.9 KiB
TypeScript
123 lines
3.9 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
||
QR CODE
|
||
|
||
A QR code as an <svg>, sized by its container.
|
||
|
||
`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'
|
||
|
||
import DoveMark from './DoveMark.tsx'
|
||
import { qrMatrix, qrPath } from '../lib/qr.ts'
|
||
|
||
/* The spec's minimum quiet zone, in modules. Less and some
|
||
scanners can't find the edge of the code. */
|
||
const MARGIN = 4
|
||
|
||
/* Dark enough to scan as black; the site's deep teal. */
|
||
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
|
||
|
||
/* 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,
|
||
centre = null,
|
||
ink = QR_INK,
|
||
paper = QR_PAPER,
|
||
label,
|
||
ref,
|
||
className,
|
||
}: {
|
||
value: string
|
||
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 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
|
||
const badge = matrix.size * BADGE
|
||
const badgeAt = (total - badge) / 2
|
||
const doveWidth = badge * 0.78
|
||
const doveHeight = doveWidth / DOVE_ASPECT
|
||
const imageSize = badge * 0.8
|
||
|
||
return (
|
||
<svg
|
||
ref={ref}
|
||
xmlns="http://www.w3.org/2000/svg"
|
||
viewBox={`0 0 ${total} ${total}`}
|
||
role="img"
|
||
aria-label={label}
|
||
className={className}
|
||
shapeRendering="crispEdges"
|
||
>
|
||
<rect width={total} height={total} fill={paper} />
|
||
<path d={path} fill={ink} />
|
||
|
||
{centre && (
|
||
<g shapeRendering="auto">
|
||
<rect
|
||
x={badgeAt}
|
||
y={badgeAt}
|
||
width={badge}
|
||
height={badge}
|
||
rx={badge * 0.22}
|
||
fill={paper}
|
||
stroke={ink}
|
||
strokeWidth={0.35}
|
||
/>
|
||
{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>
|
||
)
|
||
}
|