4.3k

Switch

An on/off switch control. The label is the element's content, the model binds checked, and on-toggle dispatches its Msg — same contract as checkbox, rendered as a sliding thumb. In the Zig builder the widget kind is .switch_control.

switch
Switches rendered by the engine (light theme)
on, off, and disabled switches

Markup

<column gap="12">
  <switch checked="{airplane_mode}" on-toggle="toggle_airplane_mode">Airplane mode</switch>
  <switch checked="{notifications}" on-toggle="toggle_notifications">Notifications</switch>
  <switch checked="true" disabled="true">Managed setting</switch>
</column>

Zig builder

ui.column(.{ .gap = 12 }, .{
    ui.el(.switch_control, .{ .text = "Airplane mode", .checked = model.airplane_mode, .on_toggle = .toggle_airplane_mode }, .{}),
    ui.el(.switch_control, .{ .text = "Notifications", .checked = model.notifications, .on_toggle = .toggle_notifications }, .{}),
    ui.el(.switch_control, .{ .text = "Managed setting", .checked = true, .disabled = true }, .{}),
})

Attributes

AttributeDescription
textText value for text-bearing elements; a literal or one {binding}.
checkedChecked state for checkbox/toggle; true/false or a {binding}.
disabledDisables the control; true/false or a {binding}.
on-toggleDispatch a Msg on toggle: tag or tag:{payload}. Hit-target elements only (checkbox, toggle, toggle-button, switch, accordion, ...).