NGU-Web/src/components/QrCode.tsx
Zaldimmar d81f7bd856 Adopt the new palette; square corners, superellipse buttons
The theme defaults (light and dark) come from the 16-color grid,
kept as PALETTE in src/lib/color.ts; every text pair passes the
admin's 4.5:1 check. Nothing is rounded any more except buttons,
which share one btn utility (Poppins Bold, a superellipse corner
where the browser has corner-shape) and true dots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 14:40:39 -05:00

123 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* ═══════════════════════════════════════════════════════════════
QR CODE
A QR code as an <svg>, sized by its container.
`center` 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 serializes 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 = '#001011'
export const QR_PAPER = '#ffffff'
export const QR_DOVE = '#008fa8'
/* 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 QrCenter = { kind: 'dove'; color?: string } | { kind: 'image'; src: string } | null
export default function QrCode({
value,
center = null,
ink = QR_INK,
paper = QR_PAPER,
label,
ref,
className,
}: {
value: string
center?: QrCenter
ink?: string
paper?: string
/** Accessible name for the image, e.g. "QR code for /retreats". */
label: string
ref?: Ref<SVGSVGElement>
className?: string
}) {
const level = center ? '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} />
{center && (
<g shapeRendering="auto">
<rect
x={badgeAt}
y={badgeAt}
width={badge}
height={badge}
rx={badge * 0.22}
fill={paper}
stroke={ink}
strokeWidth={0.35}
/>
{center.kind === 'dove' ? (
<DoveMark
x={(total - doveWidth) / 2}
y={(total - doveHeight) / 2}
width={doveWidth}
height={doveHeight}
style={{ color: center.color ?? QR_DOVE }}
/>
) : (
<image
href={center.src}
x={(total - imageSize) / 2}
y={(total - imageSize) / 2}
width={imageSize}
height={imageSize}
preserveAspectRatio="xMidYMid meet"
/>
)}
</g>
)}
</svg>
)
}