# Radio Group

Pick exactly one of a short list of options, all visible at once.

Pick exactly one of a short list of options, all visible at once. Each option is a 16px round control on a hairline ring whose inset ring grows into a 5px ink ring when selected, so the 6px on-ink centre forms as the ring closes in. Stacked or in a row. It is one tab stop: arrow keys move and select. Behaviour comes from the Zag radio-group machine, through Ark UI in React, Vue and Svelte. Visually hidden native radios carry focus, arrow keys and the form value.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `radio-group` machine, through Ark UI

## Add

```bash
npx fndn add radio-group
```

It writes the folder `radio-group/` into the project's ui folder (`index.ts`, `radio-group.tsx`, `RadioGroup.vue`, `RadioGroup.svelte`, `radio-group.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { RadioGroup } from "@/components/ui/radio-group";
```

```vue
import { RadioGroup } from "@/components/ui/radio-group";
```

```svelte
import { RadioGroup } from "$lib/components/ui/radio-group";
```

## Parts

In the DOM the root carries `data-slot="radio-group"` and each part `data-slot="radio-group-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `radio-group` |  | role=radiogroup; data-orientation=vertical\|horizontal; data-disabled Wrapper rule: class and style land on this outermost element (what a caller sizes and places); every other attribute stays on the control. |
| `label` | `span` | `radio-group-label` | optional | Visible group label; names the group |
| `highlight` | `span` | `radio-group-highlight` | optional | Row layout only: the list's one gliding highlight (`glideLayer` of `lib/glide.ts`, aria-hidden); it follows the pointer and keyboard focus. |
| `item` | `label` | `radio-group-item` | repeats | One option and its hit area; data-state=checked\|unchecked, data-focus-visible, data-disabled. In row layout it composes the shared option row (`lib/option.ts`). |
| `control` | `div` | `radio-group-control` |  | The 16px round control (aria-hidden); one inset ring draws both states (no dot element); carries kbd-instant |
| `item-text` | `span` | `radio-group-item-text` |  | The option's text and accessible name |
| `input` | `input` | `radio-group-input` |  | Native radio, visually hidden: focus, arrow keys and form value |
| `custom` | `label` | `radio-group-custom` | optional | Row layout only: the free-text row ("Something else") after the options, in the same list; it composes the shared option row (`lib/option.ts`). |
| `custom-input` | `input` | `radio-group-custom-input` | optional | The free-text field: type=text, named by aria-label. Typing clears the selection; picking an option clears it. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `{ value: string; label: string; disabled?: boolean }[]` |  | Options in order. Strings are accepted as value = label. |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). Selected value, or null for none. |
| `label` | `string` |  | Visible group label; otherwise pass aria-label. |
| `orientation` | `"vertical" \| "horizontal"` | `"vertical"` | vertical: stacked options. horizontal: options in a row that wraps. |
| `layout` | `"inline" \| "row"` | `"inline"` | inline: each control with its label, sized to the text. row: a list; each option fills a 30px option row (`lib/option.ts`) under one gliding highlight, stacked whatever the orientation; the control is drawn the same. |
| `disabled` | `boolean` | `false` |  |
| `name` | `string` |  | Form field name of the radios. |
| `customPlaceholder` | `string` |  | Row layout: adds the free-text row after the options with this placeholder (e.g. "Something else…"). |
| `customLabel` | `string` |  | Accessible name of the free-text field; defaults to the placeholder. |
| `customEnterKeyHint` | `"enter" \| "done" \| "go" \| "next" \| "previous" \| "search" \| "send"` |  | enterkeyhint of the free-text field: the label of the Enter key on a phone keyboard ("next" when another step follows). |
| `custom` | `string` |  | Bindable (React: `defaultCustom` and `onCustomChange`; Vue: `v-model:custom`; Svelte: `bind:custom`). Text of the free-text row. Typing makes the value null; picking an option makes it empty. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string \| null) |
| `customChange` | (text: string) |

## States

`checked`, `hover`, `press`, `focus-visible`, `disabled`, `kbd-instant`, `reduced-motion`, `row`, `custom`

## Keyboard and accessibility

Role: radiogroup.

Keyboard:

