MessageGroup
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/message_group.go
Reuse the component construction and Render methods in a scaffolded project. The example’s registerSection and shared helpers are provided by the component gallery.
package main
import (
"github.com/dyike/keel/ui/core"
"github.com/dyike/keel/ui/el"
"github.com/dyike/keel/ui/kit"
)
func init() {
registerSection("message_group", "chat", func() core.Widget {
say := func(s string) el.View { return el.ViewFunc(func(*el.Context) el.Element { return el.Text(s) }) }
received := kit.MessageGroup(
kit.Message(demoText("Support", "客服"), say(demoText("Order confirmed.", "订单已确认。"))).Header(kit.Label(demoText("Support · 10:24", "客服 · 10:24"))),
kit.Message(demoText("Support", "客服"), say(demoText("Shipping is scheduled for tomorrow morning. The tracking number will be sent after dispatch.", "明天上午安排发货,运单号会在出库后发送。"))).Footer(kit.Label(demoText("Delivered", "已送达"))),
).Name(demoText("Consecutive support messages", "客服连续消息"))
sent := kit.MessageGroup(
kit.Message(demoText("Me", "我"), say(demoText("Got it, thanks. ", "收到,谢谢。 "))).User(),
kit.Message(demoText("Me", "我"), say(demoText("Please leave it at the reception desk. ", "请帮我备注放在门卫。 "))).User(),
).Name(demoText("My consecutive messages", "我的连续消息"))
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(24).Child(received.Render(cx), sent.Render(cx))
}))
})
}
Arrange complete message lines into vertical groups, retaining the avatar, header, body, status, action, reaction, and trailer of each line. The default is to fill the available width with a spacing of theme.SpaceMd (8dp). Grouping is determined by the application, with no inference of sender and no automatic hiding of avatars or metadata for consecutive messages.
first := kit.Message("Support", answer).Header(kit.Label("Support · 10:24"))
second := kit.Message("Support", details).Footer(kit.Label("Delivered"))
group := kit.MessageGroup(first, second).Name("Support messages").Gap(6)
group.SetItems(second, first)
SetItems copies the slice and ignores nil, empty parameters are cleared; Items returns a copy. Message instances should be reused, each appearing only once within the group. The input and focus of the retained message are retained when other items are inserted, deleted, or reordered. Custom Views can also be added, but you need to provide a stable ID yourself.
Gap accepts non-negative finite dp, illegal values are ignored. Style(func(*el.DivEl)) adjusts the background, border, spacing, etc. after the default layout; nil restores the default, and the callback is only used for the current frame element. Components retain internal IDs, group roles, names, and disabled states. SetDisabled disables all sub-items and does not modify the status of the sub-component itself.
The difference from BubbleGroup is the combination object: BubbleGroup is used for the bubble surface, and MessageGroup is used for the complete message line including avatar, metadata, and operations. Neither changes the alignment of the child.
Run: go run ./examples/components -section message_group, add -theme dark to check the dark theme.