Skip to content

Ben-Platform/foldkit

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,161 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Foldkit

npm version

The frontend framework for correctness.

Documentation · Manifesto · Examples · Getting Started · Discord


Built on Effect. Architected like Elm. Written in TypeScript. One Model, one update function, one way to do things. No hooks, no local state, no hidden mutations.

Note

Foldkit is pre-1.0. The core API is stable, but breaking changes may occur in minor releases. See the changelog for details.

Who It's For

Foldkit is for developers who want to build their product with confidence instead of fighting their architecture. If you want a single pattern that scales from a counter to a multiplayer game without complexity creep, this is it.

It's not incremental. There's no React interop, no escape hatch from Effect, no way to "just use hooks for this one part." You're all in or you're not.

Built on Effect

Every Foldkit application is an Effect program. Your Model is a Schema. Side effects are values you return, not callbacks you fire — the runtime handles when and how. If you already know Effect, Foldkit feels natural. If you're new to Effect, Foldkit is a great way to immerse yourself in it.

Coming from React?

Coming from React is a guided walk through the differences. Foldkit vs React: Side by Side implements the same pixel-art editor in both frameworks so you can read them line by line.

Get Started

create-foldkit-app is the recommended way to start a new project. It scaffolds a complete setup with Tailwind, TypeScript, ESLint, Prettier, and the Vite plugin for state-preserving HMR — and lets you choose from a set of examples as your starting point.

npx create-foldkit-app@latest

Counter

This is a complete Foldkit program. State lives in a single Model. Events become Messages. A pure function handles every transition.

src/main.ts defines the program. src/entry.ts boots the runtime. The split keeps main.ts importable from tests without booting a runtime as a side effect.

// src/main.ts
import { Match as M, Schema as S } from 'effect'
import { Command, Runtime } from 'foldkit'
import { Document, html } from 'foldkit/html'
import { m } from 'foldkit/message'

// MODEL

export const Model = S.Struct({ count: S.Number })
export type Model = typeof Model.Type

// MESSAGE

const ClickedDecrement = m('ClickedDecrement')
const ClickedIncrement = m('ClickedIncrement')
const ClickedReset = m('ClickedReset')

export const Message = S.Union([
  ClickedDecrement,
  ClickedIncrement,
  ClickedReset,
])
export type Message = typeof Message.Type

// UPDATE

export const update = (
  model: Model,
  message: Message,
): readonly [Model, ReadonlyArray<Command.Command<Message>>] =>
  M.value(message).pipe(
    M.withReturnType<
      readonly [Model, ReadonlyArray<Command.Command<Message>>]
    >(),
    M.tagsExhaustive({
      ClickedDecrement: () => [{ count: model.count - 1 }, []],
      ClickedIncrement: () => [{ count: model.count + 1 }, []],
      ClickedReset: () => [{ count: 0 }, []],
    }),
  )

// INIT

export const init: Runtime.ProgramInit<Model, Message> = () => [
  { count: 0 },
  [],
]

// VIEW

export const view = (model: Model): Document => {
  const h = html<Message>()

  return {
    title: `Counter: ${model.count}`,
    body: h.div(
      [
        h.Class(
          'min-h-screen bg-white flex flex-col items-center justify-center gap-6 p-6',
        ),
      ],
      [
        h.div(
          [h.Class('text-6xl font-bold text-gray-800')],
          [model.count.toString()],
        ),
        h.div(
          [h.Class('flex flex-wrap justify-center gap-4')],
          [
            h.button(
              [h.OnClick(ClickedDecrement()), h.Class(buttonStyle)],
              ['-'],
            ),
            h.button(
              [h.OnClick(ClickedReset()), h.Class(buttonStyle)],
              ['Reset'],
            ),
            h.button(
              [h.OnClick(ClickedIncrement()), h.Class(buttonStyle)],
              ['+'],
            ),
          ],
        ),
      ],
    ),
  }
}

// STYLE

const buttonStyle = 'bg-black text-white hover:bg-gray-700 px-4 py-2 transition'
// src/entry.ts
import { Runtime } from 'foldkit'

import { Model, init, update, view } from './main'

const program = Runtime.makeProgram({
  Model,
  init,
  update,
  view,
  container: document.getElementById('root'),
})

Runtime.run(program)

Source: examples/counter/src/main.ts, examples/counter/src/entry.ts

What Ships With Foldkit

