Toggle switch for binary settings. Mirrors Fluxon's switch/1 surface.
<.switch field={@form[:notifications]} label="Enable notifications" />
<.switch name="dark" checked={@dark} label="Dark mode" size="sm" color="accent" />A hidden input always submits unchecked_value so forms receive a param even
when the switch is off.
The toggle is Zag-driven (a @zag-js/switch state machine, loaded on
demand): the hook root carries data-zag plus data-scope="switch" /
data-part anatomy, and Zag owns checked state, keyboard, and ARIA while
the native inputs stay the form surface. Styling stays lui-* tokens.
Two modes:
- client (default) — Zag owns the value from the initial
checked. - server-driven (
controlled) — the server value (checked) is truth; toggles flow out throughon_change, patches flow in.
Summary
Functions
Attributes
id(:any) - Element id; derived from field when omitted. Defaults tonil.name(:string) - Form input name; derived from field when omitted. Defaults tonil.value(:any) - Current value used to derive checked state. Defaults tonil.checked(:boolean) - Force on/off; else compares value to checked_value. Defaults tonil.checked_value(:any) - Value submitted when the switch is on. Defaults to"true".unchecked_value(:any) - Hidden input value submitted when off. Defaults to"false".label(:string) - Primary label text beside the switch. Defaults tonil.sublabel(:string) - Secondary label line under the primary label. Defaults tonil.description(:string) - Helper text under the label stack. Defaults tonil.errors(:list) - Validation messages; derived from field when used. Defaults to[].size(:string) - Track and thumb size. Defaults to"md". Must be one of"sm","md", or"lg".color(:string) - On-state track color token. Defaults to"accent".class(:any) - Extra classes merged onto the root element. Defaults tonil.field(Phoenix.HTML.FormField) - Form field; derives id, name, value, and errors. Defaults tonil.disabled(:boolean) - Render disabled and non-interactive. Defaults tofalse.controlled(:boolean) - Server-driven checked state:checkedis truth, patches flow into the machine. Defaults tofalse.on_change(:string) - Server event pushed on toggle (lantern:switch:set-checkedreplies). Defaults tonil.on_change_client(:string) - Bubbling DOM CustomEvent dispatched on toggle. Defaults tonil.- Global attributes are accepted. Arbitrary HTML/
phx-*attributes passed through. Supports all globals plus:["form", "phx-change", "phx-target", "phx-click"].