Build a React 19 + TypeScript component called CostBars: a 3D bar chart built from divs and CSS.
Constraints
- One .tsx file, "use client", imports from react only. All CSS in a template string rendered as <style href="play-cost-bars" precedence="play">, every class prefixed "cb-".
- The root fills its parent (position: relative; width/height 100%; overflow: hidden; isolation: isolate; background #0a0a0f) and is a size container: sizes follow the frame, not the window. Fit 860×560 and 320×520.
- Font: var(--font-google-sans-flex, "Google Sans Flex"), system-ui, sans-serif.
Content
- A bars prop of numbers, default [56, 100]. No headline, labels or footnote: each bar has only its number above it (semibold, #f0f0f5, tabular, 1,000 separators).
Layout
- Two columns centred on a 1px floor (linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent), max 576px). Padding 48px top and bottom, 64px from 760px.
- Container-query tiers, bar width/depth/gap then number size: under 480px 96/18/28 and 22px; from 480px 116/26/64 and 28px; from 760px 124/28/112 and 32px.
- A ResizeObserver fits the tallest bar: row height minus the column's number, depth and 8px, over that bar's share of the max; clamp 48–380px.
Each bar is a solid
- Front face is the bar box. Top face: depth tall, above it, skewX(-45deg) translateX(depth), origin 0 0. Right flank: depth wide at left 100%, skewY(-45deg), origin 0 0. A 1px lit top edge.
- Lit from the upper left. First bar, slate front: 90deg white 10% → 2% at 20% → 0 at 45%, over 270deg black 35% → 0 at 40%, over 180deg #2f3145, #23253360 40%, #191a24. Top 160deg #43465e, #33364a 55%, #2a2c3d. Flank #1c1e2a → #141520. Edge white 16%.
- Other bars, brand front: 90deg white 45% → 12% at 14% → 0 at 38%, over 270deg rgba(6,20,45,0.42) → 0 at 42%, over 180deg #8b93f0, #6b8ef8 24%, #4aa1fb 48%, #2fbdf6 74%, #22d3ee. Top 160deg #e2ecff, #c5d8fb 45%, #a3c2f6. Flank 90deg black 28% → 50% over 180deg #5e64bb, #3f63b8 24%, #2b71ab 48%, #1a7f9e 74%, #118498. Edge white 70%. Glow 0 0 90px -16px rgba(56,150,248,0.55).
- Size front and flank backgrounds to the full-grown height, anchored to the bottom, so growth reveals a fixed ramp.
- Contact shadow: rgba(0,0,0,0.7) ellipse, 24px tall, inset -12px left, -24px right, -8px bottom, blur 12px. Under brand bars a light pool: radial-gradient(closest-side, rgba(34,211,238,0.26), rgba(99,140,250,0.15) 55%, transparent), 80px tall, inset -64px sides, -32px bottom, blur 24px.
Motion
- When 30% of the chart is on screen (once): bars grow from 0 over 1.1s, cubic-bezier(0.16,1,0.3,1), the second 0.15s later. The number fades up 16px over 0.4s at the bar's delay plus 0.1s; the pool fades in over 1.2s at delay plus 0.3s.
- Each number counts from 0 over 1200ms, ease-out cubic, starting with its bar.
Interaction and accessibility
- Hover (mouse), tap (touch, toggles) or keyboard focus lifts a bar 6px over 0.45s and brightens its glow (brand 0 0 110px -10px rgba(56,150,248,0.85); slate gets a faint lavender one), tightens its shadow and brightens the pool.
- The root is role="group" aria-label="3D bar chart". Bars are focusable, role="img", aria-label like "Bar 2: 56", with a 2px rgba(147,197,253,0.9) ring around the whole solid; the visible counting numbers are aria-hidden.
- prefers-reduced-motion: grown bars and final numbers at once, no lift.
- Off screen, finish any count-up; clean up on unmount.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420"use client";
import { useEffect, useRef, useState, useSyncExternalStore, type CSSProperties } from "react";
export type CostBarsProps = {
bars?: number[];
};
const DEFAULT_BARS = [56, 100];
const STAGGER_S = 0.15;
const COUNT_MS = 1200;
const MIN_BAR = 48;
const MAX_BAR = 380;
const HEADROOM = 8;
function format(value: number) {
return Math.round(value).toLocaleString("en-US");
}
const REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
function subscribeReducedMotion(onChange: () => void) {
const query = window.matchMedia(REDUCED_MOTION);
query.addEventListener("change", onChange);
return () => query.removeEventListener("change", onChange);
}
const getReducedMotion = () => window.matchMedia(REDUCED_MOTION).matches;
const getReducedMotionOnServer = () => false;
const css = `
.cb-root {
position: relative;
width: 100%;
height: 100%;
overflow: hidden;
isolation: isolate;
container: cb / size;
background: #0a0a0f;
color: #f0f0f5;
font-family: var(--font-google-sans-flex, "Google Sans Flex"), system-ui, sans-serif;
-webkit-font-smoothing: antialiased;
-webkit-tap-highlight-color: transparent;
}
.cb-root *, .cb-root *::before, .cb-root *::after { box-sizing: border-box; }
/* Layout */
.cb-inner {
--cb-bar-d: 18px;
--cb-bar-w: 96px;
--cb-col-w: 150px;
--cb-gap: 28px;
--cb-lift-y: -5px;
position: relative;
z-index: 1;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
padding: 48px 24px;
}
.cb-plot { flex: 1 1 0; min-height: 0; width: 100%; display: flex; flex-direction: column; }
.cb-row { flex: 1 1 0; min-height: 0; display: flex; align-items: flex-end; justify-content: center; gap: var(--cb-gap); }
.cb-floor {
flex: none;
width: 100%;
max-width: 576px;
height: 1px;
margin: 0 auto;
background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
}
/* A column: the number over the bar. Its width comes from the frame, never from the ticking number,
so the measured layout holds still while it counts. */
.cb-col { flex: 0 1 var(--cb-col-w); min-width: 0; display: flex; flex-direction: column; align-items: center; text-align: center; }
.cb-meta { display: flex; flex-direction: column; align-items: center; width: 100%; }
.cb-figure { font-size: 22px; line-height: 28px; font-weight: 600; color: #f0f0f5; font-variant-numeric: tabular-nums; white-space: nowrap; }
/* The slot is the bar's full-grown box. The bar grows from 0 to 100% of it, and the top face pokes
above it, so the slot carries a depth of headroom. */
.cb-slot {
--cb-slot-h: calc(var(--cb-bar-max, 200px) * var(--cb-ratio, 1));
position: relative;
flex: none;
width: var(--cb-bar-w);
height: var(--cb-slot-h);
margin-top: calc(var(--cb-bar-d) + 8px);
outline: none;
touch-action: manipulation;
}
.cb-lift { position: absolute; inset: 0; transition: transform 0.45s cubic-bezier(0.16,1,0.3,1); }
.cb-bar {
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 0;
/* Safari clips blurred glows on some boxes; its own layer keeps the bloom round. */
transform: translateZ(0);
will-change: height;
transition: box-shadow 0.45s ease;
}
.cb-root[data-phase="play"] .cb-bar { height: 100%; animation: cb-grow 1.1s cubic-bezier(0.16,1,0.3,1) var(--cb-delay, 0s) both; }
@keyframes cb-grow { from { height: 0; } }
.cb-top {
position: absolute;
bottom: 100%;
left: 0;
width: 100%;
height: var(--cb-bar-d);
transform: skewX(-45deg) translateX(var(--cb-bar-d));
transform-origin: 0 0;
}
.cb-side {
position: absolute;
top: 0;
left: 100%;
width: var(--cb-bar-d);
height: 100%;
background-size: 100% var(--cb-slot-h);
background-position: bottom;
transform: skewY(-45deg);
transform-origin: 0 0;
}
/* The ramp is sized to the grown bar and anchored to the floor, so the bar reveals up through a fixed
gradient instead of squeezing the whole ramp into every frame of the animation. */
.cb-front { position: absolute; inset: 0; border-radius: 2px; background-size: 100% var(--cb-slot-h); background-position: bottom; }
.cb-edge { position: absolute; left: 0; right: 0; top: 0; height: 1px; transition: background-color 0.45s ease; }
/* Contact shadow, tight and dark under the solid, and the light the brand bar throws on the floor. */
.cb-shadow {
position: absolute;
bottom: -8px;
left: -12px;
right: -24px;
height: 24px;
border-radius: 100%;
background: rgba(0,0,0,0.7);
filter: blur(12px);
pointer-events: none;
transition: opacity 0.45s ease, transform 0.45s cubic-bezier(0.16,1,0.3,1);
}
.cb-pool { position: absolute; bottom: -32px; left: -64px; right: -64px; height: 80px; pointer-events: none; opacity: 0; }
.cb-root[data-phase="play"] .cb-pool { opacity: 1; animation: cb-fade 1.2s ease-out var(--cb-pool-delay, 0.3s) both; }
@keyframes cb-fade { from { opacity: 0; } }
.cb-pool-light {
position: absolute;
inset: 0;
background: radial-gradient(closest-side, rgba(34,211,238,0.26), rgba(99,140,250,0.15) 55%, transparent 100%);
filter: blur(24px);
transition: transform 0.45s cubic-bezier(0.16,1,0.3,1);
}
.cb-pool-light::after { content: ""; position: absolute; inset: 0; background: inherit; opacity: 0; transition: opacity 0.45s ease; }
/* Faces, lit from the upper left: a specular edge down the left of the front, shadow gathering on the
right, a bright top plate catching the light and the flank in shade. */
.cb-muted .cb-front {
background-image:
linear-gradient(90deg, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0.02) 20%, rgba(255,255,255,0) 45%),
linear-gradient(270deg, rgba(0,0,0,0.35) 0%, rgba(0,0,0,0) 40%),
linear-gradient(180deg, #2f3145 0%, #23253360 40%, #191a24 100%);
}
.cb-muted .cb-top { background: linear-gradient(160deg, #43465e 0%, #33364a 55%, #2a2c3d 100%); }
.cb-muted .cb-side { background-image: linear-gradient(180deg, #1c1e2a 0%, #141520 100%); }
.cb-muted .cb-edge { background-color: rgba(255,255,255,0.16); }
.cb-muted .cb-bar { box-shadow: 0 0 70px -20px rgba(150,160,215,0); }
.cb-brand .cb-front {
background-image:
linear-gradient(90deg, rgba(255,255,255,0.45) 0%, rgba(255,255,255,0.12) 14%, rgba(255,255,255,0) 38%),
linear-gradient(270deg, rgba(6,20,45,0.42) 0%, rgba(6,20,45,0) 42%),
linear-gradient(180deg, #8b93f0 0%, #6b8ef8 24%, #4aa1fb 48%, #2fbdf6 74%, #22d3ee 100%);
}
.cb-brand .cb-top { background: linear-gradient(160deg, #e2ecff 0%, #c5d8fb 45%, #a3c2f6 100%); }
.cb-brand .cb-side {
background-image:
linear-gradient(90deg, rgba(0,0,0,0.28) 0%, rgba(0,0,0,0.5) 100%),
linear-gradient(180deg, #5e64bb 0%, #3f63b8 24%, #2b71ab 48%, #1a7f9e 74%, #118498 100%);
}
.cb-brand .cb-edge { background-color: rgba(255,255,255,0.7); }
.cb-brand .cb-bar { box-shadow: 0 0 90px -16px rgba(56,150,248,0.55); }
/* Lifted: on hover, keyboard focus or a tap. */
.cb-slot:is([data-active="true"], :focus-visible) .cb-lift { transform: translateY(var(--cb-lift-y)); }
.cb-slot:is([data-active="true"], :focus-visible) .cb-shadow { opacity: 0.75; transform: scaleX(0.92); }
.cb-slot:is([data-active="true"], :focus-visible) .cb-pool-light { transform: scale(1.08); }
.cb-slot:is([data-active="true"], :focus-visible) .cb-pool-light::after { opacity: 0.8; }
.cb-muted .cb-slot:is([data-active="true"], :focus-visible) .cb-bar { box-shadow: 0 0 70px -20px rgba(150,160,215,0.4); }
.cb-muted .cb-slot:is([data-active="true"], :focus-visible) .cb-edge { background-color: rgba(255,255,255,0.32); }
.cb-brand .cb-slot:is([data-active="true"], :focus-visible) .cb-bar { box-shadow: 0 0 110px -10px rgba(56,150,248,0.85); }
.cb-brand .cb-slot:is([data-active="true"], :focus-visible) .cb-edge { background-color: rgba(255,255,255,0.95); }
.cb-lift::after {
content: "";
position: absolute;
top: calc(-1 * var(--cb-bar-d) - 8px);
right: calc(-1 * var(--cb-bar-d) - 8px);
bottom: -8px;
left: -8px;
border: 2px solid rgba(147,197,253,0.9);
border-radius: 12px;
opacity: 0;
pointer-events: none;
}
.cb-slot:focus-visible .cb-lift::after { opacity: 1; }
/* The number fades up as its bar starts to grow. */
.cb-rise { opacity: 0; }
.cb-root[data-phase="play"] .cb-rise { opacity: 1; animation: cb-rise 0.4s ease-out var(--cb-d, 0s) both; }
@keyframes cb-rise { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
/* Sizes follow the frame, not the window. */
@container cb (min-width: 480px) {
.cb-inner { --cb-bar-d: 26px; --cb-bar-w: 116px; --cb-col-w: 190px; --cb-gap: 64px; padding-inline: 32px; }
.cb-figure { font-size: 28px; line-height: 34px; }
}
@container cb (min-width: 760px) {
.cb-inner { --cb-bar-d: 28px; --cb-bar-w: 124px; --cb-col-w: 200px; --cb-gap: 112px; --cb-lift-y: -6px; padding-block: 64px; }
.cb-figure { font-size: 32px; line-height: 40px; }
}
@media (prefers-reduced-motion: reduce) {
.cb-root .cb-bar { height: 100% !important; animation: none !important; }
.cb-root .cb-rise, .cb-root .cb-pool { opacity: 1 !important; transform: none !important; animation: none !important; }
.cb-root .cb-lift, .cb-root .cb-shadow, .cb-root .cb-pool-light { transform: none !important; transition: none !important; }
}
`;
function vars(values: Record<string, string>) {
return values as CSSProperties;
}
export default function CostBars({ bars: values = DEFAULT_BARS }: CostBarsProps) {
const rootRef = useRef<HTMLDivElement>(null);
const rowRef = useRef<HTMLDivElement>(null);
const metaRefs = useRef<(HTMLDivElement | null)[]>([]);
const visibleRef = useRef(false);
const playedRef = useRef(false);
const [phase, setPhase] = useState<"wait" | "play">("wait");
const [barMax, setBarMax] = useState<number | null>(null);
const [active, setActive] = useState<number | null>(null);
const [shown, setShown] = useState<number[]>(() => values.map(() => 0));
const reducedMotion = useSyncExternalStore(subscribeReducedMotion, getReducedMotion, getReducedMotionOnServer);
const maxValue = Math.max(...values, 1);
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const observer = new IntersectionObserver(
(entries) => {
const entry = entries[entries.length - 1];
visibleRef.current = entry.isIntersecting;
if (!playedRef.current && entry.isIntersecting && entry.intersectionRatio >= 0.3) {
playedRef.current = true;
setPhase("play");
}
},
{ threshold: [0, 0.35] },
);
observer.observe(root);
return () => observer.disconnect();
}, []);
useEffect(() => {
const row = rowRef.current;
if (!row) return;
const measure = () => {
const depth = parseFloat(getComputedStyle(row).getPropertyValue("--cb-bar-d")) || 0;
let fit = MAX_BAR;
values.forEach((value, i) => {
const meta = metaRefs.current[i];
if (!meta || value <= 0) return;
fit = Math.min(fit, (row.clientHeight - meta.offsetHeight - depth - HEADROOM) / (value / maxValue));
});
setBarMax(Math.max(MIN_BAR, Math.floor(fit)));
};
const observer = new ResizeObserver(measure);
observer.observe(row);
metaRefs.current.forEach((meta) => meta && observer.observe(meta));
return () => observer.disconnect();
}, [values, maxValue]);
useEffect(() => {
if (phase !== "play" || reducedMotion) return;
const start = performance.now();
let frame = 0;
const tick = (now: number) => {
if (!visibleRef.current) {
setShown(values);
return;
}
let done = true;
setShown(
values.map((value, i) => {
const t = Math.min(Math.max((now - start - i * STAGGER_S * 1000) / COUNT_MS, 0), 1);
if (t < 1) done = false;
return Math.round(value * (1 - Math.pow(1 - t, 3)));
}),
);
if (!done) frame = requestAnimationFrame(tick);
};
frame = requestAnimationFrame(tick);
return () => cancelAnimationFrame(frame);
}, [phase, reducedMotion, values]);
return (
<div
ref={rootRef}
className="cb-root"
role="group"
aria-label="3D bar chart"
data-phase={phase}
onPointerUp={(event) => {
if (event.pointerType !== "mouse" && !(event.target as Element).closest(".cb-slot")) setActive(null);
}}
>
<style href="play-cost-bars" precedence="play">
{css}
</style>
<div className="cb-inner" style={barMax ? vars({ "--cb-bar-max": `${barMax}px` }) : undefined}>
<div className="cb-plot">
<div ref={rowRef} className="cb-row">
{values.map((value, i) => {
const delay = i * STAGGER_S;
const tone = i === 0 ? "muted" : "brand";
return (
<div key={i} className={`cb-col cb-${tone}`}>
<div
ref={(el) => {
metaRefs.current[i] = el;
}}
className="cb-meta"
aria-hidden="true"
>
<div className="cb-figure cb-rise" style={vars({ "--cb-d": `${delay + 0.1}s` })}>
{format(reducedMotion ? value : (shown[i] ?? 0))}
</div>
</div>
<div
className="cb-slot"
role="img"
tabIndex={0}
aria-label={`Bar ${i + 1}: ${format(value)}`}
data-active={active === i ? "true" : undefined}
style={vars({
"--cb-ratio": String(value / maxValue),
"--cb-delay": `${delay}s`,
"--cb-pool-delay": `${delay + 0.3}s`,
})}
onPointerEnter={(event) => {
if (event.pointerType === "mouse") setActive(i);
}}
onPointerLeave={(event) => {
if (event.pointerType === "mouse") setActive((current) => (current === i ? null : current));
}}
onPointerUp={(event) => {
if (event.pointerType !== "mouse") setActive((current) => (current === i ? null : i));
}}
>
<div className="cb-lift">
<div className="cb-bar">
<div className="cb-top" />
<div className="cb-side" />
<div className="cb-front" />
<div className="cb-edge" />
</div>
</div>
<div className="cb-shadow" />
{tone === "brand" && (
<div className="cb-pool">
<div className="cb-pool-light" />
</div>
)}
</div>
</div>
);
})}
</div>
<div className="cb-floor" aria-hidden="true" />
</div>
</div>
</div>
);
}