Switch
NewOverview
An on/off control with labels, descriptions, semantic colors, size options, and Phoenix form integration.
Receive updates when someone replies to your topics.
Receive a summary of forum activity each Monday.
<.form for={@form}>
<div class="grid gap-4">
<Switch.switch
field={@form[:notifications]}
label="Notifications"
sublabel="Recommended"
description="Receive updates when someone replies to your topics."
/>
<Switch.switch
field={@form[:weekly_digest]}
label="Weekly digest"
description="Receive a summary of forum activity each Monday."
/>
</div>
</.form>
Sizes
Choose a size
that fits the layout, using larger switches on touch-oriented surfaces.
<.form for={@form}>
<div class="grid gap-4">
<Switch.switch field={@form[:compact_notifications]} label="Small" size={:sm} />
<Switch.switch field={@form[:notifications]} label="Medium" />
<Switch.switch field={@form[:touch_notifications]} label="Large" size={:lg} />
</div>
</.form>
Colors
Choose a semantic color for the enabled state.
<.form for={@form}>
<div class="grid gap-4">
<Switch.switch field={@form[:primary_notifications]} label="Primary" />
<Switch.switch field={@form[:information_notifications]} label="Information" color={:info} />
<Switch.switch field={@form[:success_notifications]} label="Success" color={:success} />
<Switch.switch field={@form[:warning_notifications]} label="Warning" color={:warning} />
<Switch.switch field={@form[:danger_notifications]} label="Danger" color={:danger} />
</div>
</.form>
Disabled
Switches with disabled
set and their
hidden input elements
are omitted from
form
submissions.
This notification option is unavailable on the current plan.
<.form for={@form}>
<Switch.switch
field={@form[:premium_notifications]}
label="Premium notifications"
sublabel="Upgrade required"
description="This notification option is unavailable on the current plan."
disabled
/>
</.form>
Errors
When bound with field, validation errors render beneath the switch.
<.form for={@form}>
<Switch.switch field={@form[:notifications]} label="Required notifications" />
</.form>
Add this module
mix dizzy.add switch
Component API
Switch.switch
Attributes
| Name | Type | Default | Values |
|---|---|---|---|
checked |
:boolean
| false | โ |
checked_value |
:string
| "true" | โ |
class |
:any
| nil | โ |
color |
:atom
| :primary | :primary, :info, :success, :warning, :danger |
description |
:string
| nil | โ |
disabled |
:boolean
| false | โ |
errors |
:list
| [] | โ |
field |
{:struct, Phoenix.HTML.FormField}
| nil | โ |
id |
:string
| nil | โ |
label |
:string
| nil | โ |
name |
:string
| nil | โ |
required |
:boolean
| false | โ |
rest |
:global
| nil | โ |
size |
:atom
| :md | :sm, :md, :lg |
sublabel |
:string
| nil | โ |
unchecked_value |
:string
| "false" | โ |