component
Sparkline
Which way something has been going, as a small line with a dot at now. Green when that's good, red when it isn't, grey when it's neither.
NeutralPositiveNegativeimport { Sparkline } from 'rost/components/sparkline/Sparkline'
<Sparkline tone="positive" data={incomeByMonth} label="Income, last seven months, up to €12,000" /> src/components/sparkline/Sparkline.tsx
import type { HTMLAttributes } from 'react'
import './sparkline.css'
export interface SparklineProps extends Omit<HTMLAttributes<HTMLSpanElement>, 'children'> {
/** The values, oldest first. Two or more. */
readonly data: readonly number[]
/** Whether the way it's going is good. */
readonly tone?: 'neutral' | 'positive' | 'negative'
/** What it shows and where it ended, in words. */
readonly label: string
/** In px. The height is 32. */
readonly width?: number
}
const b = 'rost-sparkline'
const H = 32
const PAD = 3
/** The points of a sparkline, in its own px: what the SVG and the Figma drawing both use. */
export function sparklinePoints(data: readonly number[], width: number): [number, number][] {
const lo = Math.min(...data), hi = Math.max(...data)
const step = data.length > 1 ? (width - PAD * 2) / (data.length - 1) : 0
return data.map((v, i) => [PAD + i * step, hi === lo ? H / 2 : PAD + (1 - (v - lo) / (hi - lo)) * (H - PAD * 2)])
}
/**
* Which way something has been going, as a small line with a dot at now.
* Built from contracts/sparkline.contract.yaml.
*/
export function Sparkline({ data, tone = 'neutral', label, width = 120, className, style, ...rest }: SparklineProps) {
const pts = sparklinePoints(data, width)
const [x, y] = pts[pts.length - 1] ?? [0, 0]
return (
<span role="img" aria-label={label} className={[b, `${b}--tone-${tone}`, className].filter(Boolean).join(' ')} style={{ width, ...style }} {...rest}>
<svg className={`${b}__svg`} width={width} height={H} viewBox={`0 0 ${width} ${H}`} aria-hidden="true">
<polyline className={`${b}__line`} points={pts.map((p) => p.join(',')).join(' ')} />
<circle className={`${b}__dot`} cx={x} cy={y} r={3} />
</svg>
</span>
)
}
The source for everything on this page: contracts/sparkline.contract.yaml (opens in a new tab). The CSS, the docs and the checks in each app are built from it.
# yaml-language-server: $schema=../schema/component-contract.schema.json
schemaVersion: 2
id: sparkline
name: Sparkline
kind: component
status: review
summary: "Which way something has been going, as a small line with a dot at now. Green when that's good, red when it isn't, grey when it's neither."
category: display
related: [ stat, bar-chart, progress-ring ]
source:
tool: figma
fileKey: 1Zy0wiWKXQAvsfDBX3KR0d
nodeId: "753:22"
url: https://www.figma.com/design/1Zy0wiWKXQAvsfDBX3KR0d/UI-Kit-v01?node-id=753-22
extractedAt: "2026-11-17"
extractedBy: hand
frozenAt: "2026-11-17"
props:
data: { type: node, required: true, notes: "The values, oldest first, as numbers. Two or more." }
tone:
type: enum
values: [ neutral, positive, negative ]
default: neutral
notes: "Whether the way it's going is good. Up isn't always good: rising costs are `negative`."
label: { type: string, required: true, notes: "What it shows and where it ended, in words: Income, last seven months, up to €12,000." }
width: { type: number, default: 120, notes: "In px. The height is 32." }
elements:
root:
notes: "A `<span role=\"img\">` named by `label`, holding the SVG."
styles:
display: { value: inline-block, assert: false }
flex-shrink: { value: "0", assert: false }
height: { token: spacing/size/small-medium, value: 32px }
svg:
notes: "The drawing: as wide as `width`, 32px tall, with 3px kept clear on every side so the dot isn't cut."
styles:
display: { value: block, assert: false }
overflow: { value: visible, assert: false }
line:
notes: "The values joined, lowest at the bottom and highest at the top, a 2px line with round ends and joins."
styles:
fill: { value: none, assert: false }
stroke-width: { token: spacing/outline/medium, value: 2px }
stroke-linecap: { value: round, assert: false }
stroke-linejoin: { value: round, assert: false }
dot:
notes: "The last value, a 6px dot in the line's colour: now."
styles:
stroke: { value: none, assert: false }
variants:
- when: { tone: neutral }
figmaNode: "753:13"
elements:
line:
stroke: { token: sparkline/neutral, value: "#999999" }
dot:
fill: { token: sparkline/neutral, value: "#999999" }
- when: { tone: positive }
figmaNode: "753:17"
elements:
line:
stroke: { token: sparkline/positive, value: "#00d669" }
dot:
fill: { token: sparkline/positive, value: "#00d669" }
- when: { tone: negative }
figmaNode: "753:21"
elements:
line:
stroke: { token: sparkline/negative, value: "#f2705f" }
dot:
fill: { token: sparkline/negative, value: "#f2705f" }
guidance:
status: proposed
use:
- title: Next to a figure
text: "Beside a Stat, to say whether this month's number is part of a rise or a dip."
show: tone=positive
avoid:
- title: Reading values off it
text: "It has no axis on purpose. When the amounts matter, that's a **BarChart** or a **Table**."
- title: Tone as the only signal
text: "The Stat next to it says up or down in words. Red alone doesn't."
behaviour: "It scales the values to fit: the lowest touches the bottom, the highest the top, so it shows shape, not size. A flat series sits in the middle."
accessibility:
element: "`<span role=\"img\">`; the SVG inside is hidden from screen readers."
name: "`label`, which has to say what the line shows, since nobody can hear its shape."
colour: "A graphic that carries meaning: the grey is 7:1, the green 10:1, the red 7:1 on the dark page."
content: "The label ends with where it ended up: up to €12,000, not just Income."
Install
npx github:Runceanu/rost add sparkline It lands in src/rost/sparkline/, with what it needs and its contract. Install has the rest.
When to use
Next to a figure
Beside a Stat, to say whether this month's number is part of a rise or a dip.
When not to use
Reading values off it
It has no axis on purpose. When the amounts matter, that's a BarChart or a Table.
Tone as the only signal
The Stat next to it says up or down in words. Red alone doesn't.
Behaviour
It scales the values to fit: the lowest touches the bottom, the highest the top, so it shows shape, not size. A flat series sits in the middle.
Accessibility
- Element
<span role="img">; the SVG inside is hidden from screen readers. - Name
label, which has to say what the line shows, since nobody can hear its shape. - Colour
A graphic that carries meaning: the grey is 7:1, the green 10:1, the red 7:1 on the dark page.
Content
The label ends with where it ended up: up to €12,000, not just Income.
Props
| Prop | Type | Values | Default |
|---|---|---|---|
data | node | | |
tone | enum | neutral · positive · negative | neutral |
label | string | | |
width | number | | 120 |
Values
Straight from the contract. Each value sits next to its token, because six tokens here all come out as #3a3a3a and the colour alone can't tell you which one is right.
Never changes
root
-
height32pxspacing/size/small-medium
displayinline-block · flex-shrink0
svg
displayblock · overflowvisible
line
-
stroke-width2pxspacing/outline/medium
fillnone · stroke-linecapround · stroke-linejoinround
dot
strokenone
Changes by variant
3 variants. Each table is what one thing changes with: a column per tone, a row per property. Where every row starts with the same token path, it's written once, under the property.
Follows tone
| Property | Neutral | Positive | Negative |
|---|---|---|---|
dot.fillsparkline/… | neutral #999999 | positive #00d669 | negative #f2705f |
line.strokesparkline/… | neutral #999999 | positive #00d669 | negative #f2705f |