Opens a reply thread beside a channel, or as a sheet on a phone.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Channel, ChannelMessage } from "./ui/uicontext/components/kit/Channel.ts"import { Composer } from "./ui/uicontext/components/kit/Composer.ts"import { toggleReaction } from "./ui/uicontext/components/kit/Reactions.ts"import { ThreadPane } from "./ui/uicontext/components/kit/ThreadPane.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, radius } from "./ui/uicontext/components/tokens.stylex.ts"import { ada, channel, members, now, thread } from "./team.ts"const styles = stylex.create({ // the channel and the pane share one row; the pane measures it to choose a side panel or a sheet frame: { display: "flex", blockSize: 560, inlineSize: "100%", overflow: "hidden", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.lg, backgroundColor: color.panel, }, channel: { display: "grid", flexGrow: 1, minInlineSize: 0 },})const Message = UiSchema.TaggedEnum({ OpenedThread: { message: Schema.String }, ClosedThread: {}, SentReply: { text: Schema.String }, Reacted: { message: Schema.String, emoji: Schema.String, on: Schema.Boolean },})// Each message's replies, by the id of the message that started the thread.const Replies = Schema.Record(Schema.String, Schema.Array(ChannelMessage))export const TriageThread = Html.component({ name: "TriageThread", model: Schema.Struct({ messages: Schema.Array(ChannelMessage), replies: Replies, open: Schema.NullOr(Schema.String), sent: Schema.Number, }), message: Message,}).make({ init: () => ({ messages: channel.slice(0, 3), replies: { c2: thread.slice(1) }, open: "c2", sent: 0 }), update: { OpenedThread: ($, { message }) => $.evolve({ open: message }), ClosedThread: ($) => $.evolve({ open: null }), // the reply joins the open thread, and the parent's count and last reply follow SentReply: ($, { text }) => { const parent = $.model.open if (parent === null) return $.model const at = now const reply: ChannelMessage = { id: `r${$.model.sent + 1}`, actor: ada, at, body: text } const replies = [...($.model.replies[parent] ?? []), reply] return $.evolve({ sent: (n) => n + 1, replies: (all) => ({ ...all, [parent]: replies }), messages: (all) => all.map((m) => m.id !== parent ? m : { ...m, replies: { count: replies.length, participants: [...new Map(replies.map((r) => [r.actor.id, r.actor])).values()], lastAt: at, }, }), }) }, // a reaction can land on the channel's messages or a thread's replies Reacted: ($, { message, emoji, on }) => { const react = (m: ChannelMessage) => m.id === message ? { ...m, reactions: toggleReaction(m.reactions ?? [], emoji, on, ada.name) } : m return $.evolve({ messages: (all) => all.map(react), replies: (all) => Object.fromEntries(Object.entries(all).map(([k, v]) => [k, v.map(react)])), }) }, }, view: (h, { messages, replies, open }) => { const parent = messages.find((m) => m.id === open) const onEmit = { RequestedOpenThread: ({ message }: { readonly message: string }) => Message.OpenedThread({ message }), RequestedReaction: ({ message, emoji, on }: { readonly message: string; readonly emoji: string; readonly on: boolean }) => Message.Reacted({ message, emoji, on }), RequestedDismiss: () => undefined, RequestedEdit: () => undefined, RequestedSave: () => undefined, RequestedDelete: () => undefined, RequestedRetry: () => undefined, RequestedMarkUnread: () => undefined, RequestedAction: () => undefined, } return h.div.attrs(sx(styles.frame)).children( h.div.attrs(sx(styles.channel)).children(Channel(h, { key: "triage", props: { label: "#triage", agentId: "thread-pane-demo:channel", messages, me: ada.id, now, timeZone: "UTC" }, onEmit, })), ThreadPane(h, { props: { open: parent !== undefined, title: "Thread", subtitle: "#triage" }, onEmit: { RequestedClose: () => Message.ClosedThread() }, slots: { content: parent === undefined ? null : Channel(h, { key: `thread-${parent.id}`, props: { label: "Thread replies", agentId: "thread-pane-demo:thread", messages: [parent, ...(replies[parent.id] ?? [])], me: ada.id, now, timeZone: "UTC", thread: true, }, onEmit, }), composer: Composer(h, { key: "reply", props: { label: "Reply in thread", agentId: "thread-pane-demo:reply", placeholder: "Reply…", mentionables: members, hints: false }, onEmit: { Sent: ({ text }) => Message.SentReply({ text }), Attached: () => undefined, RequestedStop: () => undefined, RequestedEditLast: () => undefined, Drafted: () => undefined }, }), }, }), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/thread-paneUsage
import { ThreadPane } from "./ui/uicontext/components/kit/ThreadPane.ts"const Message = UiSchema.TaggedEnum({ ClosedThread: {} })// `replies` and `composer` are your Channel (with `thread: true`) and Composerexport const ReplyPane = ( h: Html.H<typeof Message.Type>, open: boolean, replies: View.Content<typeof Message.Type>, composer: View.Content<typeof Message.Type>,) => ThreadPane(h, { props: { open, title: "Thread", subtitle: "#triage" }, onEmit: { RequestedClose: () => Message.ClosedThread() }, slots: { content: replies, composer }, })Examples
As a sheet
When the space it sits in is narrower than breakpoint, the thread opens as a bottom sheet instead of a side panel.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { Channel } from "./ui/uicontext/components/kit/Channel.ts"import { Composer } from "./ui/uicontext/components/kit/Composer.ts"import { ThreadPane } from "./ui/uicontext/components/kit/ThreadPane.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { ada, members, now, thread } from "./team.ts"const styles = stylex.create({ frame: { display: "flex", alignItems: "center", gap: 12 },})const Message = UiSchema.TaggedEnum({ ClickedOpen: {}, ClosedThread: {} })export const ThreadSheet = Html.component({ name: "ThreadSheet", model: Schema.Struct({ open: Schema.Boolean }), message: Message,}).make({ init: () => ({ open: false }), update: { ClickedOpen: ($) => $.evolve({ open: true }), ClosedThread: ($) => $.evolve({ open: false }), }, view: (h, { open }) => h.div.attrs(sx(styles.frame)).children( Button(h, { emphasis: "outline" }).on("click", Message.ClickedOpen()).children("Open thread (3 replies)"), ThreadPane(h, { // a breakpoint wider than the space it has: here it always opens as a bottom sheet, as on a phone props: { open, title: "Thread", subtitle: "#triage", breakpoint: 4000 }, onEmit: { RequestedClose: () => Message.ClosedThread() }, slots: { content: Channel(h, { key: "thread", props: { label: "Thread replies", agentId: "thread-pane-sheet:thread", messages: thread, me: ada.id, now, timeZone: "UTC", thread: true }, onEmit: { RequestedOpenThread: () => undefined, RequestedReaction: () => undefined, RequestedDismiss: () => undefined, RequestedAction: () => undefined, RequestedEdit: () => undefined, RequestedSave: () => undefined, RequestedDelete: () => undefined, RequestedRetry: () => undefined, RequestedMarkUnread: () => undefined }, }), composer: Composer(h, { key: "reply", props: { label: "Reply in thread", agentId: "thread-pane-sheet:reply", placeholder: "Reply…", mentionables: members, hints: false }, onEmit: { Sent: () => undefined, Attached: () => undefined, RequestedStop: () => undefined, RequestedEditLast: () => undefined, Drafted: () => undefined }, }), }, }), ),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
openrequired | boolean | — | Whether the pane shows |
titlerequired | string | — | The pane's heading: "Thread" |
subtitle | string | — | Where the thread is: "#triage" |
breakpoint | number | 720 | Below this width (in CSS pixels) of the space it sits in, the pane is a bottom Sheet; 720 by default |
compactAs | "sheet" | "screen" | — | What it becomes below the breakpoint: a bottom sheet (the default), or a screen of its own |
Events
| Event | Payload | Description |
|---|---|---|
RequestedClose | {} | The person closed the pane: its close button, Escape, or the sheet's backdrop |
Slots
content, composer
Accessibility
- The pane is a landmark named by its title; opening it moves focus to the title, and closing it returns focus to what opened it.
- Escape or the Close thread button closes it. On a narrow screen it opens as a sheet from the bottom instead.