components/menu
导航 · 37 / 64

Menu

ui.widgets.Menu

上下文动作列表与快捷键提示。

BEHAVIOR CONTRACT

trigger、portal、separator、danger item、outside dismiss 和 Escape 成套实现。

RECORDED INTERACTION

Harness 打开 Actions 菜单并展示 Cut / Copy / Delete。

examples/storybook/stories.zig:2335
// ── Menu(trigger+popover:item 在弹层里,story 给 trigger 加按钮,e2e 点开验 item)──
pub fn buildMenu(scope: *ui.Scope, cx: *ui.Cx) anyerror!*ui.Node {
    const a = cx.allocator;
    const c = try col(cx, 8);
    try c.appendChild(a, try label(cx, "Click the trigger to open the menu"));
    const items = [_]W.MenuItem{
        .{ .id = "cut", .label_text = "Cut", .shortcut = "⌘X" },
        .{ .id = "copy", .label_text = "Copy", .shortcut = "⌘C" },
        .{ .id = "sep", .kind = .separator },
        .{ .id = "del", .label_text = "Delete", .danger = true },
    };
    const m = try W.Menu(.{ .items = &items }).mount(scope, cx);
    // 给 trigger 放一个有标签的按钮,让 story 可见可点
    try m.trigger.appendChild(a, try W.Button(.{ .label = "Actions ▾", .variant = .secondary }).mount(scope, cx));
    m.wrapper.meta.ownership.meta.test_id = "story.menu.trigger";
    try c.appendChild(a, m.wrapper);
    return c;
}
在 GitHub 查看源码examples/storybook/stories.zig:2335

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

zenit · 双授权开源项目可按 GPL-3.0-only 免费使用;闭源或商业产品需要商业授权。可联系作者:zongyi.xzy#gmail.com(# 换成 @)zenit 5f9add5+wip 2026-09-30