# Radio Group

Coordinate a roving native radio group. (Custom element.)

Reference: https://timeless.build/docs/components/radio-group/

> **Choosing between components.** One component, two levels of behavior. `<ui-radio-group>` adds roving focus and a single change event over the group; the plain `<fieldset class="ui-choice-group">` below it is the same markup with no custom element, and native radios already give it arrow-key selection and one-of-many semantics. The element is an addition, not a replacement, so it is never a choice between a CSS version and a JavaScript version.

## Markup

```html
<ui-radio-group value="system" aria-labelledby="theme-label">
  <span id="theme-label">Theme</span>
  <label class="ui-choice">
  <input class="ui-radio" type="radio" name="theme" value="system" checked>
  <span data-ui-part="body">System</span>
</label>
  <label class="ui-choice">
  <input class="ui-radio" type="radio" name="theme" value="light">
  <span data-ui-part="body">Light</span>
</label>
  <label class="ui-choice">
  <input class="ui-radio" type="radio" name="theme" value="dark">
  <span data-ui-part="body">Dark</span>
</label>
</ui-radio-group>
<fieldset class="ui-choice-group">
  <legend class="ui-label">Deploy target</legend>
  
  <label class="ui-choice" for="deploy-target-preview">
    <input class="ui-radio" id="deploy-target-preview" name="deploy-target" type="radio" value="preview" checked>
    <span data-ui-part="body">
      <span data-ui-part="title">Preview</span>
      
    </span>
  </label>
  <label class="ui-choice" for="deploy-target-production">
    <input class="ui-radio" id="deploy-target-production" name="deploy-target" type="radio" value="production">
    <span data-ui-part="body">
      <span data-ui-part="title">Production</span>
      
    </span>
  </label>
  
</fieldset>
```

## Install

```js
import '@timelessui/components/css/tokens.css'
import '@timelessui/components/css/core/choice-groups.css'
import '@timelessui/components/css/core/forms.css'
import '@timelessui/components/css/themes/atmosphere/tokens.css'
import '@timelessui/components/css/themes/atmosphere/forms.css'
import '@timelessui/components/css/themes/atmosphere/choice-groups.css'
import '@timelessui/components/register/ui-radio-group'
```

A `register/` import is a side effect: it defines the element as the module evaluates, so it
has to run in code the browser loads. To register explicitly instead — to control the timing,
or to define into another window — call the function from the matching `define/` entry point,
which registers nothing on its own:

```js
import { defineRadioGroupElement } from '@timelessui/components/define/ui-radio-group'
defineRadioGroupElement()
```

## Anatomy

Parts are authored in your own markup and identified by the selector below. Required parts
must be present for the component to work. Private `data-ui-internal-*` hooks are written by
the runtime and must never be authored.

**<ui-radio-group>**

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `choice` | Yes | `input[type='radio']` | One native radio input, sharing a `name` with the rest. Native form submission and reset keep working. |

**ui-choice-group**

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `description` | No | `[data-ui-part~='description']` | Group-level help text under the legend. |
| `error` | No | `[data-ui-part~='error']` | Group-level validation message. |

**ui-choice**

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `body` | No | `[data-ui-part~='body']` | Wrapper for the title and description beside the control. |
| `title` | No | `[data-ui-part~='title']` | The choice label text. |
| `description` | No | `[data-ui-part~='description']` | Supporting copy under the choice label. |

## Attributes

Every value below is implemented by the stylesheets this component ships. Boolean attributes
are presence-based: author the attribute with no value, or omit it.

**<ui-radio-group>**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `orientation` | `vertical` · `horizontal` | `vertical` | Layout and arrow-key axis. |
| `value` | any `string` | — | The radio checked on load and after a form reset. Match one input’s `value`. Assign the `value` property for live changes. |

**ui-choice-group**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `data-ui-orientation` | `vertical` · `horizontal` | `vertical` | Layout direction of the choices. |
| `data-ui-density` | `compact` · `normal` · `spacious` | `normal` | Gap between choices. |

**ui-choice**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `data-ui-density` | `compact` · `normal` · `spacious` | `normal` | Gap between the control and its label. |

## Element API

Attributes above are the authoring surface. These are the DOM properties and events the
registered element adds once it upgrades.

### `<ui-radio-group>`

| Property | Type | Notes |
| --- | --- | --- |
| `orientation` | `'vertical' | 'horizontal'` | Reflects the `orientation` attribute |
| `defaultValue` | `string` | Reflects the `value` attribute |
| `value` | `string` | Live value. Assigning it does not rewrite the authored default and does not dispatch transition events. |

| Event | Detail | Description |
| --- | --- | --- |
| `ui-before-change` | `CustomEvent<RadioGroupChangeDetail>` | Cancelable proposal dispatched before the checked radio changes. Call `preventDefault()` to reject the transition and keep the current value. |
| `ui-change` | `CustomEvent<RadioGroupChangeDetail>` | Dispatched after the checked radio has changed. Bubbles and is composed. |

## State

Native attributes, ARIA, and platform pseudo-classes are authoritative. Style state through
the selectors below rather than adding your own state classes.

| State | Source | How to express it |
| --- | --- | --- |
| `invalid` | aria | Set `aria-invalid="true"` on the `<fieldset>` and describe the error. |
| `invalid` | native | Native `:invalid`, or `aria-invalid="true"`. |
| `disabled` | native | Native `disabled`. |

## Styling

Root identity: `ui-radio-group`, `ui-choice-group`, `ui-choice`, `ui-radio`.
Required stylesheets: `tokens.css`, `core/choice-groups.css`, `core/forms.css`, `themes/atmosphere/tokens.css`, `themes/atmosphere/forms.css`, `themes/atmosphere/choice-groups.css`.

This component adds no custom properties of its own. Restyle it through the design tokens at https://timeless.build/docs/styling/theming/ or your own CSS.

Design tokens this component reads (26), global and set at the theme level: `--ui-accent`, `--ui-bg-control`, `--ui-bg-control-muted`, `--ui-bg-surface`, `--ui-bg-surface-subtle`, `--ui-danger`, `--ui-danger-soft`, `--ui-disabled-opacity`, `--ui-duration-fast`, `--ui-ease-standard`, `--ui-fg`, `--ui-fg-muted`, `--ui-fg-subtle`, `--ui-focus`, `--ui-line`, `--ui-line-strong`, `--ui-radius-md`, `--ui-radius-pill`, `--ui-radius-sm`, `--ui-shadow-control`, `--ui-shadow-inset`, `--ui-space-1`, `--ui-space-2`, `--ui-space-3`, `--ui-space-4`, `--ui-space-5`.

## Accessibility

Keep the native elements, roles, and relationships shown in the markup. Timeless adds only the
state and keyboard coordination the platform does not already provide, and it never supplies
your accessible names — those depend on your content.

Follows the [Radio Group pattern](https://www.w3.org/WAI/ARIA/apg/patterns/radio/) from the ARIA Authoring Practices Guide.

| Key | Action |
| --- | --- |
| `Arrow keys` | Move focus to the previous or next radio, following the orientation and skipping disabled ones. |
| `Home / End` | Move focus to the first or last enabled radio. |

Native radio semantics do the rest: one tab stop per group, checked state, form submission, and reset. Timeless adds roving focus and change events without replacing the inputs.

## Before JavaScript runs

The markup above is complete and usable on its own: native controls submit, labels associate, and authored ARIA is already correct. Registration adds state synchronisation, focus management, and keyboard coordination on top of it.
