MessageContent
Example sourceGo · Copyable
This is the complete source for the gallery example above.examples/components/message_content.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_content", "chat", func() core.Widget {
mixed := kit.MessageContent(
kit.Bubble(kit.Label(demoText("Export complete. The file is below.", "导出已完成,文件在下方。"))),
kit.Attachment(demoText("order-summary.csv", "订单汇总.csv"), 2048),
kit.Bubble(kit.Label(demoText("The download link expires in 24 hours.", "下载链接将在 24 小时后过期。"))).Variant(kit.BubbleGhost),
)
msg := kit.Message(demoText("Assistant", "助手"), mixed).Header(kit.Label(demoText("Assistant · Just now", "助手 · 刚刚"))).Footer(kit.Label(demoText("Delivered", "已送达")))
return el.Embed(el.ViewFunc(func(cx *el.Context) el.Element {
return el.Div().P(24).Gap(16).Child(msg.Render(cx))
}))
})
}
Mix multiple bubbles, attachments, and normal views in the same message body. Bubbles passed directly will inherit the message alignment; when any direct bubble is a Ghost, the head and tail will be automatically indented. Explicit setting of HeaderInset/FooterInset still takes precedence.
content := kit.MessageContent(
kit.Bubble(kit.Label("Export complete")),
kit.Attachment("orders.csv", 2048),
kit.Bubble(kit.Label("The link expires in 24 hours")).Variant(kit.BubbleGhost),
)
msg := kit.Message("Assistant", content).Header(kit.Label("Just now"))
SetItems copies the new sequence and ignores nil, empty parameters are cleared; Items returns a copy. Children should be reused and appear only once per instance; custom views need to provide stable IDs. Persistent bubbles are rendered using an internal stable copy. Dynamic rearrangement retains its input and focus. The style and alignment of the original bubble will not be modified. Changing the source bubble's variant updates the style and Ghost inheritance on the next frame.
Gap sets non-negative finite dp, the default is theme.SpaceMd; Style configures the text stack, nil returns to default. Style callbacks are executed after default values and should not retain elements or add children. SetDisabled disables the entire body, and the disabling of the ancestor Message is also effective.
When installed directly through the Message constructor parameter or Content(content), the component is responsible for mixing and will not include another layer of User bubbles. The independent Render defaults to the left and uses the receiver's default bubble color. Bubbles wrapped in ordinary Views will not be recursively detected; automatic Ghost inheritance is only for direct Bubble children.
Run: go run ./examples/components -section message_content, add -theme dark to check the dark theme.