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 through Behaviors.Keys as the slot's single keydown owner.
  • 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 ownFormJoins the application byExamples
A fact that outlives the moment: a match, a count, a page, a positionbundleplacing it in the parent, as a field and a wrapper MessageMediaQuery, Timer, Pagination, websocket
Interaction state a view's slots must reflectbundle + Behaviorplacing it, then attaching its Behavior to the view's slotsRovingTabindex, Press, DismissLayer
Nothing: a stream you react toentrylifting it into your Subscriptions, mapped to your MessagekeyboardEvents, ticks, broadcastMessages
Nothing: observation bound to one elementMountattaching it in the view with h.OnMountResize, Autofocus, KeepInView
Nothing: work done once that reports backCommandreturning it from updatecopyText, share, enterFullscreen
Nothing: a value computed from what you havefunctioncalling itrange, 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

bash
pnpm add foldkit-primitives foldkit-bundle effect foldkit

effect 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:

ts
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.Model gains dark: { matches: boolean }, and Page.Message gains GotDarkMessage.
  • placements.initial({ theme: 'light' }) starts dark at { matches: false }, so initial render outputs <div>Light mode</div>.
  • Once the Foldkit runtime starts placements.subscriptions(), MediaQuery reads matchMedia('(prefers-color-scheme: dark)') and dispatches GotDarkMessage through placements.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 }