Composition / foldkit-builder
builder
Once pages are stored as a tree of typed blocks, authors need a way to insert, reorder, duplicate, copy-paste, undo, and configure those blocks without corrupting the document or losing unsaved work. Wiring selection, keyboard shortcuts, drag-and-drop targets, inline text edits, and per-block settings forms by hand mixes transient UI state with your stored page.
foldkit-builder gives you the headless state machine for editing a
foldkit-composition Document. It wraps the page
in an undo history, tracks selection, clipboard, and drag state, generates a
foldkit-form settings editor per Block, and plugs into
any form as a single control so validation and autosave work out of the box. For
the full accessible HTML view, pair it with
foldkit-mixins-builder.
Try it running in the live CMS demo.
Install
pnpm add effect foldkit foldkit-composition foldkit-form foldkit-builderA first page builder
This defines a Heading and Section Block, builds PageBuilder with starter
props, and applies an insert edit through its Bundle:
import { Schema } from 'effect'
import { Builder, Message as BuilderMessage } from 'foldkit-builder'
import { Block, Catalog, Composition, Content, NodeId, Region } from 'foldkit-composition'
import { Renderer } from 'foldkit-composition/foldkit'
const Heading = Block.define('Heading', { Props: Schema.Struct({ text: Schema.String }), provides: [Content.Flow] })
const Section = Block.define('Section', {
Props: Schema.Struct({}),
regions: { body: Region.many({ accepts: [Content.Flow] }) },
provides: [Content.Section],
})
const Site = Catalog.make({ blocks: [Heading, Section], roots: [Content.Section] })
const SiteRenderer = Renderer.make(Site, {
Heading: ({ field, h }) => h.h2([], [field('text')]),
Section: ({ regions, h }) => h.section([], [...regions.body]),
})
const PageBuilder = Builder.make('PageBuilder', {
catalog: Site,
renderer: SiteRenderer,
starters: { Section: {}, Heading: { text: 'New heading' } },
})
const op = Composition.Op.insert({ id: NodeId.make('sec1'), block: 'Section', props: {}, at: Composition.root(0) })
const start = PageBuilder.bundle.init(undefined).model
const next = PageBuilder.bundle.update(start, BuilderMessage.Applied({ op }), undefined).modelPageBuilder.document(next) now holds the new Section node under roots: ['sec1'],
next.selected is Option.some('sec1'), and pressing undo (Undid()) steps
back to the empty page.
To bind the builder inside a foldkit-form field (so forms and foldkit-cms
can validate and autosave the Document), pass PageBuilder.input as the field's
control:
import { Entity } from 'foldkit-entity'
import { Form } from 'foldkit-form'
const Page = Entity.define('Page', Schema.Struct({ id: Schema.String, document: Composition.Document }))
const PageInput = Entity.input(Page, Schema.Struct({ document: Composition.Document.check(Composition.valid(Site)) }))
const PageForm = Form.make('PageForm', PageInput, { inputs: { document: PageBuilder.input } })PageForm.control('document').field(model).valueholds the Builder's Model inside the form.- Its built-in plain view draws the Block palette, layers tree, selected node
text props, undo/redo buttons, and edit-mode canvas; swap in
PageBuilder.inputWith(BuilderView.submodel(view))to render withfoldkit-mixins-builder.