React component for shadcn/ui
Sparkline Stat
A React sparkline stat tile for shadcn/ui that draws its trend line in on view and scrubs to any point on hover, touch, or arrow keys.
Sparkline Stat is a React stat tile for shadcn/ui: a headline number, an optional change badge, and a small trend line under it. The line draws itself in the first time the tile scrolls into view. Hover or touch the line and a marker springs to the nearest point and the headline follows that point's value and date, then rolls back to the latest value when you let go. Use it for dashboard KPIs like revenue, signups, latency, or retention. It needs no chart library.
Example
Features
- The line draws in left to right once, when at least a third of the tile is on screen. Re-renders never replay it, and the fill and the end dot fade up after the line lands.
- Hover with a mouse, or press and slide a finger, to scrub the line. The marker snaps to the nearest point on a short spring. While you scrub, the headline and date update instantly with tabular digits, so a fast sweep never leaves numbers spinning or shifting sideways. When you let go, or new data arrives, only the digits that change roll to the new value.
- New
datareshapes the line on a spring from the old series to the new one, even when the number of points changes. Passing a new array with the same values does nothing. - Color follows the trend: green when it moves the good way, red when it
doesn't, and neutral when it is flat. The badge arrow carries direction
too, so color is never the only cue.
positive="down"flips it for latency, costs, or churn. - Empty, single-point, and flat series all render cleanly, with no
NaNpaths. A flat series sits mid-plot; an empty one shows a placeholder at the same height, so nothing shifts when data arrives. - The SVG stretches to any width without measuring, and the stroke stays crisp at every size, so the tile renders on the server with no layout shift.
Installation
pnpm dlx shadcn@latest add @tentui/sparkline-stat
Usage
import { SparklineStat } from "@/components/sparkline-stat";<SparklineStat
label="Revenue"
data={[12_400, 18_100, 15_200, 22_900, 27_300, 31_800, 29_400]}
format={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }}
delta={{ value: 0.12, format: "percent" }}
/>data is a plain number[], oldest first. The headline shows value, or the
last point when value is omitted. format takes Intl.NumberFormat
options and applies to the headline, the spoken values, and a delta with
format: "number". Numbers are formatted in the given locale.
formatLabel names a point by its index, for example a date. It is shown
under the value while a point is active. At rest that line shows caption, or
the last point's label.
<SparklineStat
label="p95 latency"
data={latency}
format={{ style: "unit", unit: "millisecond", unitDisplay: "narrow" }}
formatLabel={(i) => days[i]}
caption="Last 14 days"
positive="down"
variant="line"
/>The component has no card chrome, so it drops into your own card, grid cell,
or popover. height sets the sparkline's height in px.
Controlled
The active point is an index into data, or null for the latest value. Use
activeIndex and onActiveIndexChange to control it, for example to scrub
several tiles together:
const [active, setActive] = useState<number | null>(null);
<SparklineStat data={visitors} activeIndex={active} onActiveIndexChange={setActive} />
<SparklineStat data={latency} activeIndex={active} onActiveIndexChange={setActive} />Interaction
| Input | Action |
|---|---|
| Hover the line | Moves the marker to the nearest point |
| Press and slide a finger | Scrubs the line; lifting returns to the latest value |
| Pointer leaves | Returns to the latest value |
← / ↓, → / ↑ | Moves the active point by one |
Home / End | Jumps to the first or last point |
Escape | Returns to the latest value |
The plot uses touch-action: pan-y, so vertical swipes still scroll the
page. Each time touch or the keyboard moves the active point, supported
devices play a light selection haptic; a mouse hover stays silent. Set
haptics={false} to turn it off.
Accessibility
The root is a figure whose aria-label sums up the stat, for example
"Revenue, $1,904, up 12.4%". The SVG is hidden from assistive technology.
The plot is a focusable slider named "Revenue history", with an
aria-valuetext such as "Nov 21, $1,604" that screen readers read as you
move. The focus ring shows only for keyboard focus, and the active point
resets when focus leaves.
With reduced motion enabled, the line appears without drawing in, the marker and the line jump instead of springing, and the headline changes without rolling.
API reference
FAQ
How do I install the Sparkline Stat with shadcn?
Run npx shadcn@latest add @tentui/sparkline-stat after adding the @tentui
registry to components.json (installation guide), or
run npx shadcn@latest add https://tentui.com/r/sparkline-stat.json without
it. The CLI copies sparkline-stat.tsx into your project and installs
@number-flow/react, motion, lucide-react, and web-haptics.
Do I need Recharts or another chart library for a sparkline?
No. The Sparkline Stat draws its own SVG line, fill, and marker from a plain
number[], so it works without Recharts, visx, or d3.
Is the Sparkline Stat keyboard accessible?
Yes. The sparkline is a focusable slider: the arrow keys move the active
point by one, Home and End jump to the first and last points, and
Escape returns to the latest value. Its aria-valuetext reads each point's
label and value, and the root figure has an aria-label that sums up the
value and trend.
How do I control the hovered point or sync several tiles?
Pass activeIndex and onActiveIndexChange. The index points into data,
and null shows the latest value. Share one state between tiles and hovering
any of them scrubs them all.
Can I show a metric where going down is good?
Yes. Set positive="down" and a fall is colored green and a rise red, which
suits latency, costs, or churn. The badge arrow shows direction either way.
Is the Sparkline Stat free to use?
Yes. The Sparkline Stat is free and source-available: the shadcn CLI copies its source into your project, and you can use it in unlimited personal and client projects under the TentUI License. The license doesn't allow redistributing or reselling the component itself.
Related components
All charts and data- Chart Brush
An area or line chart with two draggable handles for picking a time range. The selected stretch is inked in while the rest fades back, the change rolls in the header, and every point the handles cross ticks.
- Contribution Heat
A calendar heatmap with a spring-driven optical hover loupe, zero-shift day-by-day readout, click-to-pin inspection, and interactive intensity filtering.
- Range Span
A two-handle range control with a stretchy selected fill, draggable span panning, rubber-band limits, and live min and max labels that merge before colliding.
- Tick Ruler
A ruler-style number picker you drag or flick, with a click and a haptic tick on every step.