⚠️ Work in progress — createCMS is pre-1.0 and not production-ready (not tested in production). Expect breaking changes.
createCMS
Reference

Editor cms adapter

CMS document hook and field sources for @createcms/react/editor/cms.

Optional adapter between the editor primitive and a createcms collection client. Consumers with their own data layer can skip this entry.

import {
  useCmsDocument,
  useCmsFieldSources,
  useVariableSuggest,
  assetUrl,
} from '@createcms/react/editor/cms';

Usage

const doc = useCmsDocument({
  client: cmsClient.pages,
  rootId,
  branchId,
  message: () => commitMessageRef.current,
  templates: cmsClient.templates,
  collection: 'pages',
});

<pageEditor.Root
  key={doc.key}
  defaultValue={doc.tree}
  onSave={doc.save}
  onChange={doc.onChange}
>
  <Canvas.Root resolve={doc.resolve} />
</pageEditor.Root>;

Wire onChange={doc.onChange} so resolveTree sees unsaved edits. Pass key={doc.key} on Editor.Root so a reload resets the uncontrolled store.

Client shape

The hook duck-types four collection methods with the live proxy envelope: getBlockTree({ query }), getBranch({ query }), updateBlocks({ body }), and resolveTree({ body }). getBranch is required because getBlockTree does not return headCommitId.

The createcms client proxy mints a new object on every property access. Pass client once; the hook captures it in a ref on the first render.

Status

StatusMeaning
loadingInitial load or reload() in flight
idleTree ready
savingupdateBlocks in flight
conflictHEAD_MISMATCH (branch advanced)
errorOther save or load failure

On conflict, call reload() to fetch the latest head or save({ force: true }) to overwrite without a head check.

Errors

error.fields is { blockId, key, message }[] for server-side TYPE_MISMATCH issues. Render it in editor chrome. Editor.FieldError still shows client-side validateField findings only.

Known error codes on CmsDocumentError.code:

CodeMeaning
HEAD_MISMATCHBranch head advanced since the last load
TYPE_MISMATCHServer rejected property values (error.fields)
BLOCK_NOT_ALLOWED_IN_PARENTBlock type not allowed under its parent
PROTECTED_BRANCHBranch is protected from direct edits
COMMIT_MESSAGE_REQUIREDSave requires a commit message
UNKNOWNUnrecognised or malformed wire error

Templates

When templates and collection are set, onAdd(blockType) calls getTemplateDefaults and returns the defaults object for merging into store.add / AddBlock.

Field sources

Registry controls for image, reference, link, and {{variable}} suggest call useCmsFieldSources(cmsClient) with the top-level createcms client (not a collection namespace such as cmsClient.pages).

const sources = useCmsFieldSources(cmsClient);
const suggest = useVariableSuggest(sources);

Pass client once; the hook captures it in a ref on the first render, same as useCmsDocument.

Return value

MemberRole
assets.list(query?)Media library listing (listAssets({ query }))
assets.get(ids)Batch asset lookup by id (getAssets({ query: { ids } }))
assets.useUpload()React hook wrapping media.useUploadAssets() when present
roots.list(collection, query?)Collection root listing
roots.get(collection, rootId)Single root by id
roots.bySlug(collection, slug, parentRootId?)Draft slug lookup
variables.list(query?)Site variables
templates.defaults(collection, blockType)Block template defaults

Proxy envelope

Live client calls use { query } / { body }, not flattened JSDoc examples:

sources.assets.list({ limit: 20, cursor });
cmsClient.media.listAssets({ query: { limit: 20, cursor } });

Cache

Per-hook, in-memory only: successful responses and in-flight promises are keyed by operation and arguments (stable JSON). Failed requests are not cached. assets.list and roots.list also populate id caches used by get.

assetUrl

Build the status-gated media URL for content and canvas (not the direct AssetListItem.url):

assetUrl(assetId, { format: 'webp', w: 800 });
// /api/cms/media/asset/<id>?format=webp&w=800

Labels

referenceLabel(collection, rootId, sources) and linkLabel(linkValue, sources) resolve display names from root properties (title, label, name, then slug, path, id). Internal link labels ignore fragment and query. Failed root lookups fall back to the raw id.

useVariableSuggest

Loads variables (paged, up to 1000 offset) and exposes a suggest object for Canvas.InlineText or form controls. Pattern: /\{\{(\w*)$/. getItems filters loaded keys by prefix; accept inserts {{key}}.

assets.useUpload() is a nested React hook: call it at component top level, not inside callbacks.

Types

TypeDescription
UseCmsDocumentOptionsOptions for useCmsDocument.
UseCmsDocumentResultReturn value: tree, key, headCommitId, resolve, status, error, save, reload, onChange, onAdd.
CmsDocumentStatus'loading' | 'idle' | 'saving' | 'conflict' | 'error'.
CmsDocumentError{ code, message, fields? } from a failed save or load.
CmsFieldErrorOne field issue: { blockId, key, message }.
CmsDocumentClientDuck-typed collection client the hook expects.
CmsDocumentResolveOptional reference / link / string resolvers for the canvas.
CmsTemplatesClientClient with getTemplateDefaults.
CmsFieldSourcesReturn type of useCmsFieldSources.
UseCmsFieldSourcesClientTop-level client shape for field sources.
CmsAssetListItemOne asset row from assets.list.
CmsAssetListQueryQuery for assets.list.
CmsAssetListResultPaginated asset list result.
CmsRootListItemOne root row from roots.list.
CmsRootListQueryQuery for roots.list.
CmsRootListResultPaginated root list result.
CmsVariableListItemOne variable row.
CmsVariableListQueryQuery for variables.list.
CmsVariableListResultPaginated variable list result.
CmsSuggestItemOne suggest row for variables.
CmsSuggestRenderContextContext passed to variable suggest UI.
CmsVariableSuggestSuggest config from useVariableSuggest.
CmsMediaUploadStateUpload batch state from assets.useUpload().
CmsMediaUploadFileStatePer-file upload state.
AssetUrlOptionsOptions for assetUrl (format, w, h, …).
CMS_RESOLVE_DEBOUNCE_MSDebounce before resolveTree after local edits (default 300 ms).
linkLabelResolve a link value to a display string.
referenceLabelResolve a reference root id to a display string.

On this page