Edits a ramp made of breakpoints, such as an ADSR shape or an automation curve. The registry component is written on the envelope primitive, which installs with it, and draws the points, the segments between them and a grid.
Overview
- Value:
valueis a list of{ x, y, curve? }points. Both axes are normalized to 0…1, and the points are sorted byx. Map that range to your own unit, such as time or level. - Curves: a point's
curve, from -1 to 1, bends the segment that leaves it. 0, or no value, draws a straight line. A positive curve holds the start of the segment and steepens its end; a negative curve is its mirror image, rushing the start and holding the end. The bend is the exponential of an analog envelope stage,(e^(k·t) - 1) / (e^k - 1)withkthe curve times 11, so neither end turns vertical. The last point has no segment. - Pointer: drag a point to move it. Double-click empty space to add a point, or double-click a point to remove it. A point stays between its neighbors, so points never cross.
- Point count: points are added and removed between
minPoints(default2, a start and an end) andmaxPoints. Set both to the same number to fix the shape. - Grid:
snapsets the grid that points snap to, for example0.25for quarters, or one step per axis,{ x: 0.125, y: 0 }, to snap across and leave the level free. Hold Cmd/Ctrl to move off the grid. Withoutsnap, points move freely. The registry component draws the grid. - Fine control: hold Shift while dragging to move a point at a quarter of the pointer's speed. The registry component shows the point's value above it while it is dragged or focused.
- Accessibility: the points share one tab stop. The arrow keys move the focused point, Page Up and Page Down (or Home and End) move between points, and Delete or Backspace removes it. Each point is a
role="slider". The registry component names the points "Point n of N". With the primitive, setaria-labelandgetAriaValueTextyourself.
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 grid, sizes and themed points, is one command:
The styled component, with size variants and theme styles:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/envelope
Or the primitive alone, to style yourself:
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/envelope-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 { Envelope } from "@/components/audio-ui/envelope";
Uncontrolled: set only the initial points.
Copy to Clipboard<Envelope defaultValue={[ { x: 0, y: 0 }, { x: 0.2, y: 1 }, { x: 0.6, y: 0.5 }, { x: 1, y: 0 }, ]} />
Controlled: drive the points from state.
Copy to Clipboardconst [points, setPoints] = useState(initialPoints); <Envelope onValueChange={setPoints} onValueCommitted={(next, details) => console.log("Committed:", next, details.reason)} value={points} />
The primitive alone, part by part:
A Root owns the points. A Graph is the surface they are edited on: it takes the double click that adds a point, and the Alt drag that bends a segment. A Line and a Fill draw the curve and the area under it, each as an <svg> stretched over the Graph. A Point is a handle for one point and a Segment a handle on the middle of the stretch between two.
You write a Point for each point and a Segment for each stretch, with their index: the Root's content can read its state, whose value is the points, so the handles follow the points as they are added and removed. That is a function in React, the default slot in Vue and a snippet in Svelte.
Give the Graph its size and a position. The pointer is read against the Graph's box, so a Graph with no height cannot take a click. Make it position: relative or absolute, and the Line and the Fill, which are absolute, take its box.
Copy to Clipboardimport { useState } from "react"; import { Envelope } from "@/components/audio-ui/envelope/react"; export function Amp() { const [points, setPoints] = useState([ { x: 0, y: 0 }, { x: 0.15, y: 1 }, { x: 0.6, y: 0.5 }, { x: 1, y: 0 }, ]); return ( <Envelope.Root className="relative h-40 w-80" onValueChange={setPoints} value={points}> {({ value }) => ( <> <Envelope.Graph className="absolute inset-0 rounded-md bg-muted"> <Envelope.Fill className="text-primary/20" /> <Envelope.Line className="text-primary" /> </Envelope.Graph> {value.map((_, index) => ( <Envelope.Point aria-label={`Point ${index + 1}`} className="size-3 rounded-full bg-primary" index={index} key={index} /> ))} {value.slice(0, -1).map((_, index) => ( <Envelope.Segment aria-label={`Curve ${index + 1}`} className="size-2 rounded-full bg-muted-foreground" index={index} key={index} /> ))} </> )} </Envelope.Root> ); }
Interaction
Pointer
Behavior · 10
x stays between its neighbors and y within 0…1. Dragging never deletes a point. While dragging, the point follows the pointer even if a controlled value updates late.maxPoints. Added on a bent segment it sits on the curve, and each half is bent to keep its shape.minPoints.minPoints.onValueCommitted fires when a drag that moved a point ends, and right away for an add or a remove.A refused add or remove sets data-blocked for a moment on the graph or the point, which the registry styles turn into a short shake.
Keyboard
The points share one tab stop. On the focused point:
Keys · 6
y by stepx by step (clamped between neighbors)minPointsEach segment has a handle on its curve. The handles share one tab stop:
Keys · 4
API reference
Registry component
Envelope accepts style props (size, className) plus the props of the Envelope primitive's Root. aria-label / aria-labelledby name the envelope group.
Value and state
Props · 9
x. curve, -1 to 1, bends the segment leaving the point.minPoints, the shape is fixed.aria-valuetext.Layout and display
Props · 6
xDomain={[0, 2000]}."ms" or "dB".The Envelope primitive that installs with it exposes Envelope.Root, Envelope.Graph (the SVG surface; double-click to add), Envelope.Line and Envelope.Fill (the curve and the area under it), Envelope.Point (the draggable handle) and Envelope.Segment (the handle on a segment's midpoint, which bends it). You write one of each per point and per segment, with its index, from the Root's children given as a function of its state, whose value is the points. Every part accepts a render prop and className / style as functions of state; points expose data-dragging, data-removable, data-blocked, data-disabled and data-index, and segment handles data-dragging, data-flat and data-disabled.
Options
Set on the Root, as props (an attribute in kebab case in the web adapter, the points as JSON).
| Option | Type | Default | |
|---|---|---|---|
value, defaultValue | { x, y, curve? }[] | an ADSR-like shape | The controlled points, or the starting ones. Each is normalized to 0 to 1 on both axes, sorted by x, and curve, from -1 to 1, bends the stretch that leaves it. |
minPoints, maxPoints | number | 2, none | How few and how many points there may be. Equal, the shape is fixed. |
snap | number or { x, y } | 0 | The grid the pointer snaps points to, in normalized units: 0.25 snaps to quarters, 0 is no grid, { x: 0.125, y: 0 } snaps across and leaves the level free. |
step | number | 0.01 | The keyboard step on both axes. |
format, locale | Intl.NumberFormat options, a locale | How a point's coordinates are written for its aria-valuetext. | |
disabled | boolean | false | Takes no gesture. |
onValueChange | (points, details) => void | Every change, which details.cancel() refuses. | |
onValueCommitted | (points, details) => void | The end of a gesture. |
The Line takes a strokeWidth, in pixels, which stays the same however the Graph is stretched. A Point takes getAriaValueText(point, index) to announce itself in your own words, and a Segment getAriaValueText(curve, index).
What a change reports
details carries what moved the points and how:
reason:"drag","keyboard","add","remove"or"curve"(a bend).index: the point or the segment that moved, or the index of the point that was added or removed.source:"mouse","touch","pen","keyboard"or"programmatic", for analytics. It is never a reason to behave differently.event: the DOM event behind it, when there is one.cancel(): refuses the change. A controlled envelope keeps the points you give it.
While a pointer drags, the points follow it even if a controlled value lags behind.
In the web adapter, onValueChange is an input event and onValueCommitted a change event, both with detail: { value, reason, index, source }, and event.preventDefault() refuses a change.
States to style from
data-disabledon every part.data-draggingon the Root, the Graph, the Line and the Fill while a point or a segment is dragged, and on a Point or a Segment only while it is the one dragged.data-blockedon the Graph, briefly, when an addition was refused, and on a Point when its removal was refused: a feedback to animate. It is raised a frame after the change, so that an animation can play again.data-removableon a Point while there are more thanminPoints, anddata-indexon a Point and a Segment.data-flatanddata-curveon a Segment.
Accessibility
A Point is a role="slider" on its level, from 0 to 1, and its aria-valuetext reads its coordinates, 0.5, 1, with the format and the locale of the Root. A Segment is a slider on its curve, from -1 to 1. Give each an aria-label: the Graph, the Line and the Fill are hidden from assistive technology.
Parts
Parts · 6
strokeWidth: number.strokeWidth: number.getAriaValueText: value, index: key.getAriaValueText: value, index: key.Events
Events · 2
@value-change. Web: the input event.@value-committed. Web: the change event.