---
title: "Modal 组件 — zenit Zig UI"
description: "阻塞式任务、确认与聚焦上下文。window-root portal、barrier、焦点、Escape、inside / outside hit-test 和入场动画一起保证。"
url: https://zenit.z.express/zh/components/modal
language: zh-CN
alternate_en: https://zenit.z.express/components/modal.md
alternate_es: https://zenit.z.express/es/components/modal.md
alternate_ja: https://zenit.z.express/ja/components/modal.md
alternate_ko: https://zenit.z.express/ko/components/modal.md
alternate_fr: https://zenit.z.express/fr/components/modal.md
alternate_de: https://zenit.z.express/de/components/modal.md
project: zenit v0.1.0-alpha (Zig 0.15.2, macOS)
source: https://github.com/version-next/zenit
---

# Modal

`ui.widgets.Modal`

阻塞式任务、确认与聚焦上下文。

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

**BEHAVIOR CONTRACT**

window-root portal、barrier、焦点、Escape、inside / outside hit-test 和入场动画一起保证。

**RECORDED INTERACTION**

Harness 打开居中 dialog，并悬停内部按钮验证不会误关。

`examples/storybook/stories.zig:2504`

```zig
pub fn buildModal(scope: *ui.Scope, cx: *ui.Cx) anyerror!*ui.Node {
    const a = cx.allocator;
    const c = try col(cx, 12);
    const vis = try scope.createSignal(bool, false);
    const st = try cx.bindState(ModalStory, .{ .visible = vis });
    const btn = try W.Button(.{ .label = "Open Modal", .on_click = cx.on(ModalStory, st, ModalStory.open) }).mount(scope, cx);
    btn.meta.ownership.meta.test_id = "story.modal.open";
    try c.appendChild(a, btn);

    const m = try W.Modal(.{ .title = "Example Modal", .width = 400 }).visible(vis).mount(scope, cx);
    m.dialog.meta.ownership.meta.test_id = "story.modal.dialog";
    try m.body.appendChild(a, try ui.text(cx, "Modal body content.", .{ .font_size = 14, .color = light.color.fg_primary }));
    // 带 hover 样式的按钮：damage-rect e2e 用它触发"层内小块变化 → 部分重绘"
    const ok_btn = try W.Button(.{ .label = "OK" }).mount(scope, cx);
    ok_btn.meta.ownership.meta.test_id = "story.modal.ok";
    try m.body.appendChild(a, ok_btn);
    // portaled 时 overlay 已挂到 window-root portal（覆盖整窗口）；未 portal 时才内联。
    if (!m.portaled) try c.appendChild(a, m.overlay);
    return c;
}
```

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

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

### ModalProps

`ui.widgets.Modal`

[src/ui/components/modal/mod.zig:25](https://github.com/version-next/zenit/blob/HEAD/src/ui/components/modal/mod.zig#L25)

```zig
fn Modal(props: ModalProps) ModalBuilder
```

| 字段 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `title` | `?[]const u8` | `null` | 标题 |
| `width` | `f32` | `480` | 宽度 |
| `max_height` | `f32` | `600` | 最大高度 |
| `overlay_color` | `?Color` | `null` | 遮罩颜色 (null = 使用主题 overlay) |
| `background` | `?Color` | `null` | 对话框背景 |
| `close_on_overlay` | `bool` | `true` | 点击遮罩关闭 |
| `visible` | `?*Signal(bool)` | `null` | 显示控制 Signal（外部传入） |
| `close_icon_asset` | `?svg_assets.Asset` | `null` | 关闭按钮 SVG 资产（可选，null 则回退文本 ✕） |

### ModalResult

`ui.widgets.Modal`

[src/ui/components/modal/mod.zig:63](https://github.com/version-next/zenit/blob/HEAD/src/ui/components/modal/mod.zig#L63)

| 字段 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `overlay` | `*Node` | — | — |
| `dialog` | `*Node` | — | — |
| `body` | `*Node` | — | — |
| `portaled` | `bool` | — | — |
