Sparkline
A compact trend line with the latest reading and a native disclosure for the full data.
pnpm dlx shadcn@latest add @haze/sparkline---
import { ChevronDown } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Props {
label?: string;
samples?: { label: string; value: number }[];
unit?: string;
detailsLabel?: string;
emptyLabel?: string;
class?: string;
}
const {
label = "Request duration", unit = "ms", detailsLabel = "View samples", emptyLabel = "No samples yet.", class: className,
samples = [
{ label: "Mon", value: 176 },
{ label: "Tue", value: 193 },
{ label: "Wed", value: 181 },
{ label: "Thu", value: 169 },
{ label: "Fri", value: 154 },
],
} = Astro.props;
const valid = samples.filter(({ value }) => Number.isFinite(value));
const minimum = valid.reduce((lowest, point) => Math.min(lowest, point.value), Infinity);
const maximum = valid.reduce((highest, point) => Math.max(highest, point.value), -Infinity);
const scale = Number.isFinite(maximum - minimum) ? 1 : 2;
const span = maximum / scale - minimum / scale;
const format = (value: number) => unit ? `${value} ${unit}` : String(value);
const points = valid.map(({ value }, index) => ({
x: valid.length === 1 ? 110 : 4 + index * 212 / (valid.length - 1),
y: span === 0 ? 26 : 4 + 44 * ((maximum / scale - value / scale) / span),
}));
const last = valid.at(-1);
const end = points.at(-1);
---
<figure class={cn("grid min-w-0 gap-3 rounded-xl border border-border bg-card p-4.5 text-card-foreground", className)}>
<figcaption class="grid gap-1">
<span class="text-[0.8125rem]/5 font-medium text-muted-foreground">{label}</span>
{last && <span class="wrap-anywhere text-xl/7 font-medium tracking-[-0.01em] tabular-nums">{format(last.value)} <span class="text-[0.8125rem]/5 font-normal text-muted-foreground">{last.label}</span></span>}
</figcaption>
{end ? (
<>
<svg aria-hidden="true" viewBox="0 0 220 52" preserveAspectRatio="none" class="h-13 w-full">
<path d="M0 4H220M0 26H220M0 48H220" class="fill-none stroke-border" vector-effect="non-scaling-stroke" />
{points.length > 1 && <polyline points={points.map(({ x, y }) => `${x},${y}`).join(" ")}
class="fill-none stroke-brand stroke-2" stroke-linecap="round" stroke-linejoin="round" vector-effect="non-scaling-stroke" />}
<circle cx={end.x} cy={end.y} r="3" class="fill-brand" />
</svg>
<details class="group border-t border-border pt-2">
<summary class="flex min-h-7 cursor-pointer list-none items-center justify-between gap-2 rounded-sm text-[0.8125rem]/5 font-medium text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">
{detailsLabel}
<ChevronDown aria-hidden="true" class="size-3.5 transition-[rotate] duration-200 ease-snappy group-open:rotate-180" />
</summary>
<dl class="grid gap-1 pt-2 text-[0.8125rem]/5">
{valid.map(({ label: sample, value }) => <div class="flex min-w-0 flex-wrap items-start justify-between gap-x-3">
<dt class="wrap-anywhere text-muted-foreground">{sample}</dt>
<dd class="wrap-anywhere font-medium tabular-nums">{format(value)}</dd>
</div>)}
</dl>
</details>
</>
) : <p class="text-[0.8125rem]/5 text-muted-foreground">{emptyLabel}</p>}
</figure>When to use
- A small stat card needs recent trend context
- Readers need exact numeric samples as well as the line
When not to use
- The values correspond to unrelated categories. Use bar-list.
- Multiple series or axes need detailed comparison.
Installation
Add the Haze registry to components.json once, then install Sparkline with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/sparklinenpx shadcn@latest add @haze/sparklineyarn dlx shadcn@latest add @haze/sparklinebunx --bun shadcn@latest add @haze/sparklineFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | "Request duration" | Metric caption. |
| samples | { label: string; value: number }[] | – | Finite samples in time order, with a readable label for each. |
| unit | string | "ms" | Abbreviated unit, or an empty string. |
| detailsLabel | string | "View samples" | Label for the sample disclosure. |
| emptyLabel | string | "No samples yet." | Text displayed when there are no samples. |
| class | string | – | Additional classes on the figure. |
Keyboard interactions
- Enter or Space
- Toggle the list of samples when its summary is focused.
Accessibility
- The SVG is decorative. The latest value and the full list of samples are available as text.
- Samples are spaced evenly. Aggregate at a regular interval when the horizontal spacing carries meaning.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Sparkline" with `npx shadcn@latest add @haze/sparkline`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Sparkline from "@/components/haze/ui/sparkline.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/sparkline.md
Customize with real content
Adapt the Haze sparkline ui for our product. Replace the demo values of these props with our real content: label, samples, unit, detailsLabel, emptyLabel, class. Keep the layout, the heading level rules and these accessibility details: The SVG is decorative. The latest value and the full list of samples are available as text. Samples are spaced evenly. Aggregate at a regular interval when the horizontal spacing carries meaning. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using sparkline, check it fits: use it when a small stat card needs recent trend context; readers need exact numeric samples as well as the line. Avoid it when the values correspond to unrelated categories. use bar-list.; multiple series or axes need detailed comparison.. Alternatives: @haze/stat, @haze/bar-list, @haze/uptime-tracker.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.