3D bar chart
A 3D bar chart from joinstarlight.com, built from skewed divs and CSS gradients. The bars grow in, the numbers count up, and each bar lifts when you hover it.
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="ui-cost-bars" precedence="ui">, 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.
"use client";
/*
* CostBars: a 3D bar chart, from the cost chart on joinstarlight.com (Starlight).
*
* Two CSS-built solids, each topped with its number. Each solid is three divs: a front face, a top face skewed back with skewX(-45deg) and a right flank
* skewed with skewY(-45deg), all shaded as if lit from the upper left. The front gradient is sized to
* the full-grown bar and pinned to the floor, so a growing bar reveals the ramp instead of squashing it.
*
* When the chart scrolls into view the bars grow (1.1s, 0.15s apart) and each number counts up over
* 1.2s. A ResizeObserver fits the bar height to the space the parent leaves, and container queries pick
* the bar width, depth and type size, so it works from a phone-width frame up. Hover, focus or tap a bar
* to lift it and brighten its glow. Under reduced motion the chart shows its finished state and nothing
* moves.
*
* Usage:
* import CostBars from "./CostBars";
* <div style={{ height: 560 }}><CostBars /></div>
*
* Requirements: React 19 (it renders its CSS through <style href precedence>), no other dependencies.
* It fills its parent, so give the parent a height (about 520px or more). Font: Google Sans Flex through
* --font-google-sans-flex, falling back to system-ui. Pass your own values with the `bars` prop.
*/
import { useEffect, useRef, useState, useSyncExternalStore, type CSSProperties } from "react";
export type CostBarsProps = {
/** One number per bar. The first bar is drawn in muted slate, the rest in the brand ramp. */
bars?: number[];
};
const DEFAULT_BARS = [56, 100];
const STAGGER_S = 0.15; // delay between the bars
const COUNT_MS = 1200; // count-up length
const MIN_BAR = 48; // px, the tallest bar never gets shorter than this
const MAX_BAR = 380; // px, nor taller than the live site's desktop bar
const HEADROOM = 8; // px between a number and its bar's top face; matches .cb-slot's margin-top
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);
// "wait": bars flat, text hidden. "play": the CSS animations run (and hold their end state).
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);
// Play once, the first time most of the chart is on screen. Also tracks visibility, so a running
// count-up finishes at once when the chart leaves the screen.
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();
}, []);
// Fit the tallest bar to the room the frame leaves: the row's height, less each column's number and
// the top face's headroom. Bar width and depth come from the container queries in the CSS.
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]);
// Count each number up from 0, starting with its bar and easing out (cubic) so it lands as the bar
// tops out.
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) => {
// A tap outside the bars drops a lifted bar.
if (event.pointerType !== "mouse" && !(event.target as Element).closest(".cb-slot")) setActive(null);
}}
>
<style href="ui-cost-bars" precedence="ui">
{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) => {
// Touch and pen have no hover: a tap lifts the bar, a second tap drops it.
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>
);
}






