Stat Bars
Percentage bars that grow, one after another, when the chart scrolls into view.
$npx shadcn@latest add https://framebrick.co/r/stat-bars.jsonFrom the Nitip template, see it in context.
94%
On-time projects
78%
Returning clients
62%
Organic traffic
import { StatBars } from "@/components/ui/stat-bars";
const stats = [
{ value: 94, label: "On-time projects" },
{ value: 78, label: "Returning clients" },
{ value: 62, label: "Organic traffic" },
];
export default function StatBarsDemo() {
return <StatBars stats={stats} height={200} className="w-full max-w-lg" />;
}Installation
$ npx shadcn@latest add https://framebrick.co/r/stat-bars.json1. Install the dependencies.
npm install motion clsx tailwind-merge2. Add
cn()tolib/utils.ts(skip if shadcn already created it).import { clsx, type ClassValue } from "clsx"; import { extendTailwindMerge } from "tailwind-merge"; /* tailwind-merge must know the site's type scale (globals.css) is a font size, not a text color — otherwise cn("text-h6 text-muted-foreground") drops text-h6. */ const twMerge = extendTailwindMerge({ extend: { classGroups: { "font-size": [{ text: ["display", "h1", "h2", "h3", "h4", "h5", "h6"] }] } }, }); export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); }3. Copy the component into
components/ui/stat-bars.tsx."use client"; import * as React from "react"; import { motion, useReducedMotion, type Transition } from "motion/react"; import { cn } from "@/lib/utils"; /* * Stat Bars — percentage bars that grow when the chart scrolls into view (from the Nitip "Chart"). * Each bar's height follows its value, and so does the strength of its fill (75–100%). The text on it stays at full * strength: fading the whole bar, as Framer does, dropped the label to 3.3–4.3:1 on a coloured primary * (axe, 2026-10-01). Bars start at 30% and grow 50 ms apart (Nitip's refinement over Framer, kept here). * Below 640px the bars lie flat and their width follows the value instead. A flat bar is at least 96px tall and grows * when its label wraps: at a fixed 96px the label's last lines were cut off (measured 2026-10-12: 117px of content). * They grow once, the first time they are seen. With reduced motion they simply arrive at their value. * * Two ways back to the true value, because the server renders every bar at the 30% it starts from and only the * animation takes it to the value — so a bar that is never animated shows the wrong data, not just a late one: * - without JavaScript, a <noscript> style points --fill at --value (it never applies when scripts run); * - in print, a bar that never scrolled into view is set to its value (print:sm:h-(--value)). */ const spring: Transition = { type: "spring", bounce: 0.2, duration: 0.4 }; export type Stat = { value: number; label: string }; export type StatBarsProps = { stats: Stat[]; /** Height of the chart from 640px up. */ height?: number; /** Fade the fill with the value (75–100%). Turn it off for a light `primary`, where the label would lose contrast. */ fade?: boolean; className?: string; }; export function StatBars({ stats, height = 320, fade = true, className }: StatBarsProps) { // The bar's height comes from a CSS variable, and MotionConfig only skips transform and size animations, so // reduced motion is handled here: with it, the bars do not grow into their value but arrive at it. // The rendered markup must not depend on it: useReducedMotion() reads matchMedia on the first client render, which // the server cannot, so a different starting value would be a hydration mismatch. Only the transition changes. const reduce = useReducedMotion(); return ( <div className={cn("flex w-full flex-col gap-1 sm:h-(--chart-height) sm:flex-row sm:items-end sm:gap-1.5", className)} style={{ ["--chart-height" as string]: `${height}px` }} > {/* Absolutely positioned, so that without JavaScript it is not an extra flex item and adds no gap. */} <noscript className="absolute"> <style>{`[data-stat-bar]{--fill:var(--value)!important}`}</style> </noscript> {stats.map((stat, index) => ( <motion.div key={stat.label} data-stat-bar="" initial={{ ["--fill" as string]: "30%" }} whileInView={{ ["--fill" as string]: `${stat.value}%` }} viewport={{ once: true, amount: 0.5 }} transition={reduce ? { duration: 0 } : { ...spring, delay: index * 0.05 }} style={{ ["--value" as string]: `${stat.value}%` }} className="relative flex min-h-24 w-(--value) flex-col justify-between gap-2 overflow-hidden rounded-md px-4 py-3 text-primary-foreground sm:h-(--fill) sm:min-h-0 sm:w-auto sm:flex-1 sm:gap-6 sm:py-4 print:sm:h-(--value)!" > {/* Only the fill fades with the value; the numbers and labels stay at full strength (axe, 2026-10-01). */} <span aria-hidden className="absolute inset-0 rounded-md bg-primary" style={{ opacity: fade ? 0.75 + (stat.value / 100) * 0.25 : 1 }} /> <div className="relative flex flex-col gap-3"> <p className="text-xl font-semibold tabular-nums">{stat.value}%</p> <div className="h-px bg-primary-foreground/25" /> </div> <p className="relative text-sm text-balance">{stat.label}</p> </motion.div> ))} </div> ); }
Usage
import { StatBars } from "@/components/ui/stat-bars";
<StatBars stats={[{ value: 94, label: "Projects delivered on time" }, { value: 78, label: "Clients who come back" }]} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| stats | { value: number; label: string }[] | — | Values are percentages (0–100). |
| height | number | 320 | Height of the chart from 640 px up, in px. |
| fade | boolean | true | Fade the fill with the value (75-100%). Turn it off for a light primary, where the label would lose contrast. |
| className | string | — | Classes for the row. |
- Bars start at 30% and grow 50 ms apart — Nitip's refinement over Framer, kept here.
- Only the fill fades with the value; the number and the label stay at full strength. Fading the whole bar, as Framer does, dropped the label to 3.3-4.3:1 on a coloured primary.
- Below 640 px the bars lie flat and their width follows the value.
- They grow once, the first time they are seen.
- The server renders every bar at the 30% it starts from, so a bar that is never animated shows the wrong data. Without JavaScript a noscript style points the height at the real value, and in print a bar that never scrolled into view is set to its value; with JavaScript on screen neither applies.
- With reduced motion the bars appear at their value instead of growing.
AI prompt
Paste into v0, Lovable, Bolt or Claude to rebuild this component in your own project.
Create a React + Tailwind CSS + Motion (motion/react) component called StatBars.
- A row of bars, one per stat: bg-primary, text-primary-foreground, rounded-md, p-4, the value as a large
tabular-nums percentage at the top, a hairline under it, and the label at the bottom.
- Each bar's height is its value in percent of the chart height, and its opacity follows the value
(minimum 0.35 so small bars stay readable).
- Animate a CSS variable --fill from 30% to the value with whileInView (viewport once, amount 0.5) and a spring
(bounce 0.2, duration 0.4s), delayed by 50ms per bar, with the bar's height reading height: var(--fill).
- Under 640px the bars stack as horizontal rows whose width is the value instead.
- With prefers-reduced-motion (useReducedMotion), keep the starting value the same and use a zero-duration transition,
so the bars arrive at their value when they come into view — a CSS variable is not a transform, so MotionConfig would
not skip it. Do not change the starting value itself: it is rendered into the server HTML, and a different one on the
client is a hydration mismatch.
- The server renders --fill at 30%, so a bar that is never animated shows the wrong height. Give the container a
<noscript> style `[data-stat-bar]{--fill:var(--value)!important}` (position it absolute so it adds no flex gap), mark
each bar with data-stat-bar, and give each bar `print:sm:h-(--value)!` so a printed page shows true heights.










