Switch
Switch - Phlex implementation
Examples
Default
A control that allows the user to toggle between two states.
<div class="flex items-center space-x-2">
<%= render "ui/switch", id: "airplane-mode" %>
<%= render "ui/label", for: "airplane-mode" do %>Airplane Mode<% end %>
</div><div class="flex items-center space-x-2">
<%= render UI::Switch.new(id: "airplane-mode") %>
<%= render UI::Label.new(for: "airplane-mode") { "Airplane Mode" } %>
</div><div class="flex items-center space-x-2">
<%= render UI::SwitchComponent.new(id: "airplane-mode") %>
<%= render(UI::LabelComponent.new(for: "airplane-mode")) { "Airplane Mode" } %>
</div>Checked by Default
Switch that starts in the on state.
<div class="flex items-center space-x-2">
<%= render "ui/switch", id: "notifications", checked: true %>
<%= render "ui/label", for: "notifications" do %>Enable Notifications<% end %>
</div><div class="flex items-center space-x-2">
<%= render UI::Switch.new(id: "notifications", checked: true) %>
<%= render UI::Label.new(for: "notifications") { "Enable Notifications" } %>
</div><div class="flex items-center space-x-2">
<%= render UI::SwitchComponent.new(id: "notifications", checked: true) %>
<%= render(UI::LabelComponent.new(for: "notifications")) { "Enable Notifications" } %>
</div>With Description
Switch with additional descriptive text.
Receive emails about new products, features, and more.
<div class="flex items-start space-x-4">
<%= render "ui/switch", id: "marketing" %>
<div class="space-y-0.5">
<%= render "ui/label", for: "marketing" do %>Marketing emails<% end %>
<p class="text-sm text-muted-foreground">Receive emails about new products, features, and more.</p>
</div>
</div>Receive emails about new products, features, and more.
<div class="flex items-start space-x-4">
<%= render UI::Switch.new(id: "marketing") %>
<div class="space-y-0.5">
<%= render UI::Label.new(for: "marketing") { "Marketing emails" } %>
<p class="text-sm text-muted-foreground">Receive emails about new products, features, and more.</p>
</div>
</div>Receive emails about new products, features, and more.
<div class="flex items-start space-x-4">
<%= render UI::SwitchComponent.new(id: "marketing") %>
<div class="space-y-0.5">
<%= render(UI::LabelComponent.new(for: "marketing")) { "Marketing emails" } %>
<p class="text-sm text-muted-foreground">Receive emails about new products, features, and more.</p>
</div>
</div>Disabled
Switch in a disabled state.
<div class="flex items-center space-x-2">
<%= render "ui/switch", id: "disabled-switch", disabled: true %>
<%= render "ui/label", for: "disabled-switch" do %>Disabled<% end %>
</div><div class="flex items-center space-x-2">
<%= render UI::Switch.new(id: "disabled-switch", disabled: true) %>
<%= render UI::Label.new(for: "disabled-switch") { "Disabled" } %>
</div><div class="flex items-center space-x-2">
<%= render UI::SwitchComponent.new(id: "disabled-switch", disabled: true) %>
<%= render(UI::LabelComponent.new(for: "disabled-switch")) { "Disabled" } %>
</div>Features
- Keyboard navigation
- Custom styling with Tailwind classes
- Form integration
- Disabled state support
- ARIA attributes for accessibility
- Animation support
API Reference
Switch
A toggle control that allows the user to switch between checked and unchecked states.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| checked | Boolean | false | Whether the element is checked |
| disabled | Boolean | false | Whether the element is disabled |
| name | String | nil | Form field name |
| id | String | nil | HTML id attribute |
| value | String | 1 | The current value |
Accessibility
Adheres to theSwitch WAI-ARIA design pattern
Implements the WAI-ARIA Switch pattern with proper roles, states, and keyboard navigation.
ARIA Attributes
- aria-checked
- aria-disabled
- role="switch"
Keyboard Shortcuts
| Key | Description |
|---|---|
| Enter | Activates the focused element |
| Space | Activates the focused element |
JavaScript
Stimulus Controller
ui--switchValues
| Name | Type | Description |
|---|---|---|
| checked | Boolean | The checked |
Actions
toggle