import type { ReactNode } from 'react'; import styles from './PerfTable.module.css'; import tipStyles from './tips.module.css'; import useTips from './useTips'; export interface PerfTableRow { label: string; description?: ReactNode; /** One [before, after] pair per column */ values: [number, number][]; } /** Before/after results across several metrics; cells show the change, with exact numbers on hover or focus */ export default function PerfTable({ columns, rows, }: { /** Header and unit of each metric; lower is better. `delta: 'absolute'` labels the change in units instead of percent */ columns: { label: string; unit: string; delta?: 'percent' | 'absolute' }[]; rows: PerfTableRow[]; }) { const tips = useTips(); return (
))} {rows.map(({ label, description, values }) => ( {values.map(([before, after], i) => { const { unit, delta = 'percent' } = columns[i]; const change = delta === 'absolute' ? after - before : Math.round(((after - before) / before) * 100); const max = Math.max(before, after); return ( ); })} ))}
{columns.map(({ label }) => ( {label}
{label} {description && (
{description}
)}
0 ? styles.worse : styles.same } > {change === 0 ? 'same' : `${change > 0 ? '+' : ''}${change}${delta === 'absolute' ? ` ${unit}` : '%'}` }
{/* exact numbers stay in the DOM for crawlers and screen readers */} {before} {unit} →{' '} {after} {unit}
); }