Area Morph
An area chart that flows from one set of numbers to the next like a liquid, on the engine that morphs logos.
Preview
"use client";
import { useState } from "react";
import { useAutoplay } from "../demo-context";
import { AreaMorph } from "./AreaMorph";
const ranges = [
[20, 34, 28, 52, 46, 70, 64, 88],
[60, 42, 55, 38, 72, 50, 84, 76],
[12, 18, 40, 36, 62, 58, 80, 96],
];
export default function Demo() {
const [turn, setTurn] = useState(0);
useAutoplay(() => setTurn((t) => t + 1), 2000);
return <AreaMorph label="Visitors" values={ranges[turn % ranges.length]} max={100} labels={["Jan", "Mar", "May", "Jul", "Sep", "Nov"]} />;
}Installation
npx @usefluid/cli add area-morphpnpm dlx @usefluid/cli add area-morphyarn dlx @usefluid/cli add area-morphbunx @usefluid/cli add area-morphInstall the engine.
Terminal npm i @usefluid/motionCopy these files into your project.
components/fluid/area-morph/AreaMorph.tsx "use client"; import { MorphPath } from "@usefluid/motion/react"; import { type CSSProperties, useId } from "react"; import { areaPath } from "./area"; import styles from "./AreaMorph.module.css"; const FLOW = { w: 11, z: 0.72, spread: 0.35 }; type Props = { label: string; values: number[]; max?: number; labels?: string[]; color?: string; }; export function AreaMorph({ label, values, max, labels, color }: Props) { const gradient = `fluid-area-${useId().replace(/[^a-zA-Z0-9]/g, "")}`; const d = areaPath(values, max); return ( <figure className={styles.root} style={color ? ({ "--fluid-accent": color, "--fluid-on-accent": "#ffffff" } as CSSProperties) : undefined}> <svg viewBox="0 0 120 120" preserveAspectRatio="none" className={styles.chart} role="img" aria-label={`${label}: ${values.join(", ")}`}> <defs> <linearGradient id={gradient} x1="0" y1="0" x2="0" y2="1"> <stop offset="0" style={{ stopColor: "var(--fluid-accent)" }} /> <stop offset="1" style={{ stopColor: "var(--fluid-accent)", stopOpacity: 0.12 }} /> </linearGradient> </defs> <MorphPath d={d} fill={`url(#${gradient})`} config={FLOW} /> </svg> {labels && ( <div className={styles.axis} aria-hidden> {labels.map((l) => ( <span key={l}>{l}</span> ))} </div> )} </figure> ); }components/fluid/area-morph/area.test.ts import { describe, expect, it } from "vitest"; import { parsePath, sampleContours } from "../../packages/motion/src/index"; import { areaPath } from "./area"; describe("areaPath", () => { it("draws one closed shape that fills the 120 box from edge to edge", () => { const d = areaPath([3, 8, 5, 12, 9], 12); expect(() => parsePath(d)).not.toThrow(); const [c] = sampleContours(d); expect(sampleContours(d)).toHaveLength(1); expect(Math.min(...c.x)).toBeCloseTo(0, 1); expect(Math.max(...c.x)).toBeCloseTo(120, 1); expect(Math.max(...c.y)).toBeCloseTo(120, 1); }); it("puts the highest value near the top and keeps a floor for zeros", () => { const [c] = sampleContours(areaPath([0, 10, 0], 10)); expect(Math.min(...c.y)).toBeLessThan(20); expect(Math.min(...c.y)).toBeGreaterThanOrEqual(0); const [flat] = sampleContours(areaPath([0, 0, 0], 10)); expect(Math.min(...flat.y)).toBeLessThan(120); }); it("handles a single value and an empty list", () => { expect(() => sampleContours(areaPath([4], 10))).not.toThrow(); expect(() => sampleContours(areaPath([], 10))).not.toThrow(); }); });components/fluid/area-morph/area.ts const BOX = 120; const TOP = 8; const FLOOR = 6; const clamp = (v: number) => Math.min(BOX, Math.max(0, v)); const r = (v: number) => Math.round(v * 100) / 100; export function areaPath(values: number[], max = Math.max(1, ...values)): string { const span = BOX - TOP - FLOOR; const heightOf = (v: number) => clamp(BOX - FLOOR - (Math.max(0, v) / (max || 1)) * span); const ys = values.length ? values.map(heightOf) : [BOX - FLOOR]; if (ys.length === 1) ys.push(ys[0]); const step = BOX / (ys.length - 1); const pts = ys.map((y, i) => [i * step, y] as const); let d = `M0 ${BOX}L0 ${r(pts[0][1])}`; for (let i = 0; i < pts.length - 1; i++) { const p0 = pts[Math.max(0, i - 1)]; const p1 = pts[i]; const p2 = pts[i + 1]; const p3 = pts[Math.min(pts.length - 1, i + 2)]; const c1x = p1[0] + (p2[0] - p0[0]) / 6; const c1y = clamp(p1[1] + (p2[1] - p0[1]) / 6); const c2x = p2[0] - (p3[0] - p1[0]) / 6; const c2y = clamp(p2[1] - (p3[1] - p1[1]) / 6); d += `C${r(c1x)} ${r(c1y)} ${r(c2x)} ${r(c2y)} ${r(p2[0])} ${r(p2[1])}`; } return `${d}L${BOX} ${BOX}Z`; }components/fluid/area-morph/AreaMorph.module.css .root { display: grid; gap: 10px; width: min(100%, 460px); margin: 0; padding: 22px 22px 16px; border-radius: 24px; background: var(--fluid-surface); color: var(--fluid-text); font-family: var(--fluid-font); } .chart { display: block; width: 100%; height: 170px; overflow: visible; } .axis { display: flex; justify-content: space-between; color: var(--fluid-text-2); font-size: 12px; }Import the theme once, then point its variables at your own tokens.
layout.tsx import "@usefluid/motion/theme.css";
Usage
import { AreaMorph } from "./AreaMorph";
<AreaMorph label="Visitors" values={[20, 34, 28, 52, 46, 70]} max={100} labels={["Jan", "Mar", "May"]} />API reference
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Accessible name of the chart. |
values | number[] | — | The points, from left to right. Change them and the area flows to the new shape. |
max | number | the largest value | The value at the top. Keep it fixed when the values change, so shapes compare. |
labels | string[] | — | Text under the chart, spread from left to right. |
Keyboard and screen readers
The chart is an image named with its label and its values. With reduced motion the area changes at once.
Notes
areaPath(values, max) in area.ts draws the area in the 120 box, if you want the shape in your own svg. Every colour comes from the --fluid-* variables, and the piece is yours to change.
How it is made
The values become a smooth closed shape in the 120 box, and the morph engine flows it to the next shape: every point of the outline has its own spring, so the area moves like water settling. At rest the exact drawing of the data comes back. The chart is an image read with its label and values.
For your agent
For your agent
Add Area Morph to my project with the Fluid library. Use AreaMorph for the trend charts of my dashboard, switching ranges such as week, month and year. Read my project first. Use my logo, my colours and my font through the --fluid-* variables. Keep 44px touch targets, visible focus and reduced motion. If I have no SVG logo, ask before making one up.