Alert 警告提示
以横幅形式展示需要用户注意的成功、信息、警告或错误状态。
何时使用
- 在页面内容中展示持续可见的状态、风险或操作结果。
- 需要用户主动关闭,或需要附加操作(如撤销、重试)时使用。
- 只需短暂反馈时用 Message;需要确认操作时用 Popconfirm。
在线体验
基本用法
import "github.com/tangoui/tango/ui"
func alertPage(c *ui.Context) {
ui.Alert(c, "info", "有新版本可用").Show()
}status 支持 success、info、warning、error。链式设置完成后调用 Show() 构建组件。
常见用法
辅助说明与关闭
func alertDescription(c *ui.Context) {
ui.Alert(c, "warning", "请检查配置").
Description("保存前仍有两个必填项未完成。").Closable().Show()
}Closable() 显示关闭按钮。调用 Show() 后,用 Closed() 读取本帧的关闭动作并更新业务状态。
深色效果与居中
func alertEffects(c *ui.Context) {
ui.Alert(c, "success", "发布成功").Effect(ui.AlertEffectDark).Center().Show()
ui.Alert(c, "error", "发布失败").NoIcon().Show()
}Effect(ui.AlertEffectDark) 使用状态色实底;Center() 居中文本和图标;NoIcon() 隐藏状态图标。
附加操作
func alertAction(c *ui.Context) {
ui.Alert(c, "info", "草稿已保存").Action(func() {
ui.Button(c, "撤销").Small()
}).Show()
}Action 的回调构建右侧操作区,适合放置小型按钮;不要在其中塞入复杂表单。
Banner 模式
func alertBanner(c *ui.Context) {
ui.Alert(c, "warning", "系统将在今晚维护").Banner().Show()
}Banner() 适合放在页面或窗口顶部,默认会显示状态图标。普通提示的图标默认隐藏,可用 ShowIcon(true) 打开。
主题与样式
Alert 的状态色、圆角、间距和图标由当前主题控制。ConfigProvider.Defaults("Alert", ...) 支持 showIcon、closable、banner 三个布尔值和 variant 字符串。Ant Design 的 Variant("outlined") 默认带状态色边框,Variant("filled") 去掉边框。Effect(ui.AlertEffectDark) 对应 Element Plus 的深色实底,shadcn 使用带边框的 Alert 卡片。
注意事项
即时模式下,把是否显示、业务文案等状态放在页面函数外或 ui.State 中,避免每帧重置。关闭后应由业务状态决定是否再次构建。可关闭提示必须提供明确文案;键盘用户可以聚焦关闭按钮并用 Enter 或空格操作。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Alert
func Alert(c *Context, status, title string) *AlertWAlert adds a banner; call Show after options. status: success, info, warning, error. title is antd's title (v6; message before).
As in antd and Element Plus the icon is off unless ShowIcon(true) (or Banner, which shows it by default).
ConfigProvider.Defaults("Alert", ...) keys: "showIcon", "closable", "banner" (bool), "variant" (string: "outlined" | "filled").
Under shadcn/ui it is the bordered Alert card with an outlined (lucide-style) icon; "error" is the destructive variant.
返回 *AlertW,可链式调用的方法见 AlertW 方法。
AlertW 方法
ui.AlertW:AlertW is an inline status banner.
| 方法 | 说明 | 签名 |
|---|---|---|
Action | Action builds an operation area at the right of the text (Ant action), e.g. a small button. | Action(fn func()) *AlertW |
Banner | Banner styles the alert as a page-top banner: no border or radius, and the icon on unless ShowIcon(false) (antd banner). | Banner() *AlertW |
Center | Center centers the icon and text horizontally (Element center); the close button stays at the end. | Center() *AlertW |
Closable | Closable adds the close button. | Closable() *AlertW |
CloseDisabled | CloseDisabled disables the close button (antd closable.disabled); ConfigProvider.ComponentDisabled disables it too. | CloseDisabled(on bool) *AlertW |
CloseIcon | CloseIcon replaces the close icon and makes the alert closable (antd closable.closeIcon). | CloseIcon(ic *icon.Icon) *AlertW |
CloseText | CloseText shows text instead of the close icon and makes the alert closable (EP close-text). | CloseText(s string) *AlertW |
Closed | Closed reports a click on the close button. | Closed() bool |
Description | Description(d string) *AlertW | |
Effect | Effect selects the fill: AlertEffectLight (default) or AlertEffectDark, the solid status color with contrasting text (Element effect="dark"). | Effect(e AlertEffect) *AlertW |
Icon | Icon replaces the status icon (antd icon) and shows it. | Icon(ic *icon.Icon) *AlertW |
NoIcon | NoIcon hides the status icon (same as ShowIcon(false)). | NoIcon() *AlertW |
Show | Show builds the alert. | Show() *AlertW |
ShowIcon | ShowIcon shows or hides the status icon (antd showIcon, EP show-icon; default off, on for a Banner). | ShowIcon(on bool) *AlertW |
Variant | Variant selects "outlined" (default, status border) or "filled" (no border) (antd 6.4 variant). | Variant(v string) *AlertW |