Skip to content

Switch

A toggle switch that includes a real hidden <input type="checkbox">, so it submits with a Rails form like any other field.

html
<label
  data-controller="zag-switch"
  data-part="root"
  data-zag-switch-name-value="notifications"
  class="flex cursor-pointer items-center gap-3"
>
  <input data-part="hidden-input" class="sr-only" />
  <span
    data-part="control"
    class="bg-muted data-[state=checked]:bg-primary relative inline-flex h-6 w-11 shrink-0 items-center rounded-full transition-colors"
  >
    <span
      data-part="thumb"
      class="bg-popover inline-block h-5 w-5 translate-x-0.5 rounded-full shadow transition-transform data-[state=checked]:translate-x-5"
    ></span>
  </span>
  <span data-part="label" class="text-sm font-medium">Email notifications</span>
</label>

Parts

root, control, thumb, label, hidden-input.

Values

AttributeTypeDefaultDescription
data-zag-switch-checked-valueBooleanfalseChecked by default.
data-zag-switch-name-valueString""name of the hidden input (for forms).
data-zag-switch-value-valueString"on"Submitted value when checked.
data-zag-switch-disabled-valueBooleanfalseDisable the switch.
data-zag-switch-required-valueBooleanfalseMark the field required.
data-zag-switch-invalid-valueBooleanfalseMark the field invalid.
data-zag-switch-read-only-valueBooleanfalseRead-only.

Events

Emits zag-switch:changedetail is { checked }.

Programmatic control

checked (getter), check(), uncheck(), toggle().