Sparkline

A trend in the width of a table cell. The densest thing in the package: it exists so a `MetricTable` row can show *shape* without spending a chart's worth of pixels, which is the whole roic.ai lesson — one row that reads a decade beats one chart that reads…

data displayserver320pxuiv1.2.0

Preview

Sparkline — live rendercharts-sparkline--in-context

Rendered from the story the storybook (a11y) CI gate runs axe against — the preview can't show something that hasn't been verified.

Install

Two equivalent paths — the URL works in any shadcn-CLI setup; the alias works once you've registered @interlace in your components.json.

Via shadcn URL
npx shadcn@latest add https://ds.interlace.tools/r/sparkline.json
With the @interlace alias
npx shadcn@latest add @interlace/sparkline

Behavior

What this component does — the keyboard path, the measured colour, the text equivalent and the states it can be in. Every figure below is read out of the test or the source that enforces it by scripts/build-behavior-map.mjs, so nothing here can outlive the gate that keeps it true.

9 states, one closed union — not a boolean per condition. Partial is a state, not an absence: a spinner that hides progress, or a missing prior rendered as 0, is the failure this model exists to make impossible.

StateReads asRoleEncoding
loadingloadingStands instead of the bodymuted
error! errorStands instead of the bodydanger
not-applicablen/aStands instead of the bodyhatchedrecede
not-countednot countedStands instead of the bodyhatchedmuted
emptynoneStands instead of the bodymuted
partialpartialStands next to the body and changes how to read itdashedmuted
truncatedtruncatedStands next to the body and changes how to read itdashedmuted
first-measurement· first measurementStands next to the body and changes how to read itdashedaccent
idleThe absence of absencemuted

Hatched and dashed are the two absences drawn apart on purpose: not-counted means no measurement was taken and is never a zero; truncated means the total is unknown and must never be used as a denominator. Each state also carries a spoken announcement — transitions, never ticks.

Where it renders, how narrow it survives, and whether its lines are inside the coverage gate — three answers a consumer needs before the paste, not after the bug.

Render boundary
server component

Renders during SSR / RSC with no hydration cost. It can be imported from a server component without pulling the page into the client.

Minimum viewport
MIN_VIEWPORT = 320

Declared, not assumed. Below 320 CSS px the preflight contract draws a dev-mode outline — how to switch it on.

Coverage gate
100% × 4

Inside the v8 gate: lines 100, statements 100, functions 100, branches 100. A threshold under 100 only records how much you stopped caring, so there isn't one.

History

This component is at v1.2.0, first shipped in @interlace/ui 1.0.0. The version is stamped as a banner into the file the install writes, so the copy in your tree says which one you have — compare it with the number above before deciding whether to re-run the install.

@interlace/ui 1.1.01 entry

  • Addedminor

    New Distribution chart — a quantity spread over a fixed set of named bins, read against a reference distribution. It is a new component rather than an extension of TimeSeries because three of that component's properties are chronological and cannot be parameterised out: its axis keys are sorted (['Thu','Fri','Sat'] sorts into a week that does not exist), a line asserts the metric passed through every value between two samples when between two hourly aggregates there is nothing to pass through, and delta() first-to-last is arithmetic performed on a circle. An unmeasured bin **hatches**. Everywhere else a null can simply be dropped, but for bars a height of zero and a bar never drawn are the same picture — this is null ≠ zero at its hardest case. bin.note prints a second axis reading under the first, replacing the timezone toggle a caller would otherwise build; a toggle hides half the truth and is missing from every screenshot. Also: error states on every chart that fetches — TimeSeries, Sparkline, MetricTable, NetworkGraph, Distribution — resolved through resolveDataState so the precedence is the array in data-state-model rather than five hand-rolled ladders. In a dashboard "the fetch failed" and "there is no data" are different statements. Delta and SeriesTable get none: neither has a fetch lifecycle. StatItem gains tone, carried by three signals rather than a hue alone — the rail, the value colour, and an sr-only judgement.

@interlace/ui 1.0.01 entry

Import

25 named exports — pull the parts you need.

Public API
import { ANNOTATION_KINDS, AnnotationKind, Direction, MIN_VIEWPORT, NumericPoint, Sparkline, areaPath, axisSlots, bandScales, compact, day, delta, describeDistribution, describeSeries, keepAtNarrow, linePath, nearestIndex, nearestSlot, numeric, peakBin, plotScales, seriesScales, slotAt, stepPath, ticks } from '@/components/ui/charts/sparkline';

API reference

Parsed from the type declarations in the source — the same file the install writes into your tree, so this table can't drift from the component you get.

SparklineProps

Also accepts every <svg> attribute.

