Radio

A native radio with Harmo's appearance, the sibling of Checkbox. For a few short options side by side, use a segmented control instead.

Installation

bun add @harmo-ui/css @harmo-ui/react

Usage

import { Field, Radio, RadioGroup } from "@harmo-ui/react";

<RadioGroup label="Export format" name="export-format" defaultValue="csv">
  <Field orientation="horizontal" label="CSV">
    <Radio value="csv" />
  </Field>
  <Field orientation="horizontal" label="Excel workbook">
    <Radio value="xlsx" />
  </Field>
</RadioGroup>

Examples

This site is using your theme

Group

Radios that share a name form one group: Tab reaches the chosen one and the Arrow keys move the choice. A Field with role="radiogroup" names the group, with one horizontal Field per option.

Export format
<RadioGroup label="Export format" name="export-format" defaultValue="csv">
  <Field orientation="horizontal" label="CSV">
    <Radio value="csv" />
  </Field>
  <Field orientation="horizontal" label="Excel workbook">
    <Radio value="xlsx" />
  </Field>
  <Field orientation="horizontal" label="PDF report">
    <Radio value="pdf" />
  </Field>
</RadioGroup>

With descriptions

.hfield-content holds an option's label and description, connected to the radio with aria-describedby.

Delivery

Starts delivering as soon as you publish.

Pick a date and time in your account's time zone.

<RadioGroup label="Delivery" name="delivery" defaultValue="now">
  <Field
    orientation="horizontal"
    label="Send now"
    description="Starts delivering as soon as you publish."
  >
    <Radio value="now" />
  </Field>
  <Field
    orientation="horizontal"
    label="Schedule"
    description="Pick a date and time in your account's time zone."
  >
    <Radio value="scheduled" />
  </Field>
</RadioGroup>

Disabled

disabled dims an option, and its label turns gray, whether or not it's chosen.

Template category
Data region, set by your administrator
<>
  <RadioGroup
    label="Template category"
    name="category"
    defaultValue="marketing"
  >
    <Field orientation="horizontal" label="Marketing">
      <Radio value="marketing" />
    </Field>
    <Field orientation="horizontal" label="Utility">
      <Radio value="utility" />
    </Field>
    <Field orientation="horizontal" label="Authentication">
      <Radio value="authentication" disabled />
    </Field>
  </RadioGroup>
  <RadioGroup
    label="Data region, set by your administrator"
    name="region"
    defaultValue="eu"
    disabled
  >
    <Field orientation="horizontal" label="European Union">
      <Radio value="eu" />
    </Field>
    <Field orientation="horizontal" label="United States">
      <Radio value="us" />
    </Field>
  </RadioGroup>
</>

Required

required on the radios asks for an answer before the form submits. It changes nothing visible; mark the question as required in its wording.

Why are you closing your account?
<RadioGroup
  label="Why are you closing your account?"
  name="reason"
  required
>
  <Field orientation="horizontal" label="It costs too much">
    <Radio value="cost" />
  </Field>
  <Field orientation="horizontal" label="It's missing features I need">
    <Radio value="features" />
  </Field>
  <Field orientation="horizontal" label="Something else">
    <Radio value="other" />
  </Field>
</RadioGroup>

API reference

React

<RadioGroup> renders the group: a Field with role="radiogroup", named by its label, that gives its radios one name. <Radio> renders the <input type="radio">; put each in a horizontal Field for its label and description. Other props are the group <div>'s, or the input's.

PropEffect
label, optional, description, errorAs for a Field
nameThe name its radios submit under; generated when left out
value or defaultValueThe chosen radio's value; a controlled value is null while none is chosen
onValueChangeCalled with the newly chosen value
required, disabledSet on every radio in it

HTML

An <input class="hradio" type="radio">, with a <label for> per radio and the same name on every radio in a group. A group is a Field with role="radiogroup" and aria-labelledby, holding one horizontal Field per option. Load radio.css; field.css for the group and labels. It needs no script.

HookEffect
checkedChosen
disabledDimmed, not-allowed cursor; its Field label turns gray
requiredOn the radios, when an answer is required

See Radio's full contract.

Accessibility

Radios that share a name form one group: Tab reaches the chosen one, and the Arrow keys move the choice. The role="radiogroup" Field names the group.