Number 54: Comparison bars
Specification
- Catalogue
- No. 54 of 110
- Section
- Charts and numbers
- Interaction
- Layout Animation
- Element
- Chart, Tabs
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
Bars that rescale when you switch the measure, with figures rolling beside them.
Proof the visitor can explore: they pick what to compare, and the change is animated where they are looking.
Bars scaleX 500ms ease-out. Figures roll with NumberFlow. Measure tabs use the CSS pill.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useId, useState, type CSSProperties } from "react";
import { Num, tabKey } from "@/components/pieces/shared";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/kit/tabs.css";
import "./benchmark-bars.css";
function Tabs({ label, options, value, onChange }: { label: string; options: string[]; value: number; onChange: (i: number) => void }) {
const idPrefix = useId();
return (
<div className="tabs" role="tablist" aria-label={label} style={{ "--n": options.length, "--i": value } as CSSProperties}>
<span data-light aria-hidden="true" />
{options.map((o, k) => (
<button
key={o}
id={`${idPrefix}-${k}`}
type="button"
role="tab"
aria-selected={value === k}
tabIndex={value === k ? 0 : -1}
onClick={() => onChange(k)}
onKeyDown={(e) => {
const n = tabKey(e.key, k, options.length);
if (n === null) return;
e.preventDefault();
onChange(n);
document.getElementById(`${idPrefix}-${n}`)?.focus();
}}
>
{o}
</button>
))}
</div>
);
}
const METRICS = ["Speed", "Price", "Support"];
const ROWS = [
{ name: "You", ours: true, v: [92, 81, 95] },
{ name: "Option A", v: [74, 88, 62] },
{ name: "Option B", v: [66, 59, 71] },
{ name: "Option C", v: [51, 70, 48] },
{ name: "Option D", v: [38, 45, 55] },
];
function BenchmarkBarsDemo() {
const [m, setM] = useState(0);
return (
<div className="dm">
<div className="dm-bench">
<Tabs label="Measure" options={METRICS} value={m} onChange={setM} />
<ol className="dm-bars">
{ROWS.map((r) => (
<li key={r.name} className="dm-bar" data-ours={r.ours || undefined}>
<span>{r.name}</span>
<span className="dm-track">
<span className="dm-fill" style={{ transform: `scaleX(${(r.v[m] ?? 0) / 100})` }} />
<span className="dm-val">
<Num value={r.v[m] ?? 0} />
</span>
</span>
</li>
))}
</ol>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ click: '[role="tab"]', nth: 1 },
{ click: '[role="tab"]', nth: 2 },
{ click: '[role="tab"]', nth: 0 },
];
export default function BenchmarkBarsEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Comparison bars" sample tour={TOUR}>
<BenchmarkBarsDemo />
</PieceStage>
);
}