PropTypeDescription
points*readonly Point[]
widthnumber
heightnumber
decorativebooleanSet when the same numbers are already announced adjacently — in a `MetricTable` row the value and delta cells say it, so a second announcement is noise. Renders `aria-hidden` instead of `role="img"`.
labelstringName used in the accessible label. Ignored when `decorative`.
polarityPolarityWhich direction counts as good. `inverse` for latency, cost, error rate, open issues. Without this the sparkline coloured purely by DIRECTION, so an inverse-polarity row rendered a red line beside a green delta — the same row asserting "bad" and "good" simultaneously. Only visible by looking at it; every unit test passed.
loadingbooleanRender a `<Skeleton variant="sparkline" />` placeholder at the exact inline cell size, so a metric arriving mid-window does not reflow its column.
errorunknownThe fetch failed. The empty form of this component is `aria-hidden` — correct, because in a `MetricTable` row the numbers beside it already say there is no trend. A FAILED row is the opposite case: nothing else in the row knows, so this is the only element that can say so, and it says it out loud.
announceAnnouncementOptionsContext for the absence sentence — noun, coverage, reason.

Skeleton

Pass loading and the component renders a shape-matched <Skeleton /> in its own place — same box, so nothing reflows when the data lands. Below is the same story with loading={true}.

Sparkline — loading statecharts-sparkline--loading
Usage
<ANNOTATION_KINDS loading={isPending} />

Accessibility

Every story for this component is rendered headlessly and checked with axe-core (wcag2aa, wcag22aa, best-practice, ACT) on every PR. That gate has no continue-on-error, so what ships has zero known violations.

What follows is what static analysis can see. Axe cannot press a key and never sees an overlay open, so the operable-without-a-mouse claim lives in Behavior instead, where the keyboard path is replayed step by step.

Focus + keyboard behaviour
Native element semantics — no interaction layer to get wrong.
Focus ring (WCAG 2.2 SC 2.4.13)
Not focusable — no focus indicator required.
Reduced motion
No animation to gate.
ARIA in the source
  • role="img"

Examples

8 more states from the same story file.

R-rule compliance

Every primitive in @interlace/ui models to the portable 26-rule floor enforced by the componentApi ESLint preset. The cells below pin exactly where each rule applies in this file.

RuleConceptWhere
R4Extends the element props`ComponentProps<'svg'>` minus what we own
R6data-slot`data-slot="sparkline"`
R7className merged + ...rest`cn(...)` + `{...props}`
R8No `isXxx``decorative`, not `isDecorative`
R13Ecosystem firstno charting dep — SVG + `seriesScales` is the engine
R14Declares min viewport`data-min-viewport={String(MIN_VIEWPORT)}`
R18Tailwind onlyzero inline `style`; sizing via width/height attributes
R19Tokens only`stroke-viz-positive` / `stroke-viz-negative`
R20AA contrastboth tokens alias `--success` / `--destructive` (≥7:1)
R23Absence is a vocabulary`loading` / `error` / no-trend are three different cells
R26A11y`role="img"` + computed label, or `aria-hidden` when decorative

Minimum viewport

This primitive declares MIN_VIEWPORT = 320 CSS px (DESIGN_PRINCIPLES #14). When mounted in a container narrower than this, the preflight contract draws a dev-mode outline so the regression is visible during local development.

Opt-in to the dev-mode warning
<body data-interlace-dev>...</body>

Add the data-interlace-dev attribute to the body in development builds only — preflight then outlines any primitive whose container is below its declared data-min-viewport.

Dependencies

Base UI primitive
Native / no Base UI dependency
Lucide icons
none
NPM dependencies
none

Source

The full implementation — components/ui/charts/sparkline.tsx once installed.

199 lines · TypeScript
import * as React from 'react';

// @interlace/sparkline v1.2.0 — Interlace design system.
// Docs, props and live preview: https://ds.interlace.tools/c/sparkline
// What changed since: https://ds.interlace.tools/c/sparkline#history
// Generated banner — keep it, the upgrade diff reads this version.

/**
 * @interlace/ui — Sparkline
 *
 * A trend in the width of a table cell. The densest thing in the package: it
 * exists so a `MetricTable` row can show *shape* without spending a chart's
 * worth of pixels, which is the whole roic.ai lesson — one row that reads a
 * decade beats one chart that reads a quarter.
 *
 * ## Colour is never the only signal
 *
 * Rising draws in `--viz-positive`, falling in `--viz-negative`. Roughly 8% of
 * men cannot tell those apart, so the direction is ALSO in the accessible name
 * ("up 12.4%") and in the end-cap position. If you find yourself removing the

…179 more lines…

Registry JSON

The raw registry record — what the shadcn CLI fetches.