ExplanationCallout.tsx
src/components/ExplanationCallout.tsx
1import React, {useId} from 'react';2import {theme as t} from '../theme';3import {move} from '../motion';4import {machineFrameAt, type PauseWindow} from '../explanationTimeline';5import {Text} from './Primitives';67type Box = {x: number; y: number; width: number; height: number};8export type Explanation = PauseWindow & {9 target: Box;10 balloon: {x: number; y: number; width: number};11 title?: string;12 text?: string[];13 code?: string;14};1516const codeColor = (token: string) => token.startsWith("'") ? token.includes('#') ? t.danger : t.action17 : /^(function|if|return|null)$/.test(token) ? t.reducer18 : /^(Readonly|Balances|Action)$/.test(token) ? t.state : t.ink;1920/** A quiet annotation, distinct from operational call arrows and packet motion. */21export const ExplanationCallout: React.FC<{frame: number; explanation: Explanation}> = ({frame, explanation}) => {22 const mask = `explanation-${useId().replace(/:/g, '')}`;23 const {from, duration, target, balloon, title, text = [], code} = explanation;24 const local = frame - from, fade = Math.min(18, duration / 4);25 const show = move(local, 0, fade) * (1 - move(local, duration - fade, duration));26 if (local < 0 || local >= duration) return null;27 const lines = code?.split('\n') ?? [];28 const titleY = 43, textY = title ? 79 : 43;29 const codeY = textY + text.length * 27 + (text.length ? 11 : 0);30 const height = (lines.length ? codeY + (lines.length - 1) * 26 : textY + Math.max(0, text.length - 1) * 27) + 28;31 const rise = (1 - show) * 12;32 const anchorX = target.x + Math.min(155, target.width / 2), anchorY = target.y - 4;33 const tailX = Math.max(balloon.x + 32, Math.min(balloon.x + balloon.width - 32, anchorX));34 const bottom = balloon.y + height + rise;35 return <g opacity={show}>36 <defs><mask id={mask} maskUnits="userSpaceOnUse" x="0" y="0" width="1920" height="1080">37 <rect width="1920" height="1080" fill="white"/>38 <rect x={target.x - 9} y={target.y - 9} width={target.width + 18} height={target.height + 18} rx="19" fill="black"/>39 </mask></defs>40 <rect width="1920" height="1080" fill={t.paper} opacity="0.82" mask={`url(#${mask})`}/>41 <rect x={target.x - 9} y={target.y - 9} width={target.width + 18} height={target.height + 18} rx="19" fill="none" stroke={t.reducer} strokeWidth="1.4" opacity="0.5"/>42 <path d={`M${tailX} ${bottom + 10} L${anchorX} ${anchorY}`} fill="none" stroke={t.reducer} strokeWidth="2" strokeDasharray="4 6" strokeLinecap="round"/>43 <circle cx={anchorX} cy={anchorY} r="4" fill={t.reducer}/>44 <g transform={`translate(${balloon.x} ${balloon.y + rise})`}>45 <rect x="0" y="6" width={balloon.width} height={height} rx="18" fill={t.ink} opacity="0.06"/>46 <rect width={balloon.width} height={height} rx="18" fill={t.white} stroke={t.reducer} strokeWidth="1.6"/>47 <path d={`M${tailX - balloon.x - 10} ${height - 1} L${tailX - balloon.x} ${height + 10} L${tailX - balloon.x + 10} ${height - 1}`} fill={t.white} stroke={t.reducer} strokeWidth="1.6"/>48 {title && <Text x={26} y={titleY} size={24} weight={600}>{title}</Text>}49 {text.map((line, i) => <Text key={i} x={26} y={textY + i * 27} size={19} color={t.muted}>{line}</Text>)}50 {lines.map((line, i) => <text key={i} x={26} y={codeY + i * 26} fontFamily={t.mono} fontSize={20} xmlSpace="preserve" style={{whiteSpace: 'pre'}}>51 {line.split(/('(?:[^'\\]|\\.)*'|\b(?:function|if|return|null|Readonly|Balances|Action)\b)/g).map((token, j) => <tspan key={j} fill={codeColor(token)}>{token}</tspan>)}52 </text>)}53 </g>54 </g>;55};5657/** Freeze every animated property by giving the machinery one paused frame clock. */58export const PauseAndExplain: React.FC<{frame: number; explanations: Explanation[]; children: (machineFrame: number) => React.ReactNode}> = ({frame, explanations, children}) => <>59 {children(machineFrameAt(frame, explanations))}60 {explanations.map((explanation, i) => <ExplanationCallout key={i} frame={frame} explanation={explanation}/>)}61</>;