---
title: "Switch コンポーネント — zenit Zig UI"
description: "即時に反映される設定のためのトグル。off・on・disabled を明確に区別し、キーボードとアクセシビリティのセマンティクスを備えます。"
url: https://zenit.z.express/ja/components/switch
language: ja
alternate_en: https://zenit.z.express/components/switch.md
alternate_zh: https://zenit.z.express/zh/components/switch.md
alternate_es: https://zenit.z.express/es/components/switch.md
alternate_ko: https://zenit.z.express/ko/components/switch.md
alternate_fr: https://zenit.z.express/fr/components/switch.md
alternate_de: https://zenit.z.express/de/components/switch.md
project: zenit v0.1.0-alpha (Zig 0.15.2, macOS)
source: https://github.com/version-next/zenit
---

# Switch

`ui.widgets.Switch`

即時に反映される設定のためのトグル。

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

**BEHAVIOR CONTRACT**

off・on・disabled を明確に区別し、キーボードとアクセシビリティのセマンティクスを備えます。

**RECORDED INTERACTION**

harness が Off のスイッチを切り替え、トラックと状態表示が同時に更新されます。

`examples/storybook/stories.zig:392`

```zig
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 でソースを見るexamples/storybook/stories.zig:392](https://github.com/version-next/zenit/blob/HEAD/examples/storybook/stories.zig#L392)

Storybook にあるこの story のコードそのままです（zenit 5f9add5+wip 2026-09-30）。`W` は `ui.widgets` のことで、`col` / `row` / `label` は Storybook 内の小さなレイアウトヘルパーです。

### SwitchProps

`ui.widgets.Switch`

[src/ui/components/checkbox/mod.zig:679](https://github.com/version-next/zenit/blob/HEAD/src/ui/components/checkbox/mod.zig#L679)

```zig
fn Switch(props: SwitchProps) SwitchBuilder
```

| フィールド | 型 | デフォルト値 | 説明 |
| --- | --- | --- | --- |
| `initial_checked` | `bool` | `false` | 初始开关态。\*\*只在 mount 时读取一次\*\*（语义同 CheckboxProps.initial\_checked）。 |
| `label_text` | `?[]const u8` | `null` | — |
| `disabled` | `bool` | `false` | — |
| `on_change` | `?core.HandlerRef` | `null` | — |
| `state_id` | `?u64` | `null` | — |

### \*Node

`ui.widgets.Switch`

mount は `*Node` を直接返します。親ノードに append してください。