Foldkit is a complete system, not a collection of libraries you stitch together.

  • Commands: Side effects are named Effects that return Messages and are executed by the runtime. Define them with Command.define, passing the result Message schemas so the Effect's return type stays in lockstep with your Messages. Use any Effect combinator you want: retry, timeout, race, parallel. You write the Effect, the runtime runs it.
  • Mount: The seam where view code reaches a real DOM element, like focusing an input or handing the live Element to a third-party library that owns its own DOM. The runtime runs your Effect on mount, dispatches its Message back through update, and runs the paired cleanup on unmount.
  • Routing: Type-safe bidirectional routing built from parser combinators. URLs parse into typed Routes and Routes build back into URLs. No string matching, no mismatches between parsing and building.
  • Subscriptions: Declare which streams your app needs as a function of the Model. The runtime diffs and switches them as the Model changes.
  • Managed Resources: Model-driven lifecycle for long-lived browser resources like WebSockets, AudioContext, and RTCPeerConnection. Acquire on state change, release on cleanup.
  • Submodels: A pattern for composing nested modules. A child owns its own Model, Messages, update function, and view; the parent embeds it and wraps child Messages in a Got*Message envelope. The pattern scales unchanged from a login form to a multi-page app.
  • OutMessage: A typed channel for a child Submodel to emit domain events up to its parent, so the parent reacts to meaningful facts instead of internal child Messages.
  • UI Components: Accessible, keyboard-friendly primitives — Button, Checkbox, Combobox, Dialog, Disclosure, DragAndDrop, Fieldset, Input, Listbox, Menu, Popover, RadioGroup, Select, Switch, Tabs, Textarea, and Transition. Every component is a Submodel you embed and style through a typed ViewConfig.
  • Field Validation: Per-field validation state modeled as a discriminated union. Define rules as data, apply them in update, and the Model tracks the result.
  • Virtual DOM: Declarative views powered by Snabbdom, with lazy memoization and fast, keyed diffing. Views are plain functions of your Model.
  • DevTools: Built-in overlay for inspecting Messages, Model state, and Commands. Time-travel mode rewinds your UI to any past Model, Inspect mode browses snapshots without pausing, and Submodel drill-in filtering scopes the Message list to any nested module.
  • DevTools MCP: Expose a running Foldkit app to AI agents over the Model Context Protocol. Agents read the current Model, list and inspect Message history, rewind the UI to any past Model, and dispatch Messages into the runtime. The runtime's own Message Schema is published as JSON Schema so the agent discovers exactly what it can dispatch, and every payload is validated against the Schema before it reaches your update function. One command sets it up: npx @foldkit/devtools-mcp init.
  • Crash View and Reporting: Configure crash.view to render a custom fallback UI when the update loop throws. A crash.report callback fires first with the error, Model, and triggering Message, so you can ship it straight to Sentry or your logger.
  • Story Testing: Exercise the update function directly. Send Messages, resolve Commands inline with Story.Command.resolve and Story.Command.resolveAll, and assert with focused helpers: Story.model, Story.Command.expectHas, Story.Command.expectExact, Story.Command.expectNone, and Story.expectOutMessage. No mocking libraries, no fake timers.
  • Scene Testing: Drive your app the way a user does. Scene renders your real view, then clicks buttons, types into inputs, presses keys, and asserts on what's on screen — with accessible locators and Vitest matchers, no browser required.
  • Slow-View Monitoring: Wire slowView on makeProgram to catch renders that exceed a threshold you set. The callback fires with the current Model, the triggering Message, and the render duration, so you can log it, sample it, or ship it to your observability tool.
  • HMR: Vite plugin with state-preserving hot module replacement. Change your view, keep your state.

Correctness You (And Your LLM) Can See

Every state change flows through one update function. Every side effect is declared explicitly — in Commands, Mount Effects, Subscription streams, and Managed Resource lifecycles. You don't have to hold a mental model of what runs when — you can point at it.

This is what makes Foldkit unusually AI-friendly. The same property that makes the code easy for humans to reason about makes it easy for LLMs to generate and review. The architecture makes correctness visible, whether the reader is a person or an LLM.

Examples

  • Counter — Increment/decrement with reset
  • Todo — CRUD operations with localStorage persistence
  • Stopwatch — Timer with start/stop/reset
  • Crash View — Custom crash fallback UI with crash reporting
  • Form — Form validation with async email checking
  • Job Application — Multi-step form with cross-field validation, file uploads, and per-step error indicators
  • Weather — HTTP requests with async state handling
  • Routing — URL routing with parser combinators
  • Query Sync — URL query parameter sync with filtering and sorting
  • Snake — Classic game built with Subscriptions
  • Map — Interactive MapLibre GL map demonstrating Mount with a third-party DOM library
  • Auth — Authentication flow with Submodels and OutMessage
  • Shopping Cart — Nested models and complex state
  • WebSocket Chat — Managed Resources with WebSocket integration
  • Kanban — Drag-and-drop kanban board with cross-column reordering and keyboard navigation
  • Pixel Art — Grid-based pixel editor with painting, erasing, and palette selection
  • Canvas Art — Declarative 2D canvas with shapes, animation-frame Subscriptions, and pointer events
  • Generative Art — Prismatic flow-field particles steered by Perlin noise; move to stir, click to bloom
  • UI Showcase — Interactive showcase of every Foldkit UI component
  • Typing Game — Multiplayer typing game with Effect RPC backend (play it live)

Development

git clone https://github.com/foldkit/foldkit.git
cd foldkit
pnpm install

# Build Foldkit in watch mode
pnpm dev:core

# Run an example (in a separate terminal)
pnpm dev:example:counter

External reference repositories under repos/ are vendored in as git subtrees, so they come down with the clone. To refresh one later: git subtree pull --prefix=repos/effect-smol https://github.com/Effect-TS/effect-smol.git main --squash.

License

MIT

About

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 98.5%
  • Other 1.5%