SutraUI.Switch (Sutra UI v0.2.0)
View SourceA toggle control that allows the user to switch between checked and not checked.
Examples
# Basic switch
<.switch name="airplane_mode" />
# Switch with initial checked state
<.switch name="notifications" checked />
# Disabled switch
<.switch name="marketing_emails" disabled />
# Switch with phx-click for LiveView interactivity
<.switch name="dark_mode" phx-click="toggle_dark_mode" checked={@dark_mode} />
# Switch with phx-change inside a form
<.switch name="auto_save" phx-change="toggle_auto_save" checked={@auto_save} />
# Switch with accessibility attributes
<.switch
name="email_notifications"
id="email-notifications"
aria-label="Enable email notifications"
aria-describedby="email-help"
/>Accessibility
- Uses
<input type="checkbox">withrole="switch" - Supports
aria-labeloraria-labelledbyfor labeling - Supports
aria-describedbyfor helper text - Keyboard accessible (Space to toggle, Enter to submit forms)
- Communicates checked state via
aria-checked
Summary
Functions
Renders a switch component.
Functions
Renders a switch component.
Examples
<.switch name="airplane_mode" />
<.switch name="notifications" checked />
<.switch name="marketing_emails" disabled />Attributes
id(:string) - The id of the switch input. Defaults tonil.name(:string) (required) - The name of the switch input.value(:string) - The value to submit when checked. Defaults to"true".checked(:boolean) - Whether the switch is checked. Defaults tofalse.disabled(:boolean) - Whether the switch is disabled. Defaults tofalse.class(:string) - Additional CSS classes. Defaults tonil.- Global attributes are accepted. Additional HTML attributes including ARIA. Supports all globals plus:
["form", "required", "aria-label", "aria-labelledby", "aria-describedby", "aria-required", "aria-invalid", "phx-click", "phx-change"].