Components

Message scroller

A conversation that follows the newest message while you are at the end, stops when you scroll up, counts what arrives, and holds its place as older messages load.

Previews are drawn in the theme you made.

Edit it
scheme
direction

Follows the end #

What is a recipe?
Pieces of style for each slot of a component, picked by its axes.
And a slot?
A named part of the component, which a style or a Mixin reaches.
Can I change one?
Extend the recipe, or add a rule in your app layer.
Which layer wins?
The app layer, after components and variants.
import { Match, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { modifyFields } from 'foldkit/struct'
import {
  Bundles,
  Button,
  ChatMessage,
  MessageScroller,
  MessageScrollerSlots,
  Recipes,
  ScrollArea,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { GroupStyle } from '../../snippets/chat.js'
import { row } from '../../snippets/layout.js'

const Area = Recipes.ScrollArea.style({})
const Bubble = Recipes.ChatMessage.style({})
const Action = Recipes.Button.style({})
const Quiet = Recipes.Button.style({ tone: 'neutral', variant: 'outline' })

const Entry = Schema.Struct({
  id: Schema.String,
  from: Schema.Literals(['user', 'assistant']),
  text: Schema.String,
})
type Entry = typeof Entry.Type

const first: ReadonlyArray<Entry> = [
  'What is a recipe?',
  'Pieces of style for each slot of a component, picked by its axes.',
  'And a slot?',
  'A named part of the component, which a style or a Mixin reaches.',
  'Can I change one?',
  'Extend the recipe, or add a rule in your app layer.',
  'Which layer wins?',
  'The app layer, after components and variants.',
].map((text, at) => ({ id: `chat-${at}`, from: at % 2 === 0 ? 'user' : 'assistant', text }))

// Whether the scroller follows the end is the Bundle's Model. The application
// sends it `Appended` when a message lands at the end, and nothing when older
// ones load above: it holds the reader's place and counts nothing.
const App = Bundle.compose({
  entries: Schema.Array(Entry),
  sent: Schema.Number,
  loaded: Schema.Number,
}).pipe(
  Bundle.withMessages({ Sent: {}, LoadedOlder: {} }),
  Bundle.withChild('chat', Bundles.MessageScroller, { args: { id: 'scroller-basic' } }),
)

export default example({
  title: 'Follows the end',
  slots: MessageScrollerSlots,
  recipe: Recipes.MessageScroller,
  state: App.placements.runtime({
    Model: App.Model,
    initial: { entries: first, sent: 0, loaded: 0 },
    update: App.placements.update((model, message) =>
      Match.value(message).pipe(
        Match.tagsExhaustive({
          Sent: () => {
            const sent = model.sent + 1
            return {
              model: modifyFields(model, {
                sent: () => sent,
                entries: entries => [
                  ...entries,
                  { id: `sent-${sent}`, from: 'user', text: `Message ${sent}` },
                  { id: `reply-${sent}`, from: 'assistant', text: `Reply ${sent}` },
                ],
                chat: chat =>
                  MessageScroller.update(chat, MessageScroller.Message.Appended({ count: 2 }))
                    .model,
              }),
            }
          },
          LoadedOlder: () => {
            const loaded = model.loaded + 1
            const older = Array.from({ length: 4 }, (_, at): Entry => ({
              id: `older-${loaded}-${at}`,
              from: at % 2 === 0 ? 'user' : 'assistant',
              text: `An older message, ${loaded}.${at + 1}`,
            }))
            return {
              model: modifyFields(model, {
                loaded: () => loaded,
                entries: entries => [...older, ...entries],
              }),
            }
          },
        }),
      ),
    ),
  }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ display: 'grid', gap: '0.75rem', inlineSize: 'min(32rem, 100%)' })],
      [
        h.div(
          [h.Style({ blockSize: '20rem' })],
          [
            MessageScroller.view(
              {
                model: model.chat,
                label: 'Conversation with Ada',
                content: [
                  ChatMessage.group(
                    {
                      items: model.entries.map(({ id, from, text }) => ({
                        key: id,
                        from,
                        content: text,
                      })),
                      style: GroupStyle,
                      messageStyle: Bubble,
                    },
                    h,
                  ),
                ],
                onMessage: message => onMessage(App.Message.GotChatMessage({ message })),
                style,
                areaStyle: Area,
                areaMixins: ScrollArea.overlayBars,
              },
              h,
            ),
          ],
        ),
        row(h, [
          Button.view({ label: 'Send', onClick: onMessage(App.Message.Sent()), style: Action }, h),
          Button.view(
            { label: 'Load older', onClick: onMessage(App.Message.LoadedOlder()), style: Quiet },
            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.

Opens at the start #

Line 1 of a transcript the reader opens at the top.
Line 2 of a transcript the reader opens at the top.
Line 3 of a transcript the reader opens at the top.
Line 4 of a transcript the reader opens at the top.
Line 5 of a transcript the reader opens at the top.
Line 6 of a transcript the reader opens at the top.
Line 7 of a transcript the reader opens at the top.
Line 8 of a transcript the reader opens at the top.
Line 9 of a transcript the reader opens at the top.
Line 10 of a transcript the reader opens at the top.
Line 11 of a transcript the reader opens at the top.
Line 12 of a transcript the reader opens at the top.
Line 13 of a transcript the reader opens at the top.
Line 14 of a transcript the reader opens at the top.
import {
  Bundles,
  ChatMessage,
  MessageScroller,
  MessageScrollerSlots,
  Recipes,
  ScrollArea,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { GroupStyle } from '../../snippets/chat.js'

const Area = Recipes.ScrollArea.style({})
const Bubble = Recipes.ChatMessage.style({})

const lines = Array.from({ length: 14 }, (_, at) => ({
  key: `line-${at}`,
  from: 'assistant' as const,
  content: `Line ${at + 1} of a transcript the reader opens at the top.`,
}))

// `opens: 'start'` is the Bundle's arg: a transcript is read from its top, so
// nothing is followed until the reader reaches the end, and the jump button
// shows meanwhile.
export default example({
  title: 'Opens at the start',
  slots: MessageScrollerSlots,
  recipe: Recipes.MessageScroller,
  state: Bundles.MessageScroller.with({ id: 'scroller-start', opens: 'start' }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ blockSize: '16rem', inlineSize: 'min(32rem, 100%)' })],
      [
        MessageScroller.view(
          {
            model,
            label: 'Transcript',
            content: [
              ChatMessage.group({ items: lines, style: GroupStyle, messageStyle: Bubble }, h),
            ],
            onMessage,
            style,
            areaStyle: Area,
            areaMixins: ScrollArea.overlayBars,
          },
          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 #

Nachricht 1
Nachricht 2
Nachricht 3
Nachricht 4
Nachricht 5
Nachricht 6
Nachricht 7
Nachricht 8
Nachricht 9
Nachricht 10
Nachricht 11
Nachricht 12
import {
  Bundles,
  ChatMessage,
  MessageScroller,
  MessageScrollerSlots,
  Recipes,
  ScrollArea,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { GroupStyle } from '../../snippets/chat.js'

const Area = Recipes.ScrollArea.style({})
const Bubble = Recipes.ChatMessage.style({})

// What every conversation of an application starts from: the scroll area it
// sits in and its words. The Model and the content stay each use's.
const AppScroller = MessageScroller.configure({
  base: {
    areaStyle: Area,
    areaMixins: ScrollArea.overlayBars,
    words: { jump: 'Zum Ende', one: '{count} neue Nachricht', other: '{count} neue Nachrichten' },
  },
})

export default example({
  title: 'Configure once',
  slots: MessageScrollerSlots,
  recipe: Recipes.MessageScroller,
  state: Bundles.MessageScroller.with({ id: 'scroller-configure', opens: 'start' }),
  view: ({ style, model, onMessage, h }) =>
    h.div(
      [h.Style({ blockSize: '14rem', inlineSize: 'min(32rem, 100%)' })],
      [
        AppScroller.view(
          {
            model,
            label: 'Unterhaltung',
            content: [
              ChatMessage.group(
                {
                  items: Array.from({ length: 12 }, (_, at) => ({
                    key: `m-${at}`,
                    from: 'assistant' as const,
                    content: `Nachricht ${at + 1}`,
                  })),
                  style: GroupStyle,
                  messageStyle: Bubble,
                },
                h,
              ),
            ],
            onMessage,
            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.

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.

The recipe ↗This code ↗