Reusable components

Pinned components, slots, locks and expansion. Template reference in the DocStudio developer reference.

Public from docstudio/core: expandComponents, extractDependencies, parseComponentRef, and the types ComponentStore, ComponentRecord, ExpandedNode, ExpandedTemplate, ExpansionResult, ComponentLock, ComponentRef. Decision record: ADR 0015.

Model

A reusable component is an ordinary template (same schema) stored under name@major.minor.patch. A host template lists it in imports and places it with a component node:

{
  "id": "c1",
  "type": "component",
  "ref": "card@1.0.0",
  "props": { "title": "Hello" },
  "slots": { "main": [{ "id": "t", "type": "text", "content": ["Body"] }] }
}

Inside the component template, $props.title reads a prop and a slot node ({ "id": "main", "type": "slot", "slot": "main" }) marks where the caller's main content goes. $props and slot are valid only in component templates. Component page furniture (header and footer) is ignored with a warning.

Store

ComponentStore.get(name, version) is a synchronous function returning { template } (stored JSON, any version the migration chain can bring to the supported schema) or undefined. Hosts back it with a database, a bundle or a map. A store that throws is reported as COMPONENT_NOT_FOUND and the thrown message is not echoed.

Pinning

Every reference must be exact: name@1.2.3. An unpinned ref is UNPINNED_COMPONENT. There are no ranges and no "latest", so a document renders from exactly the component versions it names.

Expansion

expandComponents(template, { store, limits?, registry?, migrations?, formatters? }) returns { expanded, locks, diagnostics }.

For each component call the output has an ExpandedComponentScope node (type: 'componentScope') with the call's id, the pinned ref, the raw props and children holding the expanded component body. Each slot node becomes an ExpandedSlotContent (type: 'slotContent') holding the caller's nodes for that slot. All other nodes are copied with expanded children. Props are not evaluated here.

Compile (M10) uses the markers for scope: entering a componentScope evaluates its props in the current scope and makes them $props for the body; entering a slotContent restores the scope of the call site, so content written by the caller sees the caller's $props, not the component's.

  • Ids. Nodes from a component body are prefixed with the instance path (c1:box), so ids stay unique however often a component is used. Slot content keeps the caller's ids.
  • Origin. Every expanded node has origin: a JSON Pointer in the host template, or ref#pointer inside a component.
  • Loading. Each component is loaded, migrated (migrations.md) and validated (semantic-validation.md with props and slots allowed) once per expansion, however often it is used. An invalid component is COMPONENT_INVALID at the call site and the call is omitted.
  • Slots. A slot name the component does not declare is an UNKNOWN_SLOT warning; a declared slot with no content is empty.

Limits

Condition Result
Nesting deeper than maxCompositeDepth (default 8, ceiling 64) LIMIT_EXCEEDED at the call, the call is omitted
A component that contains itself, directly or through others COMPOSITE_CYCLE with the chain a@1.0.0 > b@1.0.0 > a@1.0.0
More than maxTemplateNodes expanded nodes One LIMIT_EXCEEDED, expansion stops

Dependencies and locks

extractDependencies(template) lists every component the template references: imports (with alias) and component nodes found in the body, nested containers, slot content and page regions, each with imported and used flags, sorted by ref. It does not read the store.

expansion.locks has one record per loaded component, { ref, name, version, hash }, sorted by ref. The hash is the canonical SHA-256 of the migrated component template, so it does not depend on key order. Together with the host template hash it identifies exactly what a document was built from.

Performance

Median on macOS (Node 24): 2,000 instances of one component (10,000 expanded nodes) 12 ms; 200 components with 10 uses each 40 ms. See validation/artifacts/benchmarks/core-m9-composite.json.