spec, you dispatch a command and the provider applies it, recompiles, and records it on an undo stack.
editable mode, or a command streamed in from an agent — so all of them land in the same history and all of them are undoable.
Dispatching
useGraphCommands returns dispatch(command, options?) and commit(). It must be called from inside a <GraphProvider>.
A command is a plain object you construct with its params:
layerId. Omit it and the command finds the first layer of a compatible geom — enough for a single-series chart, while a combo chart should name the layer explicitly:
id off and one is assigned at compile time, so a layer always has a stable identity — you just don’t get to choose the name.
Five commands require layerId rather than accepting it: SetLineWidthCommand, SetPointSizeCommand, SetBarBorderRadiusCommand and ToggleLineFillCommand, which write per-layer stylesheet entries and so need an address, plus RemoveLayerCommand, which needs to know what it is removing.
Nothing is mutated. A command reads the current spec and returns a new one, so the provider can swap the compiled result in without touching the object you passed as spec.
Persisting edits
The provider’sonSpecChange fires with the spec a command produced. Store that if edits need to survive a reload:
spec is a no-op — the provider recognises the echo and does not recompile.
Live gestures
A slider or colour picker that edits while it is held would otherwise leave one undo entry — and oneonSpecChange — per frame. Pass { transient: true } on those frames and call commit() when the gesture ends:
onSpecChange fires once, when commit() closes the run.
A run covers one thing being edited: the dispatches fold together only while they are the same command type against the same edit target. A different target — or a different command type on the same target — starts its own entry, and so does a committed dispatch, an undo, a redo, or a new spec, data or theme from the host, each of which closes the open run first. Forgetting to commit() delays that gesture’s onSpecChange until one of them arrives; it does not merge two gestures into one step.
Dragging something across the canvas is a different shape: move it locally in pixels and dispatch one command on drop. Transient dispatch is for gestures whose feedback genuinely needs the spec to recompile.
Undo & redo
useGraphHistory gives you the controls plus the state a history UI reads. It subscribes to the stack, so a component using it re-renders whenever a step becomes available.
boolean
Whether a step is available in each direction.
string | null
Human-readable label for the next step —
"Set line width to 3" — for a
button tooltip or a menu item. null when there is nothing to step.CommandMetadata[]
The full history, for a history panel. Each entry carries
id, timestamp,
description and author. undoStack runs oldest first; redoStack runs in
the order the steps were undone. In both it is the last entry that is next:
the end of undoStack is what an undo reverses, the end of redoStack is
what a redo re-applies.Keyboard shortcuts
Shortcuts are opt-in.useGraphHistoryShortcuts binds them, driving the graph through an handleRef so it can be called from wherever your key handling lives, including above the provider:
{ target } — an element, or a ref holding one — to scope it to a subtree instead of window, or { enabled: false } to unbind.
A chord is only claimed when the chart has a step to take: with an empty history the keystroke is left uncancelled, so an undo your app owns elsewhere on the page still gets it.
The same handleRef exposes dispatch, commit, undo and redo directly, for a toolbar or menu bar mounted outside the provider where the hooks can’t reach. undo and redo return whether the chart took the step — that’s the signal to fall through to your own handling. A step the chart declines, either because there is nothing to step or because the older spec no longer compiles, returns false.
When a command doesn’t apply
Two outcomes leave the chart exactly as it was:- No-op — the command found nothing to change: the value already matches, or the layer or scale it targets isn’t in the spec. Nothing recompiles and nothing joins the history, so there’s no empty step to undo past.
- Rejected — the edit produced a spec that fails to compile. The last good chart stays on screen and the diagnostics go to the provider’s
onError. An edit the chart never took is not one you can undo, so the history is left untouched.
undo() returns false so the keystroke falls through to your app. The step stays on the stack and applies once the data supports it again, which is also to say the steps behind it are reachable only through it.
History lifecycle
The history belongs to the chart the provider is currently showing:- A new
specfrom the host is a new baseline and clears the history, once that input compiles. The stored steps restore values from a spec that no longer exists, and may target layers or scales the new one doesn’t have. This is by reference, so aspecrebuilt on every render leaves nothing undoable — keep it stable. - An
specthat fails to compile keeps it. The chart behind the error panel is still the last good one, so an undo is the way back to something renderable. - New
dataor a theme change keeps it. The recorded steps still address the spec they were built from. - History depth is capped at 100 steps; the oldest is dropped past that.
Available commands
All commands
All commands
Each takes its params as the first constructor argument and optional metadata as the second. Scale commands take Declarations at an address the list already holds replace that entry’s, keeping its position in the cascade.
scaledAesthetic to pick the scale by the aesthetic it drives ('x', 'y', 'color', …). Axis commands take axis: 'x' | 'y' for all of them except SetAxisLabelCommand, which also accepts 'ySecondary', and the two grid line-style commands, which accept 'both' so a change across the whole grid folds into one undo entry.AddLayerCommand, AddHighlightCommand and AddAnnotationCommand each draw a line between what you construct with and what serializes: the constructor’s id is optional and minted when omitted, while the serialized params carry it settled, so a deserialized command replays to the same spec instead of adding a second thing.Chart typeWhat a chart is — a column chart, a stacked bar, a donut — spans the coordinate system, every series layer and the scales those geoms need, so this writes them together as one undo entry.
readChartType(spec) reads it back for a picker, returning null for a combo chart no single option describes. Reference lines and fitted trends keep their own shape across the switch.Titles & contentAxes & grid
Legend & headline
Appearance & formatting
Paint — the graph’s background, border, corner radius and text scale included — is a stylesheet edit rather than a config one: reach for
SetStyleRuleCommand with style.graph({ … }), and see Styling for the vocabulary.CoordinatesLayers
AddLayerCommand and RemoveLayerCommand are the two directions of every “show a trend line” or “add a series” toggle, so AddLayerCommand carries a whole layer rather than a per-feature flag. The toggles that remove a layer of their own — ToggleLinePointsCommand, ToggleGoalLineCommand, SetStatLineCommand — carry a removedLayer for the same reason: it is what lets undo bring back the layer’s own settings and identity rather than a fresh one.SetLineWidthCommand, SetPointSizeCommand, SetBarWidthCommand, SetBarBorderRadiusCommand and ToggleLineFillCommand are typed shortcuts over the stylesheet — each writes a { layer }-scoped entry through SetStyleRuleCommand, merging into whatever else that layer’s entry paints.StylesSetStyleRuleCommand is the one command for paint (widths, dashes, opacities, colors beyond the palette). An entry is addressed structurally: the rule’s own select and when say which entry it is, so the command writes whatever paints those elements under those conditions, whoever authored it. An entry’s id travels inside the rule, through the builder’s options bag — style.geom.line({ strokeWidth: 3 }, { id: 'emphasis' }).declarations: null removes every entry at the address; an address the list doesn’t hold is a no-op. See Styling for the targets and the vocabulary each one takes.ScalesHighlights
Annotations
Annotations are addressed by an
id unique across every kind, so removing, updating and moving take the id alone. UpdateAnnotationCommand patches whichever fields you name — moving one and restyling it are the same command, so a drag and a colour picker share a path. MoveAnnotationCommand is the specialised drag: a translation in panel fractions, clamped inside the panel, relative rather than absolute, and reverted by the inverse patch since clamping isn’t invertible.Commands over the wire
Commands are registered by type, so they round-trip through JSON.commandRegistry turns a command into a SerializedCommand — { type, params, metadata } — and a SerializedCommand back into a live instance. That is what lets an edit arrive from an agent, a collaborator, or a replayed session log and land on the same history as a click:
deserialize throws on a type it doesn’t know, so treat an unrecognised payload as a version mismatch rather than a no-op. This round trip is also why the Params types are the settled ones: a replayed command reproduces the same spec rather than minting a second id.
Related
- Provider & renderer —
onSpecChange,handleRef,mode="editable" - Serializable spec — the spec commands edit, and how to persist it

