Radio Button
Radio button groups for selecting one option from a collection of mutually exclusive choices.
Key Features
- 4 Variants - Radio, Card, Pill, Button (segmented control)
- Collection Support - Arrays, ActiveRecord, enums, hashes
- Form Builder - Full Rails form integration (
f.rui_radio_button) - 5 Sizes - xs, sm, base, lg, xl
- All Colors - Semantic + any Tailwind color
- Layouts - Vertical and horizontal arrangements
- Full Accessibility - ARIA attributes, keyboard navigation
- No JavaScript - Pure CSS with peer-checked styling
Basic Usage
Radio button group with a collection of options. Use the positional first argument for clean, Rails-like syntax.
Select the current status
<%# Recommended: Positional syntax %>
<%= rui_radio_button(:status,
collection: [["Draft", "draft"], ["Published", "published"], ["Archived", "archived"]],
label: "Post Status",
help_text: "Select the current status",
color: :primary
) %>
New in v0.15.0: The positional syntax rui_radio_button(:name, collection: ...) is now supported for cleaner standalone usage.
Old keyword syntax object: :post, method: :status still works for backward compatibility.
Colors
Supports all Tailwind colors via ColorBuilderHelper - semantic colors plus any Tailwind color.
Semantic Colors
<%= rui_radio_button(object: :demo, method: :opt, collection: options, color: :primary, selected: "a") %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, color: :success, selected: "a") %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, color: :danger, selected: "a") %>
Tailwind Colors
Use any Tailwind color directly - indigo, pink, violet, teal, and more!
<%= rui_radio_button(object: :tw, method: :opt, collection: options, color: :indigo, selected: "a") %>
<%= rui_radio_button(object: :tw, method: :opt, collection: options, color: :pink, selected: "a") %>
<%= rui_radio_button(object: :tw, method: :opt, collection: options, color: :violet, selected: "a") %>
Sizes
Five size options from extra small to extra large.
<%= rui_radio_button(object: :demo, method: :opt, collection: options, size: :xs) %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, size: :sm) %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, size: :base) %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, size: :lg) %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, size: :xl) %>
Card Variant
Bordered cards with full-width click targets. Perfect for pricing plans, subscription tiers, or feature selection.
<%= rui_radio_button(
object: :subscription,
method: :plan,
collection: [
["Basic", "basic", "For individuals getting started"],
["Pro", "pro", "For growing teams"],
["Enterprise", "enterprise", "Custom solutions for large orgs"]
],
variant: :card,
description_method: 2,
label: "Select Plan",
color: :primary,
selected: "pro"
) %>
Card Colors
Pill Variant
Compact, tag-like selections. Perfect for filters, categories, or quick selections.
<%= rui_radio_button(
object: :filter,
method: :period,
collection: [["Day", "day"], ["Week", "week"], ["Month", "month"], ["Year", "year"]],
variant: :pill,
layout: :horizontal,
color: :blue,
selected: "week"
) %>
Pill Colors
Layouts
Vertical (default) and horizontal arrangements for radio groups.
Vertical (Default)
Horizontal
<%# Vertical (default) %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, layout: :vertical) %>
<%# Horizontal %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, layout: :horizontal) %>
Descriptions
Add descriptions to collection items for additional context.
Standard processing time
Faster processing
Immediate attention required
<%= rui_radio_button(
object: :priority,
method: :level,
collection: [
["Low", "low", "Standard processing time"],
["Medium", "medium", "Faster processing"],
["High", "high", "Immediate attention required"]
],
description_method: 2,
label: "Priority Level",
color: :primary
) %>
States
Disabled and required states for radio button groups.
<%# Disabled %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, disabled: true, label: "Disabled") %>
<%# Required %>
<%= rui_radio_button(object: :demo, method: :opt, collection: options, required: true, label: "Required") %>
Form Builder Integration
Use with Rails form builder for automatic object binding and validation.
<%= form_with model: @post do |f| %>
<%= f.rui_radio_button(:status,
collection: Post.statuses,
label: "Post Status",
help_text: "Select the current status"
) %>
<%= f.rui_radio_button(:priority,
collection: [["Low", 1], ["Medium", 2], ["High", 3]],
variant: :button,
layout: :horizontal,
label: "Priority"
) %>
<%= f.rui_button %>
<% end %>
Note: When using f.rui_radio_button, the selected state is automatically detected from the form object's attribute value.
Rails Integration Comparison
| Rails Helper | rui_radio_button Equivalent |
|---|---|
f.radio_button |
f.rui_radio_button(:method, collection: items) |
f.collection_radio_buttons |
f.rui_radio_button(:method, collection: Model.all) |
Live Example
See the radio button component in action on the New Post page.
Accessibility
The Radio Button component follows WAI-ARIA best practices for accessible form controls.
ARIA Attributes
-
role="radiogroup"on the container (implicit with<fieldset>) -
role="radio"implicit on native<input type="radio"> -
aria-checkedstate managed automatically -
aria-describedbylinks to description text when provided
Keyboard Navigation
-
Tabto enter/exit the radio group -
Arrow Up/DownorArrow Left/Rightto navigate between options -
Spaceto select the focused option - Visible focus ring on the focused radio button
Semantic HTML
-
Native
<input type="radio">elements -
<fieldset>groups related radio buttons -
<legend>provides group label for the fieldset -
<label>elements properly associated viafor/id
Screen Reader Support
- Group label (legend) announced when entering the radio group
- Each option label announced with checked/unchecked state
- Position announced (e.g., "1 of 3")
- Description text read after label when provided
API Reference
Appearance
Visual styling options
| Parameter | Type | Default | Description |
|---|---|---|---|
| variant | Symbol |
:radio
|
Visual style
:radio
:card
:pill
:button
|
| color | Symbol |
:primary
|
Color theme - semantic or any Tailwind color |
| size | Symbol |
:base
|
Radio button size
:xs
:sm
:base
:lg
:xl
|
| layout | Symbol |
:vertical
|
Layout arrangement
:vertical
:horizontal
|
Labels & Text
Text and description options
| Parameter | Type | Default | Description |
|---|---|---|---|
| label | String | — | Legend text for the radio group |
| description | String | — | Description text for single radio (rarely used) |
| help_text | String | — | Help text displayed below group |
Collection Configuration
Options for processing collection items
| Parameter | Type | Default | Description |
|---|---|---|---|
| value_method | Symbol |
:id
|
Method to get value from collection items |
| text_method | Symbol |
:name
|
Method to get label text from collection items |
| description_method | Symbol/Integer | — | Method or array index for option description |
| selected | Any | — | Pre-selected value (overrides object value) |
States
State options
| Parameter | Type | Default | Description |
|---|---|---|---|
| required | Boolean |
false
|
Show required indicator (*) |
| disabled | Boolean |
false
|
Disable all radio buttons |
Form Builder
Options when used with Rails form builder (f.rui_radio_button)
| Parameter | Type | Default | Description |
|---|---|---|---|
| form | FormBuilder | — | Rails form builder instance (auto-set by f.rui_radio_button) |
| object | Object/Symbol | — | Object to bind radio button to |
| method | Symbol | — | Attribute name on the object (keyword alternative) |
| name | String | — | Input name attribute (keyword alternative) |
Slots
Content slots for customizing component parts
| Slot | Description |
|---|---|
| icon | Icon for button/pill/card variants via radio.with_icon(:name) |