Spring Bars
A bar chart whose bars rise like liquid, with a value bubble that flows to the bar you point at.
Preview
"use client";
import { useState } from "react";
import { useAutoplay } from "../demo-context";
import { SpringBars } from "./SpringBars";
const weeks = [
[32, 48, 41, 66, 58, 80, 72],
[54, 38, 70, 52, 88, 61, 46],
[28, 64, 50, 44, 76, 92, 84],
];
const days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
export default function Demo() {
const [turn, setTurn] = useState(0);
useAutoplay(() => setTurn((t) => t + 1), 1900);
const data = days.map((label, i) => ({ label, value: weeks[turn % weeks.length][i] }));
return <SpringBars label="Orders this week" data={data} max={100} />;
}Installation
npx @usefluid/cli add spring-barspnpm dlx @usefluid/cli add spring-barsyarn dlx @usefluid/cli add spring-barsbunx @usefluid/cli add spring-barsInstall the engine.
Terminal npm i @usefluid/motionCopy these files into your project.
components/fluid/spring-bars/SpringBars.tsx "use client"; import { useSpring } from "@usefluid/motion/react"; import { type CSSProperties, type KeyboardEvent, useMemo, useRef, useState } from "react"; import styles from "./SpringBars.module.css"; type Datum = { label: string; value: number; color?: string }; type Props = { label: string; data: Datum[]; max?: number; format?: Intl.NumberFormatOptions; locale?: string; color?: string; }; function Bar({ share, index }: { share: number; index: number }) { const fill = useRef<HTMLSpanElement>(null); useSpring( { h: share }, ({ h }) => { if (fill.current) fill.current.style.height = `${Math.max(0, h) * 100}%`; }, { w: Math.max(9, 15 - index * 0.45), z: 0.6 }, ); return <span ref={fill} className={styles.fill} />; } export function SpringBars({ label, data, max, format, locale, color }: Props) { const plot = useRef<HTMLDivElement>(null); const bubble = useRef<HTMLSpanElement>(null); const [active, setActive] = useState(data.length - 1); const key = JSON.stringify(format ?? {}); const number = useMemo(() => new Intl.NumberFormat(locale, JSON.parse(key)), [locale, key]); const top = max ?? Math.max(1, ...data.map((d) => d.value)); const current = Math.min(active, data.length - 1); const shareOf = (v: number) => Math.min(1, Math.max(0, v / top)); useSpring( { x: data.length ? (current + 0.5) / data.length : 0.5, y: data.length ? shareOf(data[current].value) : 0 }, ({ x, y }) => { if (!bubble.current) return; bubble.current.style.left = `${x * 100}%`; bubble.current.style.bottom = `${y * 100}%`; }, { w: 16, z: 0.72 }, ); const move = (e: KeyboardEvent, i: number) => { const next = e.key === "ArrowRight" ? i + 1 : e.key === "ArrowLeft" ? i - 1 : e.key === "Home" ? 0 : e.key === "End" ? data.length - 1 : -1; if (next < 0 || next >= data.length) return; e.preventDefault(); setActive(next); plot.current?.querySelectorAll<HTMLElement>("button")[next]?.focus(); }; return ( <figure className={styles.root} aria-label={label} style={color ? ({ "--fluid-accent": color, "--fluid-on-accent": "#ffffff" } as CSSProperties) : undefined} > <div ref={plot} className={styles.plot} role="group" aria-label={label}> {data.map((d, i) => ( <button key={d.label} type="button" className={styles.column} style={d.color ? ({ "--bar": d.color } as CSSProperties) : undefined} data-active={i === current ? "" : undefined} tabIndex={i === current ? 0 : -1} aria-label={`${d.label}: ${number.format(d.value)}`} onPointerEnter={() => setActive(i)} onFocus={() => setActive(i)} onKeyDown={(e) => move(e, i)} > <Bar share={shareOf(d.value)} index={i} /> </button> ))} {data.length > 0 && ( <span ref={bubble} className={styles.bubble} aria-hidden> {number.format(data[current].value)} </span> )} </div> <div className={styles.axis} aria-hidden> {data.map((d) => ( <span key={d.label}>{d.label}</span> ))} </div> </figure> ); }components/fluid/spring-bars/SpringBars.module.css .root { display: grid; gap: 10px; width: min(100%, 420px); margin: 0; padding: 24px 22px 18px; border-radius: 24px; background: var(--fluid-surface); color: var(--fluid-text); font-family: var(--fluid-font); } .plot { position: relative; display: flex; align-items: stretch; gap: 8px; height: 180px; margin-top: 36px; } .column { position: relative; display: flex; flex: 1; align-items: flex-end; min-width: 0; padding: 0; border: 0; border-radius: 12px; background: color-mix(in srgb, var(--fluid-text) 5%, transparent); cursor: pointer; } .column:focus-visible { outline: 2px solid var(--fluid-focus); outline-offset: 2px; } .fill { width: 100%; min-height: 8px; border-radius: 12px; background: color-mix(in srgb, var(--bar, var(--fluid-accent)) 45%, var(--fluid-surface)); transition: background 250ms ease; } .column[data-active] .fill { background: var(--bar, var(--fluid-accent)); } .bubble { position: absolute; z-index: 1; padding: 6px 10px; border-radius: 999px; background: var(--fluid-text); color: var(--fluid-surface); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; transform: translate(-50%, -8px); pointer-events: none; } .axis { display: flex; gap: 8px; color: var(--fluid-text-2); font-size: 12px; } .axis span { flex: 1; min-width: 0; overflow: hidden; text-align: center; text-overflow: ellipsis; }Import the theme once, then point its variables at your own tokens.
layout.tsx import "@usefluid/motion/theme.css";
Usage
import { SpringBars } from "./SpringBars";
<SpringBars label="Orders this week" max={100} data={[{ label: "Mon", value: 32 }, { label: "Tue", value: 48 }]} />API reference
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Accessible name of the chart. |
data | { label, value }[] | — | One bar per item. Labels must be unique. Change the values and the bars flow to them. |
max | number | the largest value | The value of a full bar. Keep it fixed when the data changes, so bars compare across changes. |
format | Intl.NumberFormatOptions | — | How values are written, as in Intl.NumberFormat. |
locale | string | the browser's | Locale for the values. |
Keyboard and screen readers
Each bar is a button named with its label and value. Tab reaches the chart once; the arrow keys, Home and End move between bars, and the bubble follows. With reduced motion bars change at once.
Notes
Bars further right use slightly slower springs, so a change ripples across the chart. Every colour comes from the --fluid-* variables, and the piece is yours to change.
How it is made
Each bar has a spring of its own, a little slower from left to right, so a change ripples across the chart instead of jumping. The bubble is on two springs, sideways and up, and follows the pointer or the arrow keys. Every bar is a button read with its label and value.
For your agent
For your agent
Add Spring Bars to my project with the Fluid library. Replace the bar charts of my dashboard with SpringBars, with a fixed max so bars compare across changes. Read my project first. Use my logo, my colours and my font through the --fluid-* variables. Keep 44px touch targets, visible focus and reduced motion. If I have no SVG logo, ask before making one up.