Radio button
Radio buttons let me choose one out of many options in a list.
The Radio button component allows users to choose one option from a group inside a fieldset. The full list of options is present on the page.
Details
- Extends: USWDS Radio button
- Honeycrisp: Form element atom
- Customization: Design tokens and Styles
- Modifier:
.cfa-legend
,.cfa-hint
,.cfa-checkbox
,.cfa-radio
The Radio button extends the USWDS Radio button. The visual appearance is modified using design tokens applied to the USWDS Radio button settings from the Honeycrisp Form element atom. Further customization is applied using the CSS modifier(s) .cfa-legend
, .cfa-hint
, .cfa-checkbox
, .cfa-radio
to add styles defined in a custom stylesheet.
Design tokens. Tokens define the name of basic system elements such as color, typography, or spacing. The values of tokens are relative to how the system defines them. This enables teams to alter the visual appearance of components yet remain within the system boundaries. Learn more about tokens on the Bixal's Design Tokens Guide.
Modifier. A modifier is a class name that applies a variant, type, or extended style customization to modify the component's visual appearance.
Examples
The HTML for the demonstration above is rendered using context passed to the component's Thymeleaf template fragment. Learn how to include component templates for Thymeleaf and Ruby in the source and usage section.
<div class="usa-radio cfa-radio">
<input type="radio" id="radio-6674ed712dd28" name="radio['d7340e86ca0ee']" value="option-a" class="usa-radio__input usa-radio__input--tile">
<label for="radio-6674ed712dd28" class="usa-radio__label">
<span>Option A</span>
</label>
</div>
<div class="usa-radio cfa-radio">
<input type="radio" id="radio-1ea11b5fc3402" name="radio['d7340e86ca0ee']" value="option-b" class="usa-radio__input usa-radio__input--tile">
<label for="radio-1ea11b5fc3402" class="usa-radio__label">
<span>Option B</span>
</label>
</div>
Context is the information necessary to configure and render a component template to HTML. It may include plain text strings, HTML, class names, IDs or other HTML attribute values. The context here is defined as JSON but the root attributes are translated to different variable syntaxes for Thymeleaf and Ruby templates. Learn how to pass these variables to each component template in the source and usage section.
{
"options": [
{
"modifier": "cfa-radio",
"id": "radio-6674ed712dd28",
"name": "radio['d7340e86ca0ee']",
"type": "radio",
"value": "option-a",
"label": "Option A",
"input": {
"modifier": "usa-radio__input--tile"
}
},
{
"modifier": "cfa-radio",
"id": "radio-1ea11b5fc3402",
"name": "radio['d7340e86ca0ee']",
"type": "radio",
"value": "option-b",
"label": "Option B",
"input": {
"modifier": "usa-radio__input--tile"
}
}
]
}
The HTML for the demonstration above is rendered using context passed to the component's Thymeleaf template fragment. Learn how to include component templates for Thymeleaf and Ruby in the source and usage section.
<div class="usa-radio cfa-radio">
<input type="radio" id="radio-81f1aacf2dd14" name="radio['8e942a629802f']" value="option-a" class="usa-radio__input usa-radio__input--tile">
<label for="radio-81f1aacf2dd14" class="usa-radio__label">
<span>Option A</span>
<span class="usa-radio__label-description">Option description</span>
</label>
</div>
<div class="usa-radio cfa-radio">
<input type="radio" id="radio-3245d47a40a68" name="radio['8e942a629802f']" value="option-b" class="usa-radio__input usa-radio__input--tile">
<label for="radio-3245d47a40a68" class="usa-radio__label">
<span>Option B</span>
<span class="usa-radio__label-description">Option description</span>
</label>
</div>
Context is the information necessary to configure and render a component template to HTML. It may include plain text strings, HTML, class names, IDs or other HTML attribute values. The context here is defined as JSON but the root attributes are translated to different variable syntaxes for Thymeleaf and Ruby templates. Learn how to pass these variables to each component template in the source and usage section.
{
"options": [
{
"modifier": "cfa-radio",
"id": "radio-81f1aacf2dd14",
"name": "radio['8e942a629802f']",
"type": "radio",
"value": "option-a",
"label": "Option A",
"description": "Option description",
"input": {
"modifier": "usa-radio__input--tile"
}
},
{
"modifier": "cfa-radio",
"id": "radio-3245d47a40a68",
"name": "radio['8e942a629802f']",
"type": "radio",
"value": "option-b",
"label": "Option B",
"description": "Option description",
"input": {
"modifier": "usa-radio__input--tile"
}
}
]
}
Guidance
Fieldsets. Form questions with radio button components always use the fieldset component to group available options with a visible legend
element.
Refer to additional guidance on the USWDS documentation site.
USWDS documentation siteAdditional references
Accessibility
Refer to additional accessibility guidance on the USWDS documentation site.
Checklist Key
Design library component
Source and usage
- Sass stylesheet:
./_cfa-input-select.scss
- Thymeleaf template fragment:
./cfa-input-select.th.html
- Embedded Ruby (ERB) partial template:
./_cfa-input-select.html.erb
Below is a demonstration of customizing the component theme settings. Refer to the theme and package-level settings documentation.
// Theme-level settings
@use 'cfa-uswds-theme' with (
// Global theme settings that affect the component, changing these will affect other components
$cfa-color-base-lightest: 'gray-warm-4', // Affects checkbox and radio hover state
$cfa-input-select-size: 3, // Affects checkbox and radio
$cfa-input-tile-border-radius: 0, // Affects checkbox and radio
$cfa-input-tile-border-width: 2px, // Affects checkbox and radio
// Component specific settings
$cfa-checkbox-border-radius: 0
);
// Package-level settings
@use 'cfa-core' with (
$cfa-input-select-margin: 1.5, // Affects checkbox and radio
$cfa-input-select-width: 2, // Affects checkbox and radio
$cfa-input-select-height: 2, // Affects checkbox and radio
$cfa-input-select-padding-x: 3, // Affects checkbox and radio
$cfa-input-select-padding-y: 2, // Affects checkbox and radio
$cfa-input-select-gap: 2 // Affects checkbox and radio
);
Refer to the usage documentation for additional USWDS theme settings.
@use "uswds-core" with ( /* additional USWDS theme settings */ );
This is the pre-rendered template fragment from the package. It is the same template used to render the demonstrations above. You may copy and paste from this example or use the template using the th:block th:replace
tag. See the example below.
<div th:fragment="inputSelect(options, required)" th:each="option: ${options}" th:class="'usa-' + ${option.type} + ' ' + ${option.modifier}">
<input th:class="'usa-' + ${option.type} + '__input ' + ${option.input.modifier}" th:attr="type=${option.type},id=${option.id},name=${option.name},aria-invalid=${option.ariaInvalid},aria-describedby=${option.ariaDescribedby},value=${option.value},checked=${option.checked},required=${required},data-js=${option.js},data-aria-controls=${option.dataAriaControls}" />
<label th:class="'usa-' + ${option.type} + '__label'" th:attr="for=${option.id}">
<span th:if="${option.label}" th:text="${option.label}">Label</span>
<span th:if="${option.description}" th:class="'usa-' + ${option.type} + '__label-description'" th:utext="${option.description}">Description</span>
</label>
</div>
Below is an example of how to use the fragment from the package directory using the th:block th:replace
inclusion tag. Replace the fragment parameters using your variables or context.
<th:block th:replace="~{packages/cfa-input-select/cfa-input-select.th :: inputSelect(${options}, ${required})}" />
This is the pre-rendered partial template from the package. You may copy and paste from this example or use the template directly from the path.
<% options.each do |option| %>
<div class="usa-<%= option['type'] %><% if option['modifier'] %> <%= option['modifier'] %><% end %>">
<input class="usa-<%= option['type'] %>__input<% if option['input']['modifier'] %> <%= option['input']['modifier'] %><% end %>"
<% if option['type'] %> type="<%= option['type'] %>"<% end %>
<% if option['id'] %> id="<%= option['id'] %>"<% end %>
<% if option['name'] %> name="<%= option['name'] %>"<% end %>
<% if option['ariaInvalid'] %> aria-invalid="<%= option['ariaInvalid'] %>"<% end %>
<% if option['ariaDescribedby'] %> aria-describedby="<%= option['ariaDescribedby'] %>"<% end %>
<% if option['value'] %> value="<%= option['value'] %>"<% end %>
<% if option['checked'] %> checked="<%= option['checked'] %>"<% end %>
<% if defined?(required) %>required<% end %> />
<label class="usa-<%= option['type'] %>__label"<% if option['id'] %> for="<%= option['id'] %>"<% end %>>
<% if option['label'] %><span><%= option['label'] %></span><% end %>
<% if option['description'] %><span class="usa-<%= option['type'] %>__label-description"><%= option['description'] %></span><% end %>
</label>
</div>
<% end %>
Below is an example of how to include the partial in a view from the package directory using the Ruby ERB
class. Replace the argument values using your variables or context. In a Rails environment, the render
method can be used instead with the same hash values.
<%= ERB.new(File.read('packages/cfa-input-select/_cfa-input-select.html.erb'), 0, 0, '@inputSelect').result_with_hash({required: required, options: options}) %>