Composition / foldkit-composition
composition
When marketing or editorial teams need to reorder sections, swap a hero callout, or assemble a landing page without waiting on a code deploy, teams usually end up choosing between hardcoded JSX components or storing raw HTML in the database. Raw HTML breaks layout guarantees and opens XSS holes, while hardcoded components stall every content tweak on an engineering release.
foldkit-composition represents each page as a plain JSON Document made
from typed Blocks you define in code. Your code declares each Block's prop
schema and the child Regions it exposes; the database stores only Block
names, ids, and props, validated against your Catalog before publishing.
See pages of Blocks edited and rendered live in the live CMS demo.
Install
pnpm add foldkit-compositionA first page of Blocks
This defines a Heading and a Section Block, combines them into a Catalog,
decodes a JSON page Document, and validates it:
import { Schema } from 'effect'
import { Block, Catalog, Composition, Content, Region } from 'foldkit-composition'
const Heading = Block.define('Heading', {
Props: Schema.Struct({ text: Schema.String, level: Schema.Literals([1, 2, 3]) }),
provides: [Content.Flow],
})
const Section = Block.define('Section', {
Props: Schema.Struct({ tone: Schema.Literals(['plain', 'accent']) }),
regions: { body: Region.many({ accepts: [Content.Flow] }) },
provides: [Content.Section],
})
const Site = Catalog.make({ blocks: [Heading, Section], roots: [Content.Section] })
const page = Schema.decodeUnknownSync(Composition.Document)({
format: 1,
roots: ['intro'],
nodes: {
intro: { block: 'Section', props: { tone: 'plain' }, regions: { body: ['title'] } },
title: { block: 'Heading', props: { text: 'Hello', level: 1 }, regions: {} },
},
})
const findings = Composition.validate(Site, page)
const outline = Composition.describe(Site, page)findings is [] (every node fits the Catalog), and outline returns the
readable tree:
Section intro {"tone":"plain"}
body:
Heading title {"level":1,"text":"Hello"}Block.defineandCatalog.makedescribe your vocabulary. A Catalog with duplicate Block names, an empty Region acceptance list, or a Block that provides no Content throws when defined.Composition.Documentis an Effect Schema for plain JSON, ready for a database column or form field.Composition.validatereturns a finding for each issue—code, node id, path, and message—while leavingpageuntouched.