# Radio (/components/radio)

<ReferenceLinks
  links={[
    { label: "Source", href: meta.links.source, iconName: "code" },
    { label: "Stories", href: meta.links.stories },
  ]}
/>

<Stage code={heroCode}>
  <HeroDemo />
</Stage>

## Installation [#installation]

1. Install the package.

```sh
npm install @hashintel/ds-components
```

2. Import the component.

```tsx
import { useState } from "react";

import { Radio } from "@hashintel/ds-components";

export const VisibilityPicker = () => {
  const [visibility, setVisibility] = useState("workspace");

  return (
    <>
      <Radio
        label="Visible to workspace"
        name="visibility"
        value={visibility === "workspace"}
        onChange={() => setVisibility("workspace")}
      />
      <Radio
        label="Only me"
        name="visibility"
        value={visibility === "private"}
        onChange={() => setVisibility("private")}
      />
    </>
  );
};
```

### When to use [#when-to-use]

<DoList
  items={[
    "Use Radio to build a bespoke choose-one layout — options in table rows, cards, or a grid — that RadioGroup's layouts can't express.",
  ]}
/>

### When not to use [#when-not-to-use]

<DontList
  items={[
    "Don't hand-wire a standard stack of options — use RadioGroup, which manages the shared value, group name, and option layout.",
    "Don't use a lone radio for an on/off choice — once selected it can't be unselected; use Checkbox or Toggle.",
    "Don't present long option lists as radios — beyond roughly 5 options use Select.",
    "Don't use radios to switch views or filters that apply instantly — use SegmentedControl.",
  ]}
/>

## Examples [#examples]

### Basic [#basic]

Radio is always controlled: `value` holds whether this option is selected and
`onChange` reports selection (see Installation for the group wiring). The
default is `md`-sized and `neutral`.

<Stage code={basicCode}>
  <BasicDemo />
</Stage>

### Disabled [#disabled]

A disabled radio keeps its selected state visible but can't be changed.
Disable individual options the user's role or plan rules out, and explain the
constraint nearby.

<Stage code={"<Radio disabled value label=\"Free plan (current)\" onChange={() => {}} />\n<Radio disabled value={false} label=\"Enterprise plan\" onChange={() => {}} />"}>
  <Radio disabled value label="Free plan (current)" onChange={noop} />
  <Radio disabled value={false} label="Enterprise plan" onChange={noop} />
</Stage>

### Invalid [#invalid]

Set `invalid` on the options of a group that failed validation — typically a
required choice left unmade. Pair it with visible error text — the red ring
alone doesn't say what's wrong.

<Stage code={"<Radio invalid value={false} label=\"US East\" onChange={() => {}} />\n<Radio invalid value={false} label=\"EU West\" onChange={() => {}} />"}>
  <DemoState>
    <Radio invalid value={false} label="US East" onChange={noop} />
  </DemoState>
  <DemoState>
    <Radio invalid value={false} label="EU West" onChange={noop} />
  </DemoState>
</Stage>

### Tones [#tones]

`tone` colors the selected circle: `neutral` (default) for everyday forms,
`brand` for primary flows such as onboarding, `success` for
confirmation-flavored choices. Unselected radios look the same in every tone.

<Stage code={"<Radio value tone=\"neutral\" label=\"Neutral\" onChange={() => {}} />\n<Radio value tone=\"brand\" label=\"Brand\" onChange={() => {}} />\n<Radio value tone=\"success\" label=\"Success\" onChange={() => {}} />"}>
  <Radio value tone="neutral" label="Neutral" onChange={noop} />
  <Radio value tone="brand" label="Brand" onChange={noop} />
  <Radio value tone="success" label="Success" onChange={noop} />
</Stage>

### Sizes [#sizes]

`md` is the default. Match the surrounding text — `xxs`–`sm` for dense chrome such as table rows and filter
panels, `lg` for prominent choices.

<Stage code={"<Radio value size=\"xxs\" label=\"xxs\" onChange={() => {}} />\n<Radio value size=\"xs\" label=\"xs\" onChange={() => {}} />\n<Radio value size=\"sm\" label=\"sm\" onChange={() => {}} />\n<Radio value size=\"md\" label=\"md\" onChange={() => {}} />\n<Radio value size=\"lg\" label=\"lg\" onChange={() => {}} />"}>
  <Radio value size="xxs" label="xxs" onChange={noop} />
  <Radio value size="xs" label="xs" onChange={noop} />
  <Radio value size="sm" label="sm" onChange={noop} />
  <Radio value size="md" label="md" onChange={noop} />
  <Radio value size="lg" label="lg" onChange={noop} />
</Stage>

### Label placement [#label-placement]

`labelPlacement` puts the label `right` (default) or `left` of the circle.
With `left`, the root also spreads label and circle to opposite edges once you
give it a width via `className`. 