- Tab reaches the selected option (or the first, when none is selected); the group is one tab stop
- ArrowDown / ArrowRight select the next enabled option, ArrowUp / ArrowLeft the previous; they wrap
- Home / End select the first / last enabled option
- Space selects the focused option
- Row layout: Tab from the group reaches the free-text field, which keeps Home / End for its text

Notes:

- Give the group a visible label or an aria-label.
- Disabled options are skipped by the arrow keys.
- To select from outside, control `value` (null clears): React value / onValueChange, Vue v-model:value, Svelte bind:value.

## Motion

- select: one property, the inset ring, grows from 1.5px line-strong to 5px ink over duration.base with easing.out-strong, so the on-ink centre forms as the ring closes in
- deselect: the ring runs back over duration.fast; when the selection moves, the old option empties while the new one fills, both from the same moment
- press (pointer only): the control scales to motion.press-scale-sm over duration.press with easing.out-strong
- keyboard (arrows, Space, Home, End): instant; every control carries kbd-instant and the label colour is instant too
- mount: nothing animates on the first paint, in every target
- reduced motion: the ring keeps its width and only changes colour over duration.fast; the press stays
- row layout: one glide highlight (`glideLayer` of `lib/glide.ts`) travels to the hovered or focused row (duration.move, easing.out-strong; first appearance fades in); press darkens the row (hover-2) and scales the control as above

## Tokens

`color.ink`, `color.ink-2`, `color.ink-3`, `color.on-ink`, `color.line-strong`, `color.accent`, `text.xs`, `text.sm`, `duration.fast`, `duration.base`, `duration.press`, `easing.out-strong`, `motion.press-scale-sm`, `text.md`

## Examples

### Project visibility

React · `project-visibility.tsx`

```tsx
import type { ComponentProps } from "react";
import { RadioGroup } from "@/components/ui/radio-group";

/* exactly one of a short list, all visible at once */
type Props = ComponentProps<typeof RadioGroup>;

const visibility: Props["items"] = [
  { value: "private", label: "Private: only members of web-app" },
  { value: "internal", label: "Internal: everyone at Northwind" },
  { value: "public", label: "Public: anyone with the link" },
];

export default function ProjectVisibility() {
  return <RadioGroup name="visibility" label="Who can see web-app" items={visibility} defaultValue="internal" />;
}
```

Vue · `project-visibility.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { RadioGroup } from "@/components/ui/radio-group";

/* exactly one of a short list, all visible at once */
const visibility = [
  { value: "private", label: "Private: only members of web-app" },
  { value: "internal", label: "Internal: everyone at Northwind" },
  { value: "public", label: "Public: anyone with the link" },
];

const value = ref("internal");
</script>

<template>
  <RadioGroup v-model:value="value" name="visibility" label="Who can see web-app" :items="visibility" />
</template>
```

Svelte · `project-visibility.svelte`

```svelte
<script lang="ts">
  import { RadioGroup } from "$lib/components/ui/radio-group";

  /* exactly one of a short list, all visible at once */
  const visibility = [
    { value: "private", label: "Private: only members of web-app" },
    { value: "internal", label: "Internal: everyone at Northwind" },
    { value: "public", label: "Public: anyone with the link" },
  ];

  let value = $state("internal");
</script>

<RadioGroup bind:value name="visibility" label="Who can see web-app" items={visibility} />
```

### Billing cycle, in a row

React · `billing-cycle.tsx`

```tsx
import type { ComponentProps } from "react";
import { RadioGroup } from "@/components/ui/radio-group";

/* horizontal: two or three short options in a row */
type Props = ComponentProps<typeof RadioGroup>;

const cycles: Props["items"] = [
  { value: "monthly", label: "Monthly · $24 per seat" },
  { value: "yearly", label: "Yearly · $19 per seat" },
];

export default function BillingCycle() {
  return <RadioGroup name="cycle" label="Billing cycle" orientation="horizontal" items={cycles} defaultValue="yearly" />;
}
```

Vue · `billing-cycle.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { RadioGroup } from "@/components/ui/radio-group";

/* horizontal: two or three short options in a row */
const cycles = [
  { value: "monthly", label: "Monthly · $24 per seat" },
  { value: "yearly", label: "Yearly · $19 per seat" },
];

const value = ref("yearly");
</script>

<template>
  <RadioGroup v-model:value="value" name="cycle" label="Billing cycle" orientation="horizontal" :items="cycles" />
</template>
```

