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>

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>

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>

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>

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

NameTypeDefaultDescription
checkedBooleanfalseWhether the element is checked
disabledBooleanfalseWhether the element is disabled
nameStringnilForm field name
idStringnilHTML id attribute
valueString1The current value

Accessibility

Implements the WAI-ARIA Switch pattern with proper roles, states, and keyboard navigation.

ARIA Attributes

  • aria-checked
  • aria-disabled
  • role="switch"

Keyboard Shortcuts

KeyDescription
EnterActivates the focused element
SpaceActivates the focused element

JavaScript

Stimulus Controller

ui--switch

Values

NameTypeDescription
checkedBooleanThe checked

Actions

toggle