An equalizer, a filter response or a compressor curve: a surface with two axes, on which you draw curves and live traces and place draggable handles in the axes' units. The registry component is written on the plot primitive, which installs with it.
Installation
The primitives are built on the UI SDK kernel, in beta. Nothing of ours comes from npm: the primitive and its adapter are files you copy into your project and own, and the only packages are the kernel's. The commands name its version, @ui-sdk/*@0.1.0-beta.14.
The styled component, with a frame, a graph layer and themed parts, is one command:
For the equalizer above, with its pink noise and spectrum, ready to run: npx shadcn@latest add @audio-ui/plot-demo.
The styled component, with size variants and theme styles:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/plot
Or the primitive alone, to style yourself:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/plot-react
It writes the primitive, its adapter and primitive.ts into components/audio-ui/ and installs the kernel's packages and what each file imports. The optional styles are one more command:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/primitives-css
Usage
Copy to Clipboardimport { Plot, PlotArea, PlotCurve, PlotGraph, PlotGrid, PlotHandle, } from "@/components/audio-ui/plot"; import { eqResponse } from "@/lib/engine/analysis"; <Plot minX={20} maxX={20000} minY={-18} maxY={18} scaleX="log"> <PlotGrid x={{ count: 6 }} y={{ ticks: [-12, 0, 12] }} /> <PlotGraph> <PlotArea fillTo={0} fn={response} /> <PlotCurve fn={response} /> </PlotGraph> <PlotHandle aria-label="Band 1" onValueChange={({ x, y }) => setBand({ frequency: x, gainDb: y })} value={{ x: band.frequency, y: band.gainDb }} /> </Plot>;
fn is y = fn(x) in the axes' units. @/lib/engine/analysis gives you the functions for the usual curves: eqResponse, biquadResponse and compressorOutput, and for keeping a band's handle on the curve, bandHandleDb (where it sits) and bandGainForHandle (the gain that puts it where the pointer is).
The primitive alone, part by part:
A Root is the surface. Everything you put in it is placed by value, not by pixel:
- Grid: lines and labels along each axis.
- Curve: a function of x, drawn as a path.
- Trace: a live list of values, drawn straight in the DOM.
- Line, Band, Mark, Dot: a value or a range on an axis, a point.
- Handle: a draggable point with a native
<input type="range">for each axis it moves on, so the keys and a screen reader work. - Cursor and CursorReadout: a marker that follows the pointer, and the text of the value under it.
Copy to Clipboardimport { useState } from "react"; import { Plot } from "@/components/audio-ui/plot/react"; export function Eq() { const [peak, setPeak] = useState({ x: 1000, y: 6 }); const response = (hz: number) => peak.y * Math.exp(-(Math.log2(hz / peak.x) ** 2)); return ( <Plot.Root className="h-48 w-full" maxX={20_000} maxY={12} minX={20} minY={-12} onValueChange={setPeak} scaleX="log" > <Plot.Grid className="text-muted" x={{ ticks: [100, 1000, 10_000] }} y={{ ticks: [-12, 0, 12] }} /> <Plot.Curve className="text-primary" fn={response} /> <Plot.Handle aria-label="Peak" className="size-3 rounded-full bg-primary" handle="peak" value={peak} /> </Plot.Root> ); }
Live traces and dots
A Trace takes a source, a live list of values (a spectrum, a waveform) in the shape of an external store: getSnapshot() returns the values and subscribe(listener) is called on each frame. Its path is written straight onto the DOM, so a spectrum at 60 frames renders nothing. A Dot takes a value or a source of a point the same way. Any object with those two methods works, with nothing to install, and the registry's useSpectrum gives you one over an analyser.
Handles
A handle holds a native <input type="range"> per axis, so it works from the keyboard: the arrow keys move it by a hundredth of the axis, a tenth with Shift, and Home / End go to the ends. Dragging moves it from where it is rather than jumping to the pointer, and four times finer with Shift. A double click, an Alt + click or Delete return it to defaultValue, or the middle of the plot (reason: "reset"). axis="x" or axis="y" holds it to one axis.
Live signals
PlotTrace draws a list of y values from a source straight in the DOM, with no React render per frame: the spectrum of the sound behind an equalizer, on a scale of its own (min, max). PlotDot is a point that follows a live { x, y }, such as the input level riding a compressor's curve. The use-spectrum hook turns an AnalyserNode into the source a trace wants, resampled onto a logarithmic frequency axis, and the default example plays pink noise through the equalizer so you hear what you draw.
Copy to Clipboardconst spectrum = useSpectrum(analyser, { minHz: 20, maxHz: 20000 }); <PlotGraph> <PlotTraceArea fillTo={-100} min={-100} max={-10} source={spectrum} /> <PlotCurve fn={response} /> </PlotGraph>;
The hook's options are in Analysis.
Interaction
Handles
- A press and a drag move the point on the axes it has (
axisis"both","x"or"y"). Shift with a drag moves it finely, Alt with a press or Delete returns it to itsdefaultValue. - On a handle's inputs, the arrows move it by a small step, Shift with an arrow by a large one, Home and End go to the ends of the range.
Cursor
- It follows a mouse or a pen over the plot, and sets
data-activewhile the pointer is on it. It takes no touch, which would scroll. - Its
--plot-cursor-xand--plot-cursor-yCSS variables hold its place, in percent.
A disabled plot takes none of these.
API reference
Registry component
PlotCursor takes a format function, (value) => string, for its readout; PlotTraceArea is a Trace drawn as a filled area. The rest take the primitive's props.
Options
Set on the Root, as props (an attribute in kebab case in the web adapter).
| Option | Type | Default | |
|---|---|---|---|
minX, maxX, minY, maxY | number | 0, 1 | The range of each axis, in your units. |
scaleX, scaleY | "linear", "log" or { to, from } | "linear" | How values spread along an axis: a { to, from } pair maps values into an even space and back. |
disabled | boolean | false | Takes no gesture. |
onValueChange | (value, details) => void | A handle moved. details.cancel() refuses it. | |
onValueCommitted | (value, details) => void | A handle's gesture, key or input ended. |
A Handle takes handle (its name), axis, value or defaultValue ({ x, y }) and disabled. A Grid takes x and y, each { ticks, count, labels, format }. A Curve takes fn and fillTo, and a Trace source, min, max and fillTo. A Line and a Mark take axis and value, a Band axis, from and to.
What a change reports
details carries:
handle: the name of the handle that moved, since one plot holds several.reason: what moved it,"drag","keyboard","input-change"or"reset".source:"mouse","touch","pen","keyboard","programmatic"or"agent".eventandcancel(): the DOM event behind it, and the refusal. A controlled handle keeps the value you give it.
In the web adapter, onValueChange is an input event and onValueCommitted a change event, with detail: { handle, value, reason, source }, and event.preventDefault() refuses a change.
Styling
The parts draw geometry and carry no look of their own: until you style them, a Curve is a filled black shape, the grid lines have no stroke and the labels sit at the corner of their value. The examples above give a Curve text-primary, which only colors it once the path has a stroke. A starting point, in plain CSS, that you then own:
Copy to Clipboard[data-slot="plot"] { color: #555; } [data-slot="plot-curve"] path { fill: none; stroke: currentColor; stroke-width: 2; } [data-slot="plot-grid"] [data-part="line"] { stroke: #ddd; } [data-slot="plot-grid"] [data-part="line"][data-zero] { stroke: #aaa; } [data-slot="plot-grid"] [data-part="label"][data-axis="x"] { bottom: 4px; transform: translateX(-50%); } [data-slot="plot-grid"] [data-part="label"][data-axis="y"] { left: 6px; transform: translateY(-50%); }
The registry's styled plot element already carries these.
States to style from
data-draggingon a handle while it is held,data-handlefor its name anddata-axisfor the axes it moves on.data-activeon the Cursor while the pointer is over the plot.data-fillon a Curve or a Trace givenfillTo: an area, to draw apart from a line.data-disabledon every part.
Parts
Parts · 11
axis: string, from: number, to: number.format: value.fillTo: number, fn: value, samples: number.source: value, value: value.x: value, y: value.axis: string, defaultValue: value, disabled: boolean, handle: key, value: value.axis: string, value: number.axis: string, value: number.fillTo: number, max: number, min: number, source: value.Events
Events · 2
@value-change. Web: the input event.@value-committed. Web: the change event.