Chat message
A message with its sender, time, status and actions: the user’s in a bubble, an answer as text, a sender’s run grouped, and a streamed body that never re-wraps.
Previews are drawn in the theme you made.
Edit itBasic #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column, GroupStyle, person } from '../../snippets/chat.js'
const Model = Schema.Struct({ liked: Schema.Boolean })
const Message = defineMessageUnion({ Copied: {}, Liked: {} })
// A question and its answer. What an action sends is the application's: copy
// would run a clipboard Command, and a toggle such as "Good answer" is
// `pressed` from the Model.
export default example({
title: 'Basic',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
state: {
Model,
Message,
init: () => ({ model: { liked: false } }),
update: (model, message) =>
Message.match(message, {
Copied: () => ({ model }),
Liked: () => ({ model: { liked: !model.liked } }),
}),
},
view: ({ style, model, onMessage, h }) =>
column(h, [
ChatMessage.group(
{
items: [
{
key: 'question',
from: 'user',
content: 'Which sizes does the ladder have?',
time: { at: '2026-10-17T09:30', label: '9:30' },
},
{
key: 'answer',
from: 'assistant',
author: 'Ada',
avatar: person(h, 'Ada Lovelace', 210),
content: 'Two ends, 36px and 28px, with the step knob between them.',
actions: [
{ key: 'copy', label: 'Copy', onClick: onMessage(Message.Copied()) },
{
key: 'like',
label: 'Good answer',
onClick: onMessage(Message.Liked()),
pressed: model.liked,
},
],
pinActions: true,
},
],
style: GroupStyle,
messageStyle: style,
},
h,
),
]),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Status and retry #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column, GroupStyle } from '../../snippets/chat.js'
const Model = Schema.Struct({ retried: Schema.Boolean })
const Message = defineMessageUnion({ Retried: {} })
// A message the server refused offers a retry; one still on its way says so.
// `words` renames the status and the button for the application's language.
export default example({
title: 'Status and retry',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
state: {
Model,
Message,
init: () => ({ model: { retried: false } }),
update: (model, message) =>
Message.match(message, {
Retried: () => (model.retried ? { model } : { model: { retried: true } }),
}),
},
view: ({ style, model, onMessage, h }) =>
column(h, [
ChatMessage.group(
{
items: [
{ key: 'sent', from: 'user', content: 'Book the room for Friday.', status: 'sent' },
model.retried
? { key: 'failed', from: 'user', content: 'And invite Grace.', status: 'sent' }
: {
key: 'failed',
from: 'user',
content: 'And invite Grace.',
status: 'failed',
onRetry: onMessage(Message.Retried()),
words: { failed: 'Not delivered', retry: 'Send again' },
},
{ key: 'sending', from: 'user', content: 'Thanks!', status: 'sending' },
],
style: GroupStyle,
messageStyle: style,
},
h,
),
]),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
A thread #
import { ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column, GroupStyle, person } from '../../snippets/chat.js'
// A channel: everyone at the start beside their picture, and a sender's next
// messages continued under the first.
export default example({
title: 'A thread',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
pins: { layout: 'thread' },
view: ({ style, h }) =>
column(h, [
ChatMessage.group(
{
items: [
{
key: 'deploy',
from: 'assistant',
author: 'Grace',
avatar: person(h, 'Grace Hopper', 20),
content: 'Deploy is green.',
time: { at: '2026-10-17T10:02', label: '10:02' },
},
{
key: 'noon',
from: 'assistant',
author: 'Grace',
avatar: person(h, 'Grace Hopper', 20),
content: 'Shipping at noon.',
},
{
key: 'thanks',
from: 'user',
author: 'Me',
avatar: person(h, 'Me', 140),
content: 'Great, thanks.',
},
],
style: GroupStyle,
messageStyle: style,
},
h,
),
]),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Streaming #
The ladder has two ends, 36
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Button, ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column } from '../../snippets/chat.js'
const Action = Recipes.Button.style({})
const ANSWER =
'The ladder has two ends, 36 pixels and 28, and the step knob moves every control between them at once, so a region set compact shrinks its buttons, fields and rows together.'.split(
' ',
)
const Model = Schema.Struct({ shown: Schema.Number })
const Message = defineMessageUnion({ More: {} })
// The same message drawn again as each word arrives: its body wraps greedily,
// so the words already shown stay on their lines, and it is aria-busy until
// the answer is whole. Here a press brings the next words; in an application,
// a stream does.
export default example({
title: 'Streaming',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
state: {
Model,
Message,
init: () => ({ model: { shown: 6 } }),
update: (model, message) =>
Message.match(message, {
More: () => ({
model: {
shown: model.shown >= ANSWER.length ? 3 : Math.min(model.shown + 4, ANSWER.length),
},
}),
}),
},
view: ({ style, model, onMessage, h }) => {
const done = model.shown >= ANSWER.length
return column(h, [
ChatMessage.view(
{
from: 'assistant',
author: 'Ada',
content: h.p([], [ANSWER.slice(0, model.shown).join(' ')]),
streaming: !done,
style,
},
h,
),
Button.view(
{
label: done ? 'Start over' : 'Next words',
onClick: onMessage(Message.More()),
style: Action,
},
h,
),
])
},
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Reactions #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column, GroupStyle, person } from '../../snippets/chat.js'
const Model = Schema.Struct({ up: Schema.Boolean })
const Message = defineMessageUnion({ ToggledUp: {} })
// Reactions sit under the bubble. One with an `onClick` is a toggle button (its
// count is the application's to change); one without is only shown.
export default example({
title: 'Reactions',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
state: {
Model,
Message,
init: () => ({ model: { up: false } }),
update: (model, message) =>
Message.match(message, { ToggledUp: () => ({ model: { up: !model.up } }) }),
},
view: ({ style, model, onMessage, h }) =>
column(h, [
ChatMessage.group(
{
items: [
{
key: 'plan',
from: 'assistant',
author: 'Grace',
avatar: person(h, 'Grace Hopper', 20),
content: 'The release is cut and the notes are up.',
reactions: [
{
key: 'up',
mark: '👍',
label: 'Thumbs up',
count: model.up ? 4 : 3,
pressed: model.up,
onClick: onMessage(Message.ToggledUp()),
},
{ key: 'party', mark: '🎉', label: 'Party', count: 2 },
],
},
],
style: GroupStyle,
messageStyle: style,
},
h,
),
]),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Bubble looks #
import { ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column, GroupStyle } from '../../snippets/chat.js'
// The user's bubble is a recipe axis: muted, accent, outline or ghost. The
// markup does not change; pick another in the panel.
export default example({
title: 'Bubble looks',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
pins: { bubble: 'outline' },
view: ({ style, h }) =>
column(h, [
ChatMessage.group(
{
items: [
{ key: 'q', from: 'user', content: 'Summarise the week in one line.' },
{
key: 'a',
from: 'assistant',
author: 'Ada',
content: 'Two releases, one incident, no regrets.',
},
],
style: GroupStyle,
messageStyle: style,
},
h,
),
]),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Thinking, typing, settled #
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import {
Button,
ChatMessage,
ChatMessageSlots,
Recipes,
ThinkingIndicator,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column } from '../../snippets/chat.js'
const Action = Recipes.Button.style({})
const Dots = Recipes.ThinkingIndicator.style({})
const Stage = Schema.Literals(['thinking', 'typing', 'settled'])
const Model = Schema.Struct({ stage: Stage })
const Message = defineMessageUnion({ Advanced: {} })
const next = (stage: typeof Stage.Type): typeof Stage.Type =>
stage === 'thinking' ? 'typing' : stage === 'typing' ? 'settled' : 'thinking'
// One row from thinking to typing to settled: the indicator is the content
// while the assistant works, the words replace it as they stream, and
// `streaming` keeps the row `aria-busy` until they are whole.
export default example({
title: 'Thinking, typing, settled',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
state: {
Model,
Message,
init: () => ({ model: { stage: 'thinking' } }),
update: (model, message) =>
Message.match(message, { Advanced: () => ({ model: { stage: next(model.stage) } }) }),
},
view: ({ style, model, onMessage, h }) =>
column(h, [
ChatMessage.view(
{
from: 'assistant',
author: 'Ada',
streaming: model.stage !== 'settled',
content:
model.stage === 'thinking'
? ThinkingIndicator.view({ detail: 'Reading the release notes', style: Dots }, h)
: h.p(
[],
[
model.stage === 'typing'
? 'Version 0.41 changes the default look and adds'
: 'Version 0.41 changes the default look and adds a Bundle and a configure to every stateful widget.',
],
),
style,
},
h,
),
Button.view(
{ label: 'Next stage', onClick: onMessage(Message.Advanced()), style: Action },
h,
),
]),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Quick replies #
When should I schedule it?
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Button, ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column } from '../../snippets/chat.js'
const Reply = Recipes.Button.style({ size: 'sm' })
const Model = Schema.Struct({ chosen: Schema.String })
const Message = defineMessageUnion({ Chose: { reply: Schema.String } })
// Anything is `content`: quick replies are Buttons in the body, and the
// actions row would still sit under them.
export default example({
title: 'Quick replies',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
state: {
Model,
Message,
init: () => ({ model: { chosen: '' } }),
update: (_, message) =>
Message.match(message, { Chose: ({ reply }) => ({ model: { chosen: reply } }) }),
},
view: ({ style, model, onMessage, h }) =>
column(h, [
ChatMessage.view(
{
from: 'assistant',
author: 'Ada',
content: h.div(
[h.Style({ display: 'grid', gap: '0.5rem' })],
[
h.p(
[],
[
model.chosen === ''
? 'When should I schedule it?'
: `Scheduled for ${model.chosen}.`,
],
),
...(model.chosen === ''
? [
h.div(
[h.Style({ display: 'flex', flexWrap: 'wrap', gap: '0.5rem' })],
['Tonight', 'Tomorrow', 'Next week'].map(reply =>
Button.view(
{
label: reply,
onClick: onMessage(Message.Chose({ reply })),
style: Reply,
},
h,
),
),
),
]
: []),
],
),
style,
},
h,
),
]),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Configure once #
import { ChatMessage, ChatMessageSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { column, GroupStyle } from '../../snippets/chat.js'
const Outlined = Recipes.ChatMessage.style({ bubble: 'outline' })
// What every message of an application starts from: its look and its words in
// its language. One use names a preset that changes how the actions show.
const AppChat = ChatMessage.configure({
base: {
style: Outlined,
words: {
sending: 'Wird gesendet…',
sent: 'Gesendet',
failed: 'Nicht gesendet',
retry: 'Nochmal',
},
pinActions: true,
},
presets: { quiet: { pinActions: false } },
})
export default example({
title: 'Configure once',
slots: ChatMessageSlots,
recipe: Recipes.ChatMessage,
view: ({ h }) =>
column(h, [
AppChat.group(
{
preset: 'quiet',
items: [
{ key: 'a', from: 'user', content: 'Sind wir bereit?', status: 'sent' },
{ key: 'b', from: 'user', content: 'Und die Notizen?', status: 'sending' },
],
style: GroupStyle,
},
h,
),
]),
})In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.