Keyframery

Sparkline

A word-sized trend in plain SVG and CSS, no charting library: a line, an area or a row of bars from a list of numbers, with its last point marked. It draws itself on arrival, tweens into new data (a week into a month and back), colours itself by its news if you ask, and lets people scrub it with the pointer or the arrow keys while its value rolls in a small readout. Its trend and change are readable by screen readers and agents.

See it as an AI agent does.

Resize preview. Focus the handle and use the left or right arrow keys. Hold Shift for larger steps.— px
Customize1 controls
Animation speed1×

Installation

npx shadcn@latest add @keyframery/sparkline

This adds components/keyframery/sparkline.tsx and its primitive to your project, and installs UI SDK. The motion tokens are added to your global CSS the first time you install a component.

Usage

import { Sparkline } from "@/components/keyframery/sparkline"

export default function Usage() {
  return <Sparkline data={[12, 14, 13, 18, 21, 19, 24]} label="Revenue" period="last 7 days" />
}

Examples

Coloured by its news

trendColor draws a rising line green and a falling one red. Refunds should fall, so downIsGood turns the colour round.

Bars, a target and the high and low

type="bar" grows the bars one after another. reference draws a dashed line at a value; extremes marks the highest and lowest point.

Read the point under the pointer

onActiveIndexChange tells you which point someone is reading, by pointer or arrow keys, so a headline can follow it.

Motion

MoveWhat you see
DrawA line draws itself from the left with its area unrolling under it; the last point pops in once the line reaches it.
CascadeBars grow from the baseline one after another, left to right, in about half a second whatever their number.
SpringNew data tweens the line, the area, every bar, the reference line and the last point to their new places. Up to 97 points, any length tweens into any other.
GlideWhile someone scrubs, a hairline and a dot ride to the point under the pointer and the readout follows them, swinging inward near the edges.
RollThe readout's value rolls to each new number.

Timing and springiness follow the motion style you set. To change how fast this sparkline moves, pass speed:

<Sparkline speed="fast">
  …
</Sparkline>

"fast" is 0.6×, "slow" 1.4×, and a number sets the multiplier. With reduced motion turned on, nothing moves: changes happen at once or with a short fade.

Theming

Set these CSS variables on the component or a parent. Leave them unset to follow the light and dark theme. The open Customize panel below the preview lets you try these settings.

VariableWhat it changesDefault
--sparkline-colorThe marks, without trendColor--chart-1
--sparkline-positiveGood news, with trendColoroklch(0.62 0.16 150)
--sparkline-negativeBad news, with trendColor--destructive
--sparkline-heightThe height2.5rem
--sparkline-strokeThe line's width1.75px
--sparkline-dotThe dots' size6px
--sparkline-surfaceThe ring round the dots (the surface it sits on)--background; --card in dark
--sparkline-referenceThe dashed reference line--muted-foreground

API reference

Every part takes className and children, along with the props of the element it renders. The parts are rendered by UI SDK, so they also take render, to draw your own element, and a function of their state for className and style.

Sparkline

Renders a <div>, and takes its props too.

Prop

Type

Roll

Prop

Type

SparklineBars

Renders a <div>, and takes its props too.

Prop

Type

Other parts

These take the props of the element they render, and nothing else:

  • SparklinePlot: a <div>
  • SparklineArea
  • SparklineBar: a <div>
  • SparklineCursor: a <div>
  • SparklineEnd: a <div>
  • SparklineExtreme: a <div>
  • SparklineLine
  • SparklineMarker: a <div>
  • SparklinePoint: a <div>
  • SparklinePoints: a <div>
  • SparklineReadout: a <div>
  • SparklineReference: a <div>
  • SparklineSvg

Keyboard

KeyWhat it does
TabMoves focus to the sparkline's points as one tab stop and shows the point's value.
ArrowRightMoves to the next point and shows its value.
ArrowLeftMoves to the previous point.
HomeMoves to the first point.
EndMoves to the last point.
EscapeHides the readout; any arrow brings it back.

Accessibility

Roles, keyboard and focus are declared on the component's UI SDK primitive, and state is announced through the matching ARIA attributes. More in Accessibility.

Built on UI SDK's core

UI SDK doesn't ship a sparkline, so this one is defined with its core: one definePrimitive in components/keyframery/core/sparkline-core.ts, with no React in it. UI SDK's adapters can render that same definition in Vue, Svelte, Solid and web components. To keep your own styling and take only the motion, import from @/components/keyframery/primitives/sparkline.

Questions

How does the animated Sparkline for UI SDK move?

Draw: A line draws itself from the left with its area unrolling under it; the last point pops in once the line reaches it. Cascade: Bars grow from the baseline one after another, left to right, in about half a second whatever their number. Spring: New data tweens the line, the area, every bar, the reference line and the last point to their new places. Up to 97 points, any length tweens into any other. Glide: While someone scrubs, a hairline and a dot ride to the point under the pointer and the readout follows them, swinging inward near the edges. Roll: The readout's value rolls to each new number.

Do I need Motion or Framer Motion for the animated Sparkline?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

UI SDK has no sparkline. How is this one built on UI SDK?

Its behaviour is one definition written with UI SDK's core (definePrimitive), in components/keyframery/core/sparkline-core.ts. Its parts are rendered by UI SDK's adapter, so they take render, state-based class names and data-slot, like every UI SDK component.

What happens to the Sparkline when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.

All components

On this page