Files
Reims-Go/frontend/src/components/WordmarkLoader.tsx
T
Nwflower f4395a1f28 refactor(brand): generate the frontend logo copy and wordmark geometry
assets/logo.svg was duplicated byte for byte in frontend/public, and WordmarkLoader hand-copied the ten wordmark strokes from the Python generator, so the icon and the lettering could drift silently.

scripts/brand/wordmark.py now writes frontend/public/logo.svg and frontend/src/components/wordmark.gen.ts, and the loader imports that geometry. The unreferenced assets/wordmark-*.svg outputs are gone, and frontend/scripts/test-brand.mjs fails when either copy is edited by hand.
2026-12-04 23:24:36 -07:00

135 lines
5.1 KiB
TypeScript

import React, { useEffect, useId, useRef } from 'react'
import {
WORDMARK_GRADIENT,
WORDMARK_ORIGIN,
WORDMARK_STROKES,
WORDMARK_STROKE_WIDTH,
WORDMARK_VIEWBOX,
} from './wordmark.gen'
// Geometry comes from scripts/brand/wordmark.py through wordmark.gen.ts: the
// strokes are in pen order, in the generator's baseline frame.
// Pen timing, in ms. Writing speed is constant, so long strokes take longer.
const PX_PER_MS = 0.62
const LIFT_MS = 70 // pen lift between strokes
const DOT_MS = 140
const HOLD_MS = 900 // finished word stays on screen
const FADE_MS = 420
const REST_MS = 260
const STROKE_EASE = 'cubic-bezier(0.45, 0.05, 0.35, 1)'
interface Props {
/** Rendered width in px; height follows the wordmark's aspect ratio. */
width?: number
label?: string
className?: string
}
/**
* The Reims-Go wordmark written stroke by stroke, on a loop. Used while
* waiting on network I/O. With reduced motion it shows the finished word
* with a slow pulse instead.
*/
export const WordmarkLoader: React.FC<Props> = ({ width = 168, label, className }) => {
const svgRef = useRef<SVGSVGElement>(null)
const gid = `wm-${useId().replace(/[^a-zA-Z0-9_-]/g, '')}`
useEffect(() => {
const svg = svgRef.current
if (!svg || typeof svg.animate !== 'function') return
if (window.matchMedia?.('(prefers-reduced-motion: reduce)').matches) return
const els = Array.from(svg.querySelectorAll<SVGGeometryElement>('[data-stroke]'))
const spans = els.map((el) => {
if (el.tagName.toLowerCase() === 'circle') return DOT_MS
const len = typeof el.getTotalLength === 'function' ? el.getTotalLength() : 60
return Math.max(90, len / PX_PER_MS)
})
const writeEnd = spans.reduce((sum, s) => sum + s, 0) + LIFT_MS * (spans.length - 1)
const total = writeEnd + HOLD_MS + FADE_MS + REST_MS
const at = (ms: number) => Math.min(1, ms / total)
const holdEnd = at(writeEnd + HOLD_MS)
const fadeEnd = at(writeEnd + HOLD_MS + FADE_MS)
let t = 0
const anims = els.map((el, i) => {
const s = at(t)
const e = at(t + spans[i])
t += spans[i] + LIFT_MS
const isDot = el.tagName.toLowerCase() === 'circle'
// Opacity hides the round cap that a fully-offset dash still paints.
const frames: Keyframe[] = isDot
? [
{ offset: 0, opacity: 0, transform: 'scale(0.2)' },
{ offset: s, opacity: 0, transform: 'scale(0.2)', easing: 'cubic-bezier(0.34, 1.56, 0.64, 1)' },
{ offset: e, opacity: 1, transform: 'scale(1)' },
{ offset: holdEnd, opacity: 1, transform: 'scale(1)' },
{ offset: fadeEnd, opacity: 0, transform: 'scale(1)' },
{ offset: 1, opacity: 0, transform: 'scale(0.2)' },
]
: [
{ offset: 0, opacity: 0, strokeDashoffset: 1 },
{ offset: s, opacity: 0, strokeDashoffset: 1 },
{ offset: Math.min(e, s + 0.0005), opacity: 1, strokeDashoffset: 1, easing: STROKE_EASE },
{ offset: e, opacity: 1, strokeDashoffset: 0 },
{ offset: holdEnd, opacity: 1, strokeDashoffset: 0 },
{ offset: fadeEnd, opacity: 0, strokeDashoffset: 0 },
{ offset: 1, opacity: 0, strokeDashoffset: 1 },
]
return el.animate(frames, { duration: total, iterations: Infinity, fill: 'both' })
})
svg.dataset.animated = 'true'
return () => {
anims.forEach((a) => a.cancel())
delete svg.dataset.animated
}
}, [])
return (
<div className={`wordmark-loader${className ? ` ${className}` : ''}`} role="status" aria-live="polite">
<svg
ref={svgRef}
className="wordmark-loader-svg"
viewBox={`0 0 ${WORDMARK_VIEWBOX.width} ${WORDMARK_VIEWBOX.height}`}
width={width}
height={Math.round((width * WORDMARK_VIEWBOX.height) / WORDMARK_VIEWBOX.width)}
aria-hidden="true"
>
<defs>
<linearGradient id={gid} gradientUnits="userSpaceOnUse" x1={WORDMARK_GRADIENT.x1} y1="0" x2={WORDMARK_GRADIENT.x2} y2="0">
{WORDMARK_GRADIENT.stops.map((color, i) => (
<stop key={color} offset={i / (WORDMARK_GRADIENT.stops.length - 1)} stopColor={color} />
))}
</linearGradient>
</defs>
<g transform={`translate(${WORDMARK_ORIGIN.x} ${WORDMARK_ORIGIN.y})`} fill="none" strokeWidth={WORDMARK_STROKE_WIDTH} strokeLinecap="round" strokeLinejoin="round">
{WORDMARK_STROKES.map((s, i) => s.kind === 'dot' ? (
<circle
key={i}
data-stroke=""
className="wordmark-loader-dot"
cx={s.cx}
cy={s.cy}
r={s.r}
style={{ transformOrigin: 'center', transformBox: 'fill-box' }}
/>
) : (
<path
key={i}
data-stroke=""
className={`wordmark-loader-${s.ink}`}
d={s.d}
pathLength={1}
strokeDasharray="1 1"
stroke={s.ink === 'brand' ? `url(#${gid})` : undefined}
/>
))}
</g>
</svg>
{label && <span className="wordmark-loader-label">{label}</span>}
</div>
)
}