Reusable components and template reuse

Reuse parts of DocStudio templates as versioned components with props and slots, pin them by version, and store them in a component library.

A reusable component is a template that other templates import. It has props, slots, a name and a version.

Use a component

{
  "schemaVersion": "1.0.0",
  "imports": [{ "alias": "head", "ref": "letterhead@1.0.0" }],
  "body": [
    {
      "id": "top",
      "type": "component",
      "ref": "letterhead@1.0.0",
      "props": { "organisation": "Northwind Clinic" },
      "slots": { "extra": [{ "id": "note", "type": "text", "content": ["Open weekdays"] }] }
    }
  ]
}

The component's own template reads props with $props and marks slot positions with a slot node:

{
  "schemaVersion": "1.0.0",
  "body": [
    { "id": "org", "type": "text", "content": [{ "path": "$props.organisation" }] },
    { "id": "extra", "type": "slot", "slot": "extra" }
  ]
}

Provide a component store

compile resolves components through a store you supply:

import { compile, type ComponentStore } from 'docstudio/core';

const store: ComponentStore = {
  get(name, version) {
    return name === 'letterhead' && version === '1.0.0'
      ? { template: letterheadTemplate }
      : undefined;
  },
};

const result = compile({ template, data, components: store });

With renderDocument, pass it as compile: { components: store }.

Versions

  • A published version is immutable and identified by name, version and content hash.
  • Documents pin name@major.minor.patch, so publishing a new version never changes existing documents.
  • Moving a document to a newer version is an explicit, undoable step with an impact preview in the designer. Rollback re-pins references. There is no unpublish.

Names may contain letters, digits, underscore and hyphen. A dot is refused.

In the designer

Pass library (for example createMemoryLibrary()) and catalog to the designer to publish, insert and upgrade components. The library port is synchronous, so a host preloads it, and the memory library is a reference implementation rather than a storage adapter. In component authoring mode (mode: "component"), only Submit is offered.

Composite nesting depth is limited, and custom host components are trusted code. See the designer guide on reusable components.

Last updated 2026-12-03