scrollfx

Revenue

$48.2k

+12% on last month

Active users

3,904

+4% on last month

Churn

1.8%

−0.3% on last month

  1. 1variant · area
  2. 2color · line, dot

Schedule of props

MarkPropTypeDefaultDescription
P1datanumber[]SAMPLEThe values, left to right.
P2variant'line' | 'area''line'A line alone, or a line over a hatched area.
P3curveCurve'smooth'How the pen joins the points.
P4dotbooleantrueA dot on the last value.
P5colorstringACCENTThe line's colour; the hatching is in the ink.
P6labelstring—What it shows, for screen readers. Defaults to the first and last values.
P7triggerChartTrigger'scroll'`scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view.
P8durationnumber1200With `view`, how long the line takes, in ms.
P9classNamestring'h-8 w-28'—

Notes

  1. 1.
    A tiny inline line for tables and stat cards, drawn as it reaches the middle of the screen, with an optional hatched area and an end dot.
  2. 2.
    Labels, ticks, the legend and a data table for screen readers are in the server HTML. The strokes are made in the browser at the chart’s measured size, so the hatching keeps its spacing at any width.
  3. 3.
    Tied to the scroll by default: the last stroke lands as the drawing reaches the middle of the screen. With trigger="view" it plays once as it comes into view instead. Reduced motion shows it drawn.
  1. 4.
    Detail A, In stat cards. variant="area" hatches under the line; size it with className.
  2. 5.
    Usage, as drawn:
    import { Sparkline } from '@/components/scrollfx/sparkline';
    
    export function StatCards() {
      const stats = [
        { label: 'Revenue', value: '$48.2k', change: '+12%', data: [21, 24, 22, 27, 26, 31, 30, 34, 37, 36, 41, 45] },
        { label: 'Active users', value: '3,904', change: '+4%', data: [310, 330, 325, 340, 338, 352, 349, 361, 358, 372, 380, 390] },
        { label: 'Churn', value: '1.8%', change: '−0.3%', data: [2.6, 2.5, 2.6, 2.3, 2.4, 2.2, 2.1, 2.2, 2.0, 1.9, 1.9, 1.8] },
      ];
      return (
        <div className="@container">
          <div className="grid gap-4 @2xl:grid-cols-3">
            {stats.map((s) => (
              <div key={s.label} className="rounded-[3px] border bg-card p-5">
                <p className="font-mono text-[11px] uppercase tracking-[0.16em] text-muted-foreground">{s.label}</p>
                <div className="mt-3 flex items-end justify-between gap-3">
                  <p className="font-serif text-3xl tracking-[-0.02em] tabular-nums">{s.value}</p>
                  <Sparkline data={s.data} variant="area" className="h-10 w-24" label={`${s.label}, last 12 months`} />
                </div>
                <p className="mt-2 text-xs text-muted-foreground">{s.change} on last month</p>
              </div>
            ))}
          </div>
        </div>
      );
    }
    
View source · components/scrollfx/sparkline.tsx
'use client';

/**
 * sparkline — a tiny line, the size of a word, for tables and stat cards. A
 * pen draws it left to right as it reaches the middle of the screen, over a
 * hatched area if you like, and ends on a dot in the accent colour.
 *
 * Built on svg-scroll-draw (MIT): https://github.com/DhruvilChauahan0210/scrollfx
 * Needs: react, svg-scroll-draw, lib/chart and lib/plotter (installed with it).
 * Styling: Tailwind CSS with shadcn/ui tokens. Size it with `className`
 * (h-8 w-28 by default); it sits inline, on the text's baseline.
 *
 * Its label for screen readers is in the server HTML; the strokes are made in
 * the browser at its size. Reduced motion shows it drawn.
 */

import { useMemo } from 'react';
import 'svg-scroll-draw/no-flash.css';
import { ACCENT, PenLayers, circle, curvePath, curvePoints, hatchMarks, linear, useSize, type ChartTrigger, type Curve, type Mark, type Pen, type Pt } from '@/lib/chart';

export interface SparklineProps {
  /** The values, left to right. */
  data?: number[];
  /** A line alone, or a line over a hatched area. */
  variant?: 'line' | 'area';
  /** How the pen joins the points. */
  curve?: Curve;
  /** A dot on the last value. */
  dot?: boolean;
  /** The line's colour; the hatching is in the ink. */
  color?: string;
  /** What it shows, for screen readers. Defaults to the first and last values. */
  label?: string;
  /** `scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view. */
  trigger?: ChartTrigger;
  /** With `view`, how long the line takes, in ms. */
  duration?: number;
  className?: string;
}

const SAMPLE = [12, 14, 13, 17, 16, 21, 19, 24, 23, 28, 27, 31];

export function Sparkline({ data = SAMPLE, variant = 'line', curve = 'smooth', dot = true, color = ACCENT, label, trigger = 'scroll', duration = 1200, className = 'h-8 w-28' }: SparklineProps) {
  const [ref, size] = useSize<HTMLSpanElement>();
  const lo = Math.min(...data), hi = Math.max(...data);

  const pens = useMemo((): Pen[] | null => {
    if (!size || data.length < 2) return null;
    const { w, h } = size;
    // Room for the pen's width and the end dot, so nothing is clipped.
    const pad = dot ? 4 : 2;
    const y = linear(lo, hi === lo ? lo + 1 : hi, h - 2, 2);
    const pts: Pt[] = data.map((v, i) => [pad / 2 + (i / (data.length - 1)) * (w - pad * 1.5), y(v)]);
    const fill: Mark[] = variant === 'area'
      ? hatchMarks([...curvePoints(pts, curve), [pts[pts.length - 1][0], h], [pts[0][0], h]], { angle: 90, gap: 3 }, { opacity: 0.55, width: 0.8, inset: 1.6 })
      : [];
    const [ex, ey] = pts[pts.length - 1];
    return [
      { marks: fill, from: 1.05, to: 0.7, spread: 2 },
      { marks: [{ d: curvePath(pts, curve), color, width: 1.6 }], from: 0.82, to: dot ? 0.56 : 0.5 },
      // The end dot: two turns of the pen, so it reads solid.
      { marks: dot ? [{ d: circle(ex, ey, 2.3), color, width: 1.5 }, { d: circle(ex, ey, 0.9), color, width: 1.6 }] : [], from: 0.6, to: 0.5, spread: 0.5 },
    ];
  }, [size, data, variant, curve, dot, color, lo, hi]);

  return (
    <span
      ref={ref}
      role="img"
      aria-label={label ?? `Trend from ${data[0]} to ${data[data.length - 1]}, ${data.length} values`}
      className={`relative inline-block align-middle ${className}`}
    >
      {size && pens ? <PenLayers size={size} pens={pens} trigger={trigger} duration={duration} /> : null}
    </span>
  );
}

Parts list

ItemPart, as installed
1components/scrollfx/sparkline.tsxThe component
2lib/chart.tsScales, ticks, curves, hatching and the pens the charts share
3lib/plotter.tsStroke geometry: hatching, sketched lines, scroll windows
4svg-scroll-draw ^2.29.0Dependency, from npm
ViewIn stat cards
Pens1ink2vermilion
Installnpx shadcn@latest add
https://scrollfx-ui.vercel.app/r/sparkline.json
Title

Sparkline

scrollfx · Charts
Dwg no.
sparkline
Sheet
07/14
Scale
1:1
Rev
2.29.0
Drawn by
svg-scroll-draw
Licence
MIT