<Stage code={"<Radio\n  value\n  labelPlacement=\"right\"\n  label=\"Label on the right\"\n  onChange={() => {}}\n/>\n<Radio\n  value\n  labelPlacement=\"left\"\n  label=\"Label on the left\"\n  onChange={() => {}}\n/>"}>
  <Radio
    value
    labelPlacement="right"
    label="Label on the right"
    onChange={noop}
  />
  <Radio
    value
    labelPlacement="left"
    label="Label on the left"
    onChange={noop}
  />
</Stage>

### Label alignment [#label-alignment]

`labelAlign` sets the circle against a label that wraps onto several lines:
`top` (default) pins it to the first line, `center` centers it against the
whole label.

<Stage code={"<div style={{ maxWidth: 260 }}>\n  <Radio\n    value\n    labelAlign=\"top\"\n    label=\"Keep both versions and mark the conflicting entities for manual review\"\n    onChange={() => {}}\n  />\n</div>\n<div style={{ maxWidth: 260 }}>\n  <Radio\n    value\n    labelAlign=\"center\"\n    label=\"Keep both versions and mark the conflicting entities for manual review\"\n    onChange={() => {}}\n  />\n</div>"}>
  <div style={{ maxWidth: 260 }}>
    <Radio
      value
      labelAlign="top"
      label="Keep both versions and mark the conflicting entities for manual review"
      onChange={noop}
    />
  </div>
  <div style={{ maxWidth: 260 }}>
    <Radio
      value
      labelAlign="center"
      label="Keep both versions and mark the conflicting entities for manual review"
      onChange={noop}
    />
  </div>
</Stage>

### Custom groups [#custom-groups]

Give every option the same `name` and derive each `value` from one piece of
state: the browser then enforces single selection and the arrow keys move both
focus and selection. Prefer RadioGroup unless the layout demands this.

<Stage code={mergeStrategyCode}>
  <MergeStrategyDemo />
</Stage>

### In forms [#in-forms]

The selected radio submits its `htmlValue` (default `"on"`) under `name`;
nothing submits while the group is unselected. `required` blocks native
submission until one option in the `name` group is chosen.

```tsx
import { useState } from "react";

import { Button, Radio } from "@hashintel/ds-components";

export const ExportScopeForm = () => {
  const [scope, setScope] = useState<string | null>(null);

  return (
    <form action="/api/exports" method="post">
      <Radio
        label="Current view"
        name="scope"
        htmlValue="view"
        required
        value={scope === "view"}
        onChange={() => setScope("view")}
      />
      <Radio
        label="Entire workspace"
        name="scope"
        htmlValue="workspace"
        required
        value={scope === "workspace"}
        onChange={() => setScope("workspace")}
      />
      <Button type="submit" tone="brand">
        Start export
      </Button>
    </form>
  );
};
```

## Behavior [#behavior]

- RadioGroup renders as one `<label>` wrapping a
  hidden native radio, so clicking anywhere on it selects. Don't nest it inside
  another label or interactive element.
- Selection is one-way from the user's side: clicking a selected radio fires
  nothing, and `onChange` only ever reports `true`. Clearing or switching
  happens through your state, by selecting a different option.
- Radios sharing a `name` get native arrow-key behavior, but each one stays
  controlled.
- Long labels wrap rather than truncate by default

## Content [#content]

- Name the option, not the action: “Entire workspace”, never “Select entire
  workspace”. Sentence case, no terminal punctuation.
- Phrase every option in a set with parallel structure — all nouns or all verb
  phrases — so the differences stand out.
- Keep labels under roughly 5 words and one line; move caveats into nearby
  helper text.
- For optional choices add a “None” option — a group can't be
  returned to unselected once the user picks.

## Accessibility [#accessibility]

Radio renders a native
[`<input type="radio">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio).
Grouped under a shared `name`, and implements the
[WAI-ARIA radio group pattern](https://www.w3.org/WAI/ARIA/apg/patterns/radio/)'s
interaction model.

### What Radio provides [#what-radio-provides]

- A visually hidden native `<input type="radio">` inside a `<label>`.
- With a shared `name`, the native inputs handle Tab and arrow keys.

### What you must do [#what-you-must-do]

- Always provide `label` — without it the input has no accessible name. When
  the visible label lives elsewhere, pass `htmlForId` and point that label's
  `htmlFor` at it.
- Name the group as a whole: wrap custom groups in `role="radiogroup"` with an
  `aria-label`, or a `<fieldset>` with a `<legend>`.
- `invalid` is visual only — put error text next to the group, since nothing is
  announced otherwise.
- Use a shared `name` across the options so the browser groups the inputs for
  arrow-key navigation and form submission.

## API reference [#api-reference]

<PropsTable
  of={Radio}
  defaults={{
    size: '"md"',
    tone: '"neutral"',
    labelPlacement: '"right"',
    labelAlign: '"top"',
    htmlValue: '"on"',
  }}
/>
