Primitives / DOM
Start with one: copy to the clipboard
ts
import { mapMessage } from 'foldkit/command'
import { defineMessageUnion } from 'foldkit/message'
import { ClipboardMessage, copyText } from 'foldkit-primitives/dom'
const Message = defineMessageUnion({
CopyClicked: {},
Clipboard: { message: ClipboardMessage },
})
type Model = { readonly text: string; readonly status: string }
const update = (model: Model, message: typeof Message.Type) =>
Message.match(message, {
CopyClicked: () => ({
model,
commands: [mapMessage(copyText(model.text), message => Message.Clipboard({ message }))],
}),
Clipboard: ({ message }) => ({
model: {
...model,
status: ClipboardMessage.match(message, {
Copied: () => 'Copied',
CopyFailed: ({ message }) => message,
}),
},
}),
})copyText describes a Command; returning it from update lets the runtime
perform the write. mapMessage wraps its result into the parent's union, so
success and failure land in the same reducer, and the clipboard never enters
the Model: it is not application state. readText() reads the same way.