v0.1.0-alpha
官网Home
components/switch
操作与选择Actions · 03 / 64

Switch

ui.widgets.Switch

用于立即生效的开关型布尔设置。A toggle for settings that apply immediately.

BEHAVIOR CONTRACT

明确区分 off、on、disabled,并提供成套键盘与无障碍语义。Distinct off, on and disabled states with keyboard and accessibility semantics.

RECORDED INTERACTION

Harness 点击 Off 开关,轨道与状态文本一起切换。The harness flips the Off switch; track and status text update together.

examples/storybook/stories.zig:392
pub fn buildSwitch(scope: *ui.Scope, cx: *ui.Cx) anyerror!*ui.Node {
    const a = cx.allocator;
    const c = try col(cx, 12);

    const status = try ui.text(cx, "toggled → (none)", .{ .font_size = 13, .color = light.color.fg_secondary });
    status.meta.ownership.meta.test_id = "story.switch.status";
    try c.appendChild(a, status);
    const story = try cx.bindState(SwitchStory, .{ .status = status });

    const off_switch = try W.Switch(.{
        .label_text = "Off",
        .initial_checked = false,
        .on_change = ui.Cx.boolHandlerFrom(SwitchStory, story, SwitchStory.onChange),
    }).mount(scope, cx);
    off_switch.meta.ownership.meta.test_id = "story.switch.off";
    try c.appendChild(a, off_switch);
    try c.appendChild(a, try W.Switch(.{ .label_text = "On", .initial_checked = true }).mount(scope, cx));
    try c.appendChild(a, try W.Switch(.{ .label_text = "Disabled", .disabled = true }).mount(scope, cx));
    return c;
}
在 GitHub 查看源码View source on GitHubexamples/storybook/stories.zig:392

这是 Storybook 中该 story 的原样代码(zenit 5f9add5+wip 2026-09-30)。其中 W 即 ui.widgets,col / row / label 是 Storybook 内的布局小工具。The story's verbatim source from the Storybook (zenit 5f9add5+wip 2026-09-30). W is ui.widgets; col / row / label are small Storybook layout helpers.

zenit · 双授权Dual-licensed开源项目可按 GPL-3.0-only 免费使用;闭源或商业产品需要商业授权。Free for open-source projects under GPL-3.0-only; closed-source or commercial products need a commercial license.可联系作者:Contact the author: zongyi.xzy#gmail.com(# 换成 @) (replace # with @)zenit 5f9add5+wip 2026-09-30