Add a QR code page at /qr for any page on the site
Pick "Main navigation" or a kind of record (events, organizations, teams, awards, people), then the page or record, and for a main page optionally one of its #sections. The code, a copy-link button, and PNG (1024px) and SVG downloads follow. An "NGU dove in the centre" option puts the dove on a white badge over the code and raises error correction to H so it still scans. Codes always point at SITE_ORIGIN (https://nextgenerationofunity.org, in src/lib/site.ts), so one made on localhost works on a poster. Main pages come from navConfig.ts and record URLs from hrefs.ts, the same sources the site links with. GET /api/directory lists every published record with a page, as id, name and (for organizations) kind, each filtered by the same rule as that record's own page: a team also needs its organization published. /api/people only answers for ids it's given, so there was no public list to read. The `qrcode` package does the encoding; QrCode.tsx draws the modules as one SVG path, so the preview and both downloads are one picture. The page is lazy-loaded so the encoder isn't in the main bundle. The page is unlisted: not in navConfig, so not in the header or footer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
8c26ec4906
commit
97c1827ca9
11 changed files with 892 additions and 0 deletions
94
src/components/QrCode.tsx
Normal file
94
src/components/QrCode.tsx
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
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.
|
||||
|
||||
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.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
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. */
|
||||
const INK = '#04262e'
|
||||
const TEAL = '#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 default function QrCode({
|
||||
value,
|
||||
dove = false,
|
||||
label,
|
||||
ref,
|
||||
className,
|
||||
}: {
|
||||
value: string
|
||||
dove?: boolean
|
||||
/** 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 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
|
||||
|
||||
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="#ffffff" />
|
||||
<path d={path} fill={INK} />
|
||||
|
||||
{dove && (
|
||||
<g shapeRendering="auto">
|
||||
<rect
|
||||
x={badgeAt}
|
||||
y={badgeAt}
|
||||
width={badge}
|
||||
height={badge}
|
||||
rx={badge * 0.22}
|
||||
fill="#ffffff"
|
||||
stroke={INK}
|
||||
strokeWidth={0.35}
|
||||
/>
|
||||
<DoveMark
|
||||
x={(total - doveWidth) / 2}
|
||||
y={(total - doveHeight) / 2}
|
||||
width={doveWidth}
|
||||
height={doveHeight}
|
||||
style={{ color: TEAL }}
|
||||
/>
|
||||
</g>
|
||||
)}
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue