noora
Sign in

Radio

Lets people pick exactly one option from a short list.

Default

Visibility
<.radio_group id="radio-default" name="radio-default" value="private" label="Visibility">
  <:radio value="private" label="Private" />
  <:radio value="public" label="Public" />
</.radio_group>

Horizontal

A row suits two or three short options.

Report frequency
<.radio_group
  id="radio-horizontal"
  name="radio-horizontal"
  value="week"
  label="Report frequency"
  orientation="horizontal"
>
  <:radio value="day" label="Daily" />
  <:radio value="week" label="Weekly" />
  <:radio value="month" label="Monthly" />
</.radio_group>

With description

A second line explains what the option changes.

Who can upload to the cache
<.radio_group
  id="radio-description"
  name="radio-description"
  value="members"
  label="Who can upload to the cache"
>
  <:radio
    value="members"
    label="Members and tokens"
    description="Everyone in the account can upload from their machine, and so can CI."
  />
  <:radio
    value="tokens"
    label="Tokens only"
    description="Only CI uploads, so every cached artifact comes from a trusted build."
  />
</.radio_group>

Disabled

Disable the whole group, or a single option that doesn't apply.

Disabled group
Disabled option
<.radio_group
  id="radio-disabled"
  name="radio-disabled"
  value="private"
  label="Disabled group"
  disabled
>
  <:radio value="private" label="Disabled selected" />
  <:radio value="public" label="Disabled" />
</.radio_group>
<.radio_group
  id="radio-disabled-option"
  name="radio-disabled-option"
  value="private"
  label="Disabled option"
>
  <:radio value="private" label="Private" />
  <:radio value="public" label="Public" disabled />
</.radio_group>

Single radios

Radios sharing a name behave as one group, for layouts the group doesn't cover.

<.radio id="radio-single-light" name="radio-single" value="light" label="Light" checked />
<.radio id="radio-single-dark" name="radio-single" value="dark" label="Dark" />
<.radio id="radio-single-system" name="radio-single" value="system" label="System" />

Guidelines

  • Use a radio group when people must see every option to choose well and only one can apply. For more than five or so options, use a select instead.
  • Give the group a label that names what is being chosen, such as Visibility. It renders as the group's legend, which assistive tech reads before each option.
  • Start with one option selected whenever there is a sensible default. A radio can't be cleared once chosen, so add a None option when making no choice is valid.
  • Write the options as parallel, short labels. Use description for the consequence of an option rather than restating its label.
  • Pass field to take the id, name, and value from a form field. The value submits with the form; react to a change with phx-change on the form.
  • Use radio on its own only when the options need a custom layout. Give every radio of the set the same name , and wrap them in a fieldset with a legend .
  • For options that can be combined, use checkboxes. For a single setting that turns on or off, use a checkbox or a toggle.

API reference

<.radio_group>

Attribute Type Default Description
disabled
boolean
false
Whether every radio in the group is disabled.
field
Phoenix.HTML.FormField
nil
A Phoenix form field.
id
string
nil
The id of the group.
label
string
nil
Names the group, as its legend.
name
string
–
The name the selected value is submitted under.
orientation
string
"vertical"
Whether the radios stack or sit in a row. "vertical""horizontal"
value
any
–
The value of the selected radio.

Also accepts global attributes.

Slot Description
:radio Required
A radio of the group. Attributes: valuelabeldescriptiondisabled

<.radio>

Attribute Type Default Description
checked
boolean
false
Whether the radio is selected.
description
string
nil
An optional description.
disabled
boolean
false
Whether the radio is disabled.
id
string
nil
The id of the radio input.
label Required
string
–
The label of the radio.
name
string
nil
The name shared by the radios of one group.
tabindex
integer
nil
Tabindex to add to the radio input.
value Required
any
–
The value submitted when the radio is selected.

Also accepts global attributes, including formrequired.