---
title: "Alert 组件 — zenit Zig UI"
description: "页面内持久反馈。info / success / warning / error 语义色、标题和无标题消息保持一致布局。"
url: https://zenit.z.express/zh/components/alert
language: zh-CN
alternate_en: https://zenit.z.express/components/alert.md
alternate_es: https://zenit.z.express/es/components/alert.md
alternate_ja: https://zenit.z.express/ja/components/alert.md
alternate_ko: https://zenit.z.express/ko/components/alert.md
alternate_fr: https://zenit.z.express/fr/components/alert.md
alternate_de: https://zenit.z.express/de/components/alert.md
project: zenit v0.1.0-alpha (Zig 0.15.2, macOS)
source: https://github.com/version-next/zenit
---

# Alert

`ui.widgets.Alert`

页面内持久反馈。

[Video](https://zenit.z.express/media/stories/alert.mp4?v=b7df1ff242)

**BEHAVIOR CONTRACT**

info / success / warning / error 语义色、标题和无标题消息保持一致布局。

**RECORDED INTERACTION**

录像展示四种语义级别。

`examples/storybook/stories.zig:664`

```zig
// ── Alert ──
pub fn buildAlert(scope: *ui.Scope, cx: *ui.Cx) anyerror!*ui.Node {
    const a = cx.allocator;
    const c = try col(cx, 12);
    // 全 4 变体，且都带 closable（× 关闭按钮）
    try c.appendChild(a, try W.Alert(.{ .variant = .info, .title = "Info", .message = "Informational message.", .closable = true }).mount(scope, cx));
    try c.appendChild(a, try W.Alert(.{ .variant = .success, .title = "Success", .message = "It worked.", .closable = true }).mount(scope, cx));
    try c.appendChild(a, try W.Alert(.{ .variant = .warning, .title = "Warning", .message = "Be careful.", .closable = true }).mount(scope, cx));
    try c.appendChild(a, try W.Alert(.{ .variant = .@"error", .title = "Error", .message = "Something broke.", .closable = true }).mount(scope, cx));
    // 无标题（仅 message）
    try c.appendChild(a, try W.Alert(.{ .variant = .info, .message = "Title-less alert with just a message." }).mount(scope, cx));
    return c;
}
```

[在 GitHub 查看源码examples/storybook/stories.zig:664](https://github.com/version-next/zenit/blob/HEAD/examples/storybook/stories.zig#L664)

这是 Storybook 中该 story 的原样代码（zenit 5f9add5+wip 2026-09-30）。其中 `W` 即 `ui.widgets`，`col` / `row` / `label` 是 Storybook 内的布局小工具。

### AlertProps

`ui.widgets.Alert`

[src/ui/components/alert/mod.zig:40](https://github.com/version-next/zenit/blob/HEAD/src/ui/components/alert/mod.zig#L40)

```zig
fn Alert(props: AlertProps) AlertBuilder
```

| 字段 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `variant` | `AlertVariant` `.info` `.success` `.warning` `.@"error"` | `.info` | — |
| `title` | `?[]const u8` | `null` | — |
| `message` | `[]const u8` | `""` | — |
| `closable` | `bool` | `false` | — |
| `on_close` | `?core.HandlerRef` | `null` | — |
| `close_icon_asset` | `?svg_assets.Asset` | `null` | — |

### \*Node

`ui.widgets.Alert`

mount 直接返回 `*Node`，把它 append 到父节点即可。
