React / foldkit-react-codegen
react-codegen
Handing off a Foldkit design system to a React team—or sharing presentational views with an existing React app—usually forces you to rewrite every view into JSX by hand. Worse, embedding a full Foldkit runtime is unnecessary when React already owns component state.
foldkit-react-codegen compiles pure Foldkit view functions (view(model, h: HtmlBuilder<Message>): Html) directly into React TSX source (view(model, dispatch: (message: Message) => void): ReactNode). Operating on the TypeScript AST, it preserves your variable names, helpers, control flow, and imports while refusing unsupported runtime constructs with file-and-line diagnostics.
Use foldkit-react-codegen for pure presentational markup, attributes, and events; use foldkit-react when you need Model, update, Commands, Subscriptions, or Mounts running inside React.
Install
pnpm add -D foldkit-react-codegen typescriptA first compiled view
Write a Foldkit view module using HtmlBuilder:
import type { Html, HtmlBuilder } from 'foldkit/html'
import { defineMessageUnion } from 'foldkit/message'
export type Model = { readonly saving: boolean }
export const Message = defineMessageUnion({ Saved: {} })
export type Message = typeof Message.Type
export const view = (model: Model, h: HtmlBuilder<Message>): Html =>
h.button([h.Class('save'), h.Disabled(model.saving), h.OnClick(Message.Saved())], ['Save'])Run the compiler over your source directory:
pnpm foldkit-react-codegen src --out-dir generatedThe compiler writes the equivalent React JSX module with h rewritten to dispatch:
import type { ReactNode } from 'react'
import { defineMessageUnion } from 'foldkit/message'
type Model = { readonly saving: boolean }
const Message = defineMessageUnion({ Saved: {} })
type Message = typeof Message.Type
export const view = (model: Model, dispatch: (message: Message) => void): ReactNode => (
<button className={'save'} disabled={model.saving} onClick={() => dispatch(Message.Saved())}>
Save
</button>
)Every function taking an HtmlBuilder parameter compiles into a React view taking dispatch, helper calls like row(item, h) rewrite to row(item, dispatch), h.<tag> calls become JSX elements, and non-view helper modules copy into generated/ so relative imports resolve. Import the generated view into any React component with JSX enabled (run your formatter over generated/ if committing output):
import { useState } from 'react'
export const SaveButtonView = (props: {
readonly initialModel: Model
readonly next: (model: Model, message: Message) => Model
}) => {
const [model, setModel] = useState(props.initialModel)
return view(model, message => setModel(current => props.next(current, message)))
}Verify generated output in React
After codegen, typecheck the consuming React project and render the generated component. With saving: true the button renders disabled; when enabled, clicking calls dispatch(Message.Saved()). Keep generated output in its own directory, and check CLI diagnostics before testing output in watch mode because failed builds keep the previous valid file on disk.