Sparkline
- Status
- Implemented
- Family
- 1 of 13
- Registry
- @lopifi/sparkline
npx shadcn@latest add @lopifi/theme @lopifi/sparklineThe component, rendering
Real data, the shipped component. Switch the theme to check the claims the spec makes below.
The four-layer spec
24–40px tall, no axes, no gridlines, no tooltip by default. --text-secondary stroke at 1.5px, or --positive/--negative by net direction, or neutral when polarity="neutral".
Optional endpoint dot and a single label at the last point.
Motion: stroke-dashoffset draw, 600ms --ease-out, once, on first paint or scroll-into-view ({ once: true }). Never on data change.
Touch: no scrub. A sparkline is an impression, not a chart. If a user needs values, they need MoneyChart.
Lifted verbatim from Primitives. The doc and this page are the same paragraphs, so they cannot disagree.
What lands in your repository
One component is never one file. Everything below is copied into your project, where you own it.
- Files
components/ledger/sparkline.tsx
- Lopifi dependencies · 2
- cnuse-media-query
- npm packages
- clsxtailwind-merge
- Registry entry
- /r/sparkline.json
The whole component
No compiled bundle, no black box. This is the file the registry copies, and the file you will edit.
"use client";
import * as React from "react";
import { useReducedMotion } from "@/hooks/ledger/use-media-query";
import { cn } from "@/lib/ledger/cn";
export interface SparklineProps extends React.ComponentPropsWithoutRef<"div"> {
data: number[];
height?: number;
label?: string;
polarity?: "standard" | "inverted" | "neutral";
showEndpoint?: boolean;
}
/**
* An impression, not a chart. No axes, no gridlines, no scrub — if a user
* needs values they need `MoneyChart`.
*
* Draws once, on first paint or scroll-into-view. Never on data change.
*/
export const Sparkline = React.forwardRef<HTMLDivElement, SparklineProps>(
({ className, data, height = 32, polarity = "standard", showEndpoint = true, label, ...props }, ref) => {
const drawRef = React.useRef<SVGGElement | null>(null);
const containerRef = React.useRef<HTMLDivElement | null>(null);
const reduced = useReducedMotion();
const drawn = React.useRef(false);
React.useEffect(() => {
const node = containerRef.current;
const group = drawRef.current;
if (!node || !group || reduced || drawn.current) return;
const observer = new IntersectionObserver((entries) => {
if (!entries[0]?.isIntersecting || drawn.current) return;
drawn.current = true;
group.style.animation = "ledger-wipe 600ms var(--ease-out) forwards";
observer.disconnect();
});
observer.observe(node);
return () => observer.disconnect();
}, [reduced]);
if (data.length < 2) return null;
const width = 120;
const min = Math.min(...data);
const max = Math.max(...data);
const span = max - min || 1;
const points = data.map((v, i) => ({
x: (i / (data.length - 1)) * width,
y: height - 2 - ((v - min) / span) * (height - 4),
}));
const d = points.map((p, i) => `${i === 0 ? "M" : "L"}${p.x.toFixed(2)},${p.y.toFixed(2)}`).join(" ");
const net = data[data.length - 1] - data[0];
const stroke =
polarity === "neutral"
? "var(--text-secondary)"
: (polarity === "standard") === net >= 0
? "var(--positive)"
: "var(--negative)";
const last = points[points.length - 1];
return (
<div
aria-label={label ?? `Trend, ${net >= 0 ? "up" : "down"} over ${data.length} points`}
className={cn("relative w-full", className)}
ref={mergeRefs(ref, containerRef)}
role="img"
{...props}
>
<svg
aria-hidden="true"
className="w-full"
preserveAspectRatio="none"
style={{ height }}
viewBox={`0 0 ${width} ${height}`}
>
<g ref={drawRef}>
<path
d={d}
fill="none"
stroke={stroke}
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={1.5}
vectorEffect="non-scaling-stroke"
/>
{showEndpoint ? (
<circle cx={last.x} cy={last.y} fill={stroke} r={2} vectorEffect="non-scaling-stroke" />
) : null}
</g>
</svg>
</div>
);
},
);
Sparkline.displayName = "Sparkline";
function mergeRefs<T>(...refs: (React.Ref<T> | undefined)[]) {
return (node: T) => {
for (const ref of refs) {
if (typeof ref === "function") ref(node);
else if (ref) (ref as React.MutableRefObject<T | null>).current = node;
}
};
}
Also in Primitives
- MoneyThe core primitive. A monetary value, formatted correctly.
- DeltaA signed change, with polarity and a redundant non-color cue.
- FigureA large display numeral with automatic size step-down.
- PercentPercent vs. percentage points, explicitly discriminated.
- RuleSingle / double / section accounting rule lines.
- StatusPillPayment and reconciliation states.
- TrendArrow + delta + optional sparkline, composed.
- AsOfTimestamp with live relative formatting and a stated timezone.
- RedactedMasked value with reveal.
- ReferenceCopyable transaction/account ID with disambiguated glyphs.
- CurrencyBadgeISO code + optional flag.
- EntityMarkBank/merchant logo with the full fallback ladder.
- TooltipHover/focus restatement. Never the only home of a fact.
- InfoTipThe ⓘ. Extra context that survives touch, because it opens on press.
See also
A component is specified in one place, defined in another, and running in a third. All three, from here.
Elsewhere in the system
Where Sparkline sits in the rest of the system — the live section it renders in, the calculators that compute what it displays, and the ledger payloads it is fed from.
Components are copied into your repository and meant to be edited — rounding policy, fiscal calendar and compliance copy differ per company.
Told when Primitives changes
Components are copied into your repo, so a fix here does not reach you on its own. One email when a component in this family changes in a way that needs a migration — and nothing when it does not.