Svelte · `billing-cycle.svelte`

```svelte
<script lang="ts">
  import { RadioGroup } from "$lib/components/ui/radio-group";

  /* horizontal: two or three short options in a row */
  const cycles = [
    { value: "monthly", label: "Monthly · $24 per seat" },
    { value: "yearly", label: "Yearly · $19 per seat" },
  ];

  let value = $state("yearly");
</script>

<RadioGroup bind:value name="cycle" label="Billing cycle" orientation="horizontal" items={cycles} />
```

### Why are you cancelling (rows, with free text)

React · `cancel-reason.tsx`

```tsx
import type { ComponentProps } from "react";
import { RadioGroup } from "@/components/ui/radio-group";

/* rows: a list of answers under one highlight, with a free-text row for anything else */
type Props = ComponentProps<typeof RadioGroup>;

const reasons: Props["items"] = [
  { value: "price", label: "Too expensive for our team" },
  { value: "feature", label: "Missing a feature we need" },
  { value: "switching", label: "Moving to another tool" },
];

export default function CancelReason() {
  return (
    <RadioGroup
      name="reason"
      label="Why are you cancelling the Team plan?"
      layout="row"
      items={reasons}
      customPlaceholder="Something else…"
      customLabel="Another reason"
      customEnterKeyHint="send"
      className="w-80"
    />
  );
}
```

Vue · `cancel-reason.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { RadioGroup } from "@/components/ui/radio-group";

/* rows: a list of answers under one highlight, with a free-text row for anything else */
const reasons = [
  { value: "price", label: "Too expensive for our team" },
  { value: "feature", label: "Missing a feature we need" },
  { value: "switching", label: "Moving to another tool" },
];

const value = ref<string | null>(null);
</script>

<template>
  <RadioGroup
    v-model:value="value"
    name="reason"
    label="Why are you cancelling the Team plan?"
    layout="row"
    :items="reasons"
    customPlaceholder="Something else…"
    customLabel="Another reason"
    customEnterKeyHint="send"
    class="w-80"
  />
</template>
```

Svelte · `cancel-reason.svelte`

```svelte
<script lang="ts">
  import { RadioGroup } from "$lib/components/ui/radio-group";

  /* rows: a list of answers under one highlight, with a free-text row for anything else */
  const reasons = [
    { value: "price", label: "Too expensive for our team" },
    { value: "feature", label: "Missing a feature we need" },
    { value: "switching", label: "Moving to another tool" },
  ];

  let value = $state<string | null>(null);
</script>

<RadioGroup
  bind:value
  name="reason"
  label="Why are you cancelling the Team plan?"
  layout="row"
  items={reasons}
  customPlaceholder="Something else…"
  customLabel="Another reason"
  customEnterKeyHint="send"
  class="w-80"
/>
```

### Log retention, one option not on your plan

React · `log-retention.tsx`

```tsx
import type { ComponentProps } from "react";
import { RadioGroup } from "@/components/ui/radio-group";

/* an option the plan does not include stays in the list, disabled, and says why */
type Props = ComponentProps<typeof RadioGroup>;

const retention: Props["items"] = [
  { value: "7", label: "7 days" },
  { value: "30", label: "30 days" },
  { value: "365", label: "1 year (Business plan)", disabled: true },
];

export default function LogRetention() {
  return <RadioGroup name="retention" label="Keep deploy logs for" items={retention} defaultValue="30" />;
}
```

Vue · `log-retention.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { RadioGroup } from "@/components/ui/radio-group";

/* an option the plan does not include stays in the list, disabled, and says why */
const retention = [
  { value: "7", label: "7 days" },
  { value: "30", label: "30 days" },
  { value: "365", label: "1 year (Business plan)", disabled: true },
];

const value = ref("30");
</script>

<template>
  <RadioGroup v-model:value="value" name="retention" label="Keep deploy logs for" :items="retention" />
</template>
```

Svelte · `log-retention.svelte`

```svelte
<script lang="ts">
  import { RadioGroup } from "$lib/components/ui/radio-group";

  /* an option the plan does not include stays in the list, disabled, and says why */
  const retention = [
    { value: "7", label: "7 days" },
    { value: "30", label: "30 days" },
    { value: "365", label: "1 year (Business plan)", disabled: true },
  ];

  let value = $state("30");
</script>

<RadioGroup bind:value name="retention" label="Keep deploy logs for" items={retention} />
```

