Mirror / foldkit-mirror

mirror

You want a todo filter, page number, open panel, or unsaved draft in your Foldkit app to survive a reload or live in a shareable URL like ?filter=active&page=2. Putting that state directly in location.search or localStorage as a second store forces components to read two places and risks async storage overwriting newer keystrokes; keeping it only in memory loses it when the page refreshes.

foldkit-mirror keeps selected fields of your Foldkit Model mirrored in the URL, a KeyValueStore, or a browser cookie while your Model stays the single source of truth. Your update writes to the Model as usual, a Subscription writes changes out when the encoded slice changes, and startup or navigation reads them back in.

See a filter mirrored in the URL in the live todo demo, or read Mirrored state for the full guide.

Choose between Mirror, Sync, and Remote

A mirror is a secondary representation for disposable, per-device state such as filters, sort order, page numbers, open panels, preferences, and drafts. It is last-write-wins with no durable log; use foldkit-sync when edits from multiple devices must survive offline and converge, and foldkit-remote when the server owns the data.

StateOwnerRepresentation
Filter, sort, page, search text, open panellocal ModelURL via Mirror.url
Preference, draft, collapsed sidebarlocal ModelKeyValueStore via Mirror.kv
Cart or preference rendered by the serverlocal Modelcookie via Mirror.cookie
Shared/offline edits that must convergefoldkit-sync / DurableSync journal
Server-derived factsserver / foldkit-remoteRemote cache

Every field keeps one owner: a mirror observes a local field and writes restored values into that field without claiming ownership away from your update.

How URL and store mirrors update the Model

URL and key-value mirrors share the same ownership rule, but read back into the Model at different times:

  • URL mirror: When your Model changes the filter, Filters.subscriptions updates the URL via history.pushState or replaceState. On navigation, the runtime sends UrlChanged, and Filters.urlChanged(model, url) reduces the URL back into the Model. The initial URL is already available at startup.
  • Key-value mirror: On a cold load, the Prefs.restore Command reads the KeyValueStore and sends MirrorRestored, which Prefs.reduce(model, message) applies to fields still at their initial values. Each later Model change writes outward through Prefs.subscriptions.

Every mirror integrates through three seams: reduce reads an external representation back into the Model, restore requests a store's cold-load value, and subscriptions writes Model changes outward.

Install

bash
pnpm add foldkit-mirror

foldkit and effect are peer dependencies; foldkit-surface comes with the package.

A first URL mirror: keep a filter in the URL

This mirrors filter and page into the query string, omitting fields still at their defaults and reading a URL back into the Model:

ts
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Url } from 'foldkit/url'
import { Surface } from 'foldkit-surface'
import { Mirror } from 'foldkit-mirror'

const Model = Schema.Struct({
  filter: Schema.Literals(['all', 'active', 'done']),
  page: Schema.Number,
})
const Message = defineMessageUnion({ UrlChanged: { url: Url } })
const initial: typeof Model.Type = { filter: 'all', page: 1 }

const App = Surface.application({
  Model,
  Message,
  initial,
  update: (model: typeof Model.Type) => ({ model }),
})
const Filters = Mirror.url(App, {
  name: 'filters',
  fields: [App.model.filter, App.model.page],
})

const activeUrl = Filters.href(initial, { filter: 'active', page: 2 }, '/todos')
// '/todos?filter=active&page=2'

const restored = Filters.reduce(initial, '/todos?filter=done&page=3')
// { filter: 'done', page: 3 }

What happened:

  • Mirror.url derives parameter names, default values ('all' and 1), and text codecs directly from App.model.filter and App.model.page.
  • Filters.href produces '/todos?filter=active&page=2'; when fields return to their defaults ({ filter: 'all', page: 1 }), Filters.href omits them and produces '/todos'.
  • Filters.reduce decodes '/todos?filter=done&page=3' back into { filter: 'done', page: 3 }.

When a mirror writes to the browser URL, Foldkit emits a UrlChanged echo for that write; Filters.urlChanged tracks every in-flight write and returns the Model unchanged so echoes never overwrite newer keystrokes or trigger a second render.