Primitives / foldkit-primitives
primitives
In browser apps built with Foldkit—a TypeScript framework where application state lives in one Model and changes through update—wiring a media query, a keyboard shortcut, a timer, a resize observer, or roving keyboard focus often pulls you into ad-hoc event listeners that live outside your Model.
foldkit-primitives gives you ready-made pieces for browser, clock, device, and interaction state in the shape Foldkit already uses. Each primitive observes the platform and sends a Message into your update, so the fact lives in your Model, appears in DevTools, and replays cleanly.
See the pieces running live in the Primitives gallery.
How primitives fit into your Model and update
A primitive observes an event or DOM signal and reports it as a Message; your Model keeps the fact, and your update decides what it means.
Styled widgets live in foldkit-mixins-ui, and the slots and styles behind them live in foldkit-mixins. Offline replication and URL/storage persistence belong in foldkit-sync and foldkit-mirror.
For keyboard, pointer, and focus patterns (foldkit-primitives/interaction), primitives split into four parts:
- A Bundle holds the interaction state and
Messages (which item is focused, what is pressed). - A Mount reports a DOM event as a
Message(a press, a drag, where focus moved). - A Behavior attaches
tabindex,aria-*, and handlers to a view's slots, routing keys throughBehaviors.Keysas the slot's singlekeydownowner. - A Command performs a DOM effect after render, such as focusing a newly shown row.
See Authoring components for a step-by-step walkthrough of assembling a component from those four parts.
Which form a primitive takes
Every primitive takes one of six forms depending on what state it needs to own:
| It needs to own | Form | Joins the application by | Examples |
|---|---|---|---|
| A fact that outlives the moment: a match, a count, a page, a position | bundle | placing it in the parent, as a field and a wrapper Message | MediaQuery, Timer, Pagination, websocket |
| Interaction state a view's slots must reflect | bundle + Behavior | placing it, then attaching its Behavior to the view's slots | RovingTabindex, Press, DismissLayer |
| Nothing: a stream you react to | entry | lifting it into your Subscriptions, mapped to your Message | keyboardEvents, ticks, broadcastMessages |
| Nothing: observation bound to one element | Mount | attaching it in the view with h.OnMount | Resize, Autofocus, KeepInView |
| Nothing: work done once that reports back | Command | returning it from update | copyText, share, enterFullscreen |
| Nothing: a value computed from what you have | function | calling it | range, matchHotkey, formatRelativeTime |
Choose the lighter form when you do not need persistent state; promote to a bundle when the state needs a named field in your Model.
Stateful bundles use foldkit-bundle for placement. The interaction subpath also uses foldkit-mixins as an optional peer because its Behaviors write attributes onto view slots.
Install
pnpm add foldkit-primitives foldkit-bundle effect foldkiteffect and foldkit are peer dependencies. Import by subpath (/media, /net, /time, /state, /motion, /interaction, /device, /events, /observers, /dom) so your bundler keeps only what you use.
A first primitive: follow the color scheme
This places MediaQuery inside a parent page so model.dark.matches tracks the visitor's dark-mode preference:
import { Schema } from 'effect'
import type { HtmlBuilder } from 'foldkit/html'
import { Bundle } from 'foldkit-bundle'
import { MediaQuery } from 'foldkit-primitives/media'
const Page = Bundle.compose({ theme: Schema.String }).pipe(
Bundle.withMessages({ ThemeSet: { theme: Schema.String } }),
Bundle.withChild('dark', MediaQuery, { args: { query: '(prefers-color-scheme: dark)' } }),
)
type Model = typeof Page.Model.Type
type Message = typeof Page.Message.Type
const { placements } = Page
const config = placements.complete({
init: () => placements.initial({ theme: 'light' }),
update: placements.update(model => ({ model })),
view: (model: Model, h: HtmlBuilder<Message>) =>
h.div([], [model.dark.matches ? 'Dark mode' : 'Light mode']),
subscriptions: placements.subscriptions(),
})What happened:
Page.Modelgainsdark: { matches: boolean }, andPage.MessagegainsGotDarkMessage.placements.initial({ theme: 'light' })startsdarkat{ matches: false }, so initial render outputs<div>Light mode</div>.- Once the Foldkit runtime starts
placements.subscriptions(),MediaQueryreadsmatchMedia('(prefers-color-scheme: dark)')and dispatchesGotDarkMessagethroughplacements.update(...), updating the view to<div>Dark mode</div>whenever dark mode is active.
Unlike a standalone hook outside your Model, model.dark.matches stays inspectable in DevTools, deterministic under replay, and safe during server rendering.
Running in this page
This site places two primitives in its own Model. What each holds is drawn from that Model, as it changes.
PrefersDark · your system asks for a light color scheme.
model.dark = { matches: false }Timer · 0 ticks.
model.ticks = { count: 0, running: false }