### Pick a region (no default)

React · `region.tsx`

```tsx
import type { ComponentProps } from "react";
import { RadioGroup } from "@/components/ui/radio-group";

/* no default when there is no safe guess: nothing is picked until someone chooses */
type Props = ComponentProps<typeof RadioGroup>;

const regions: Props["items"] = [
  { value: "us-east", label: "US East (Virginia)" },
  { value: "eu-west", label: "EU West (Frankfurt)" },
  { value: "ap-northeast", label: "Asia Pacific (Tokyo)" },
];

export default function Region() {
  return <RadioGroup name="region" label="Region for data-pipeline" items={regions} defaultValue={null} />;
}
```

Vue · `region.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { RadioGroup } from "@/components/ui/radio-group";

/* no default when there is no safe guess: nothing is picked until someone chooses */
const regions = [
  { value: "us-east", label: "US East (Virginia)" },
  { value: "eu-west", label: "EU West (Frankfurt)" },
  { value: "ap-northeast", label: "Asia Pacific (Tokyo)" },
];

const value = ref<string | null>(null);
</script>

<template>
  <RadioGroup v-model:value="value" name="region" label="Region for data-pipeline" :items="regions" />
</template>
```

Svelte · `region.svelte`

```svelte
<script lang="ts">
  import { RadioGroup } from "$lib/components/ui/radio-group";

  /* no default when there is no safe guess: nothing is picked until someone chooses */
  const regions = [
    { value: "us-east", label: "US East (Virginia)" },
    { value: "eu-west", label: "EU West (Frankfurt)" },
    { value: "ap-northeast", label: "Asia Pacific (Tokyo)" },
  ];

  let value = $state<string | null>(null);
</script>

<RadioGroup bind:value name="region" label="Region for data-pipeline" items={regions} />
```

## Recipe

`radio-group.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { option } from "../lib/option";
import { glideLayer } from "../lib/glide";

/* radio-group — 16px round controls on the Zag radio-group machine.
 * Options are <label>s around a visually hidden native radio, so focus, arrow keys and the form
 * value are the browser's own. Like the checkbox, the text line is 18px and the control sits 1px
 * down: control and first text line share one centre line, and a wrapped label keeps it on line one.
 *
 * Parts are found by data-slot
 * (radio-group, radio-group-item, radio-group-control, …); the root slot is `base`. The control
 * also works written by hand in another option row (a menu radio item: menu imports this recipe):
 * it hovers and presses with whatever element holds it. */

const DIR = "[unicode-bidi:plaintext] [&:dir(ltr)]:text-left [&:dir(rtl)]:text-right";
const row = option();

/* an option; in a row the shared option row goes first, so all of this wins over it */
const ITEM = [
  "relative inline-flex items-start gap-2 min-w-0 max-w-full",
  "text-sm leading-[18px] text-ink-2 cursor-pointer select-none touch-manipulation [-webkit-touch-callout:none]",
  /* the input says it too: a row is in its ink before anything has written data-state on it */
  "data-[state=checked]:text-ink [&:has(>[data-slot=radio-group-input]:checked)]:text-ink hover:text-ink",
  /* keyboard changes are instant, the label colour included (the controls carry kbd-instant) */
  "motion-colors-fast kbd:duration-0",
  "data-disabled:opacity-50 data-disabled:cursor-default data-disabled:pointer-events-none",
];

export const radioGroup = tv({
  slots: {
    base: "flex flex-col items-start gap-2 min-w-0 max-w-full",
    label: ["basis-full min-w-0 mb-0.5 text-sm font-medium leading-[18px] text-ink wrap-anywhere", DIR],
    /* layout="row": the one glide layer (glideLayer of lib/glide.ts) that travels to the row under the pointer */
    highlight: glideLayer,
    item: ITEM,
    control: [
      "kbd-instant",
      "relative grid place-items-center shrink-0 size-4 my-px rounded-[50%] bg-transparent",
      "data-[state=checked]:bg-on-ink",
      /* One moving thing: the inset ring. Selected, it grows from a 1.5px line-strong hairline to a
       * 5px ink ring, so the 6px on-ink centre forms as the ring closes in. Deselected, it runs back
       * over `fast`: when the selection moves, the old option empties while the new one fills. */
      "[box-shadow:inset_0_0_0_1.5px_var(--fndn-line-strong)] data-[state=checked]:[box-shadow:inset_0_0_0_5px_var(--fndn-ink)]",
      "[@media(hover:hover)_and_(pointer:fine)]:[:hover>&[data-state=unchecked]]:[box-shadow:inset_0_0_0_1.5px_var(--fndn-ink-3)]",
      /* the centre's background only: the inherited text colour does not move here */
      "motion-shadow-fast-out motion-also-[background-color_var(--fndn-duration-fast)_ease] motion-transform-press",
      "data-[state=checked]:motion-shadow-base-out data-[state=checked]:motion-also-[background-color_var(--fndn-duration-base)_ease]",
      /* press: pointer only (Space also makes the label :active, and keyboard changes are instant);
       * the holder is the option, or the row a hand-written control sits in */
      "[:root:not([data-fndn-input=keyboard])_:active>&]:scale-press-sm",
      /* reduced motion: the ring keeps its width and only changes colour (two layers of equal width
       * interpolate colour alone); the hairline stays line-strong under the ink, so the band fades
       * as one colour */
      "motion-reduce:motion-shadow-fast motion-reduce:data-[state=checked]:motion-shadow-fast motion-reduce:data-[state=checked]:motion-also-[background-color_var(--fndn-duration-fast)_ease]",
      "motion-reduce:[box-shadow:inset_0_0_0_5px_transparent,inset_0_0_0_1.5px_var(--fndn-line-strong)]",
      "motion-reduce:data-[state=checked]:[box-shadow:inset_0_0_0_5px_var(--fndn-ink),inset_0_0_0_1.5px_var(--fndn-line-strong)]",
      "motion-reduce:[@media(hover:hover)_and_(pointer:fine)]:[:hover>&[data-state=unchecked]]:[box-shadow:inset_0_0_0_5px_transparent,inset_0_0_0_1.5px_var(--fndn-ink-3)]",
      "data-focus-visible:outline-2 data-focus-visible:outline-solid data-focus-visible:outline-accent data-focus-visible:outline-offset-2",
    ],
    itemText: ["min-w-0 wrap-anywhere text-start", DIR],
    /* the native radio: present for focus, keys and forms, never seen */
    input: "sr-only opacity-0 pointer-events-none",
    /* the free-text row ("Something else"): a row in the same list, under the same glide, with a
     * bare text field where the control and text would be */
    custom: [row.base(), "flex w-full cursor-text active:bg-transparent has-focus-visible:shadow-[inset_0_0_0_1px_var(--fndn-accent)]"],
    customInput: [
      "flex-1 min-w-0 m-0 p-0 border-0 bg-transparent text-ink outline-none",
      "[font-family:inherit] [font-size:inherit] [font-weight:inherit] [font-style:inherit] leading-[18px] tracking-[inherit]",
      "placeholder:text-ink-3",
      /* dir="auto": the text keeps its own direction; the alignment stays on the page's side */
      "[:dir(ltr)>&]:text-left [:dir(rtl)>&]:text-right",
      /* under 16px iOS zooms the page on focus */
      "pointer-coarse:text-[16px]",
    ],
  },
  variants: {
    orientation: {
      vertical: {},
      horizontal: { base: "flex-row flex-wrap items-center gap-x-4" },
    },
    /* row: a list. Each option fills a row and composes the shared option row (lib/option.ts:
     * metrics, radius, press); the text line stays 18px and the row pads it to the option height. Rows are always
     * stacked. */
    layout: {
      inline: {},
      row: {
        base: "relative flex-col flex-nowrap items-stretch gap-0",
        label: "px-2 mb-1",
        item: [
          row.base(), ...ITEM,
          "flex w-full py-[calc((var(--option-height,30px)_-_18px)/2)] text-dense",
          "pointer-coarse:py-[calc((max(40px,var(--option-height,30px))_-_18px)/2)] pointer-coarse:text-md",
        ],
      },
    },
  },
  defaultVariants: { orientation: "vertical", layout: "inline" },
});

export type RadioGroupRecipeProps = VariantProps<typeof radioGroup>;
```
