The suggested grid as the theme defaults, light and dark, with PALETTE, the accents (olive becomes amber), the backdrop shades and every literal from the old grid mapped over. Every text pair passes the admin's 4.5:1 check; the hero's filled button is amber. 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.
|
||
|
||
`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 = '#00748a'
|
||
|
||
/* 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>
|
||
)
|
||
}
|