Keel v0.5.1 English GitHub

Alert

在线示例:这是真实的 Keel 程序编译成 WebAssembly,可以直接操作。首次加载约 10 MB。全屏打开
示例代码Go · 可复制

这是上方组件库展示案例的完整源码。examples/components/alert.go

在脚手架项目中复用组件构造和 Render 写法;示例的 registerSection 与共享辅助函数由组件库提供。

package main

import (
	"github.com/dyike/keel/ui/core"
	"github.com/dyike/keel/ui/el"
	"github.com/dyike/keel/ui/kit"
	"github.com/dyike/keel/ui/markdown"
)

func init() {
	registerSection("alert", "feedback", func() core.Widget {
		return el.Embed(&alertGallery{closable: kit.Alert(demoText("Dismissible alert", "可关闭提示")).Description(demoText("Can be restored after closing", "关闭后可以恢复")).OnClose(func() {})})
	})
}

type alertGallery struct{ closable *kit.AlertView }

func (v *alertGallery) Render(cx *el.Context) el.Element {
	return el.Div().W(el.Full).Gap(12).Child(
		kit.Alert(demoText("Maintenance banner", "维护横幅")).Banner(true).Size(kit.AlertSizeSmall).Icon(kit.IconCalendar).Description(demoText("Maintenance at 22:00 tonight. Save your work beforehand.", "今晚 22:00 维护,请提前保存工作。")).Render(cx),
		kit.Alert(demoText("Rich content", "富内容")).Content(markdown.New(demoText("**Payment incomplete**. Check:\n- Card details\n- Account balance", "**支付未完成**,请检查:\n- 卡片信息\n- 账户余额"))).Icon(kit.IconReceipt).Render(cx),
		kit.Alert(demoText("Compact alert", "紧凑提示")).Size(kit.AlertSizeXSmall).Icon(kit.IconNone).Description(demoText("Severity remains identifiable without an icon.", "无图标也保留等级标识。")).Render(cx),
		kit.Alert(demoText("Large alert", "大号提示")).Size(kit.AlertSizeLarge).Tone(kit.ToneSuccess).Render(cx), v.closable.Render(cx), el.Div().OnClick(func() { v.closable.SetVisible(true) }).P(8).Child(el.Text(demoText("Restore alert", "恢复提示"))),
		kit.Alert(demoText("Info", "提示")).Description(demoText("3 new messages. You can view them later.", "有 3 条新消息,稍后也可以查看。")).Render(cx),
		kit.Alert(demoText("Success", "成功")).Description(demoText("Order SO-123 saved.", "订单 SO-123 已保存。")).Tone(kit.ToneSuccess).Render(cx),
		kit.Alert(demoText("Warning", "警告")).Description(demoText("Unstable connection. Check your network and try again.", "连接不稳定,请检查网络后重试。")).Tone(kit.ToneWarning).Render(cx),
		kit.Alert(demoText("Danger", "失败")).Description(demoText("Save failed. Your input has been preserved.", "保存失败,输入内容已保留。")).Tone(kit.ToneDanger).Render(cx),
		el.Div().W(el.Dp(180)).Child(kit.Alert(demoText("Narrow container 123", "窄容器 123")).Description(demoText("Mixed text should wrap naturally without clipping.", "中英文 Mixed text should wrap naturally without clipping.")).Render(cx)),
	)
}

kit.Alert("保存失败").Tone(kit.ToneDanger).Description("网络不可用").OnClose(fn) 显示行内提示。默认 ToneInfo;Tone 支持 ToneNeutral、ToneInfo、ToneSuccess、ToneWarning、ToneDanger。SetTone、SetTitle、SetDescription 可程序更新。

左侧等级条和图标随主题取色,标题加粗,默认描述为 13sp Muted。

  • Size(AlertSizeXSmall/AlertSizeSmall/AlertSizeMedium/AlertSizeLarge) 设置四档字号、图标与内边距,默认 Medium 保留原布局;非法值忽略。
  • Icon(IconName) 替换等级图标,IconNone 隐藏图标及其间距。
  • Content(el.View) 替换描述,可传 Markdown 文档、富文本或操作按钮;传 nil 恢复 Description。正文控件保留自己的键盘与点击行为,并继承 Alert 的禁用状态。
  • Banner(true) 使用满宽、直角、无边框的染色横幅,不显示单独标题行;显示 Content 或 Description,没有正文时以标题作消息。关闭按钮和 Agent 名称仍使用标题。传 false 恢复行内提示。
notice := kit.Alert("维护通知").Banner(true).
    Description("今晚进行例行维护").Icon(kit.IconCalendar).
    Size(kit.AlertSizeSmall)
// 富文本由应用组合:notice.Content(markdown.New("**注意**:请先保存工作"))

只有设置 OnClose 才显示关闭按钮;点击或 Tab 聚焦后 Space/Enter 关闭,先隐藏再回调。Visible 查询状态,SetVisible 恢复或隐藏时不调用回调。

Agent 容器角色 alert,名字为标题,value 为等级。描述和关闭按钮单独可读。窄容器文字换行。运行 go run ./examples/components -section alert -theme dark;省略 theme 查看浅色。

SetDisabled(true) 禁止关闭并向按钮传递 disabled 语义;恢复启用后可以重新聚焦和关闭,不会因测量或主题切换丢失显隐状态。