Toggle

Toggle - Phlex implementation

Examples

Default

A two-state button that can be either on or off.

<%= render "ui/toggle", variant: "outline" do %>B<% end %>

Outline

Toggle with outline variant.

<%= render "ui/toggle", variant: "outline" do %>I<% end %>

With Text

Toggle with text content.

<%= render "ui/toggle", variant: "outline" do %>
  Italic
<% end %>

Disabled

A disabled toggle.

<%= render "ui/toggle", variant: "outline", disabled: true do %>I<% end %>

Small

A small toggle.

<%= render "ui/toggle", variant: "outline", size: "sm" do %>
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 6h16"/><path d="M4 12h16"/><path d="M4 18h16"/></svg>
<% end %>

Large

A large toggle.

<%= render "ui/toggle", variant: "outline", size: "lg" do %>
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 6h16"/><path d="M4 12h16"/><path d="M4 18h16"/></svg>
<% end %>

Features

  • Custom styling with Tailwind classes
  • Disabled state support
  • ARIA attributes for accessibility

API Reference

Toggle

A two-state button that can be either on or off.

Parameters

NameTypeDefaultDescription
variantStringdefaultVisual style variant
sizeStringdefaultSize of the element
typeStringbuttonThe type
pressedBooleanfalseThe pressed
disabledBooleanfalseWhether the element is disabled

Group

A container for a set of toggle items that can be toggled on or off.

Parameters

NameTypeDefaultDescription
typeStringsingleThe type
variantStringdefaultVisual style variant
sizeStringdefaultSize of the element
valueStringnilThe current value
spacingInteger0The spacing
orientationStringnilOrientation (horizontal or vertical)

Accessibility

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

ARIA Attributes

  • aria-pressed

JavaScript

Stimulus Controller

ui--toggle

Values

NameTypeDescription
pressedBooleanThe pressed

Actions

toggle