Composition is the object returned by openComposition. Every SDK edit goes through this interface. Typed methods are convenience wrappers over dispatch(); all validation runs in dispatch() regardless of which entry point you use.
Typed edit methods
Typed methods are the recommended way to apply common edits. Each one callsdispatch() internally, so all patch events, history, and persistence behavior is identical.
setStyle
CSSStyleDeclaration). Pass null as a value to remove that property.
setText
setAttribute
null to remove the attribute entirely.
setTiming
data-start, data-duration, and/or data-track attributes of one element. All fields are optional; omitted fields are left unchanged.
removeElement
addElement of the removed subtree, so undo() restores it exactly.
addElement
parent at zero-based sibling index. Pass null for parent to insert at the document body root. The fragment must be single-root and must not contain <script> tags. Returns the minted hf-id of the inserted root element.
setVariableValue
FontValue object { name, source }; image variables require an ImageValue object { url, alt?, fit? }. Scalar variables accept string | number | boolean.
setVariableValue only updates a variable’s current value — it refuses to create an undeclared variable. Use declareVariable to create one.getVariableValue
default value, or undefined if the id is undeclared or has no value set.
listVariables
id, type, label, default, and any type-specific fields (min/max/step for numbers, options for enums, source for fonts, etc). Returns [] when the composition declares no variables.
declareVariable / updateVariableDeclaration / removeVariableDeclaration
data-composition-variables schema itself. declareVariable adds a new
typed declaration (creating the attribute when the composition has none);
updateVariableDeclaration replaces an existing declaration wholesale — the id is
immutable, rename by remove + declare; removeVariableDeclaration deletes a
declaration (the last removal drops the whole attribute). All three validate via
can() (E_DUPLICATE_VARIABLE, E_VARIABLE_NOT_FOUND, E_INVALID_ARGS,
E_INVALID_VARIABLE_ID — the id must match /^[A-Za-z_][A-Za-z0-9_-]*$/ since it
becomes a CSS custom-property name, data-var-* value, and CLI --variables key;
and E_FRAGMENT_COMPOSITION). Declarations live on the composition’s declaration
root: the <html> element for a full document, or the composition root element
([data-composition-id]) for a template / fragment sub-composition. Despite its
name, E_FRAGMENT_COMPOSITION now fires only when the source has no root
element at all to carry the schema — a fragment that has a composition root
accepts declarations. (The code is kept for compatibility; the fix is “add a
composition root element”, not necessarily “add an <html>”.)
removeVariable(id) is a shorthand alias of removeVariableDeclaration(id).getVariableDeclarations / getVariableValues / validateVariableValues / getVariableUsage
getVariableDeclarations returns the typed
schema (same strict filter the render pipeline uses). getVariableValues resolves
values exactly like the runtime’s getVariables() — declared defaults merged under
the given overrides — so callers can predict what a composition script will read for
a given --variables payload. validateVariableValues runs the same checks as
--strict-variables (undeclared / type-mismatch / enum-out-of-range).
getVariableUsage statically scans every inline script for getVariables() reads
and cross-references the schema: { usedIds, unusedDeclarations, undeclaredReads, scanIncomplete } — scanIncomplete flips when scripts access variables dynamically,
making usedIds a lower bound.
setPreviewVariables
setVariableValue to change a default). Pass null to restore declared defaults.
Delegates to the preview adapter’s optional setPreviewVariables; returns false
when no adapter support is available.
getElementTimings
enterAt/exitAt using data-duration when present, falling back to data-end − data-start. GSAP labels are parsed fresh from the script each call. Elements with no timing attributes are excluded.
setElementTiming
batch() call so the history sees one undo step. Entries for unknown IDs are silently skipped.
setHold
{ start, end, fill } range that either freezes or loops the element during a pause in the timeline.
addGsapTween
animationId.
setGsapTween
removeGsapTween
addWithKeyframes
targetSelector at the given timeline position and duration. Returns the newly minted animationId. Position is a number in seconds (not a label-relative string).
replaceWithKeyframes
animationId. Because position-derived IDs renumber after the removal, the returned ID may differ from the input animationId — treat the return value as the new canonical ID.
undo / redo
history: false in options) or in embedded mode.
canUndo / canRedo
true when a step in that direction is available. Use to drive the enabled state of undo/redo UI controls.
Query
getElements
ElementSnapshot carries the element’s id, scopedId, tag, inlineStyles, classNames, attributes, text, timing fields, and animationIds. The array is a fresh copy; mutations to the returned objects have no effect.
For elements inside inlined sub-compositions, use
scopedId (e.g. "hf-host/hf-leaf") as the dispatch target, not id. Top-level elements have scopedId === id.getRootElements
getElements, which flattens every nested element into the same array). Use this when you need one entry per top-level clip rather than every element in the tree.
getElement
null if no element matches.
find
scopedId strings for matching elements. All fields in FindQuery are optional and combined with AND logic.
| Field | Type | Description |
|---|---|---|
tag | string | Exact HTML tag name, lowercase ("div", "img"). |
text | string | Substring match against the element’s text field. |
name | string | Exact match against data-name attribute. |
track | number | Exact track index (data-track). |
composition | string | Filter to elements inside a specific sub-composition host by its hf-id. |
getAllAnimationIds
ElementSnapshot’s own animationIds field, which only lists tweens whose selector resolves to that specific element.
Selection
selection
SelectionProxy that resolves getSelection() at call time and dispatches operations to all selected elements. The proxy captures the ID list when you call selection() — subsequent selection changes do not affect an already-captured proxy.
element
ElementHandle for a single element. The handle stores only the ID string, so there is no stale-reference hazard if the DOM changes between calls.
getSelection
hf-id strings. Returns [] when nothing is selected.
setSelection
selectionchange. Pass [] to clear the selection. Duplicate IDs are deduplicated automatically.
Advanced / agent
dispatch
dispatch when you need to apply op types that do not have a typed wrapper, when you are building automation that constructs ops programmatically, or when you need to set a custom origin.
dispatch by passing an array to target:
batch
patch event. If the callback throws, all DOM mutations that ran inside the batch are rolled back atomically and the model is restored to its state before batch() was called.
can
{ ok: true } when dispatch(op) would succeed, or { ok: false, code, message, hint? } when it would fail or be a no-op.
Use this as a feature-detection gate before rendering controls that depend on GSAP timeline availability, or before showing UI that applies optional edits.
E_TARGET_NOT_FOUND, E_NO_ROOT, E_NO_GSAP_TIMELINE, E_NO_GSAP_SCRIPT.
For the full op catalog see Edit operations.
Events
Allon() overloads return an unsubscribe function. Call it to remove the listener.
change
Fires after every committed mutation (whether typed method, dispatch, or batch). Use this to refresh a canvas or other derived view.
selectionchange
Fires when the selection changes, with the new ID array as the argument.
patch
Fires after every committed change with a PatchEvent containing RFC 6902 forward and inverse patches, the origin, and semantic opTypes. Use this to mirror edits into a host history stack, collaboration layer, or audit log.
persist:error
Fires when the persist adapter fails to write. The session continues; edits accumulate in memory and the queue retries on the next mutation.
Serialization
serialize
openComposition. The base HTML is never modified; this always returns a fresh serialization of the live document.
Embedded extras
These methods are only meaningful in embedded / override mode. See the embedded override mode guide.getOverrides
applyPatches
ORIGIN_APPLY_PATCHES, which prevents patch listeners from forwarding these back in an undo loop. Use this when the host owns the undo stack and needs to replay inverse patches from its own history.
Lifecycle
flush
dispose
dispose(), the Composition object is inert; continued calls produce no-ops or errors.
Always call
dispose() when you are done with a session. Skipping it leaks listeners attached to the preview adapter.Related
openComposition
Session factory — all options and modes.
Edit operations
Full op catalog for dispatch() and can().
Types reference
All exported TypeScript types — ElementSnapshot, PatchEvent, GsapTweenSpec, and more.
Querying and editing
Practical guide to find, getElements, and typed edits.
Undo, redo, and patches
History, patch events, ORIGIN_APPLY_PATCHES guard.
Embedded override mode
Template-driven products with host-owned history.