---
title: Radio Group for Astro
description: "Astro radio choices for one selected value in a related set."
seo:
    title: Radio Group for Astro | Shadcn & Tailwind CSS
related:
    - /components/checkbox
    - /components/select
sidebar:
    label: "Radio Group"
---

```astro
---
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---

<RadioGroup defaultValue="option-one" name="demo">
  <div class="flex items-center gap-3">
    <RadioGroupItem value="option-one" id="option-one" />
    <Label for="option-one">Option One</Label>
  </div>
  <div class="flex items-center gap-3">
    <RadioGroupItem value="option-two" id="option-two" />
    <Label for="option-two">Option Two</Label>
  </div>
</RadioGroup>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/radio-group
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/radio-group
```

#### src/components/ui/radio-group/index.ts

```ts
export { default as RadioGroup } from './radio-group.astro'
export { default as RadioGroupItem } from './radio-group-item.astro'
export type { RadioGroupProps } from './types.ts'
```

#### src/components/ui/radio-group/radio-group-item.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span"> & {
  value: string
  disabled?: boolean
  indicatorClass?: string
}

const {
  class: className,
  value,
  disabled,
  indicatorClass,
  ...props
} = Astro.props
---

<span
  data-slot="radio-group-item"
  data-value={value}
  data-disabled={disabled ? "" : undefined}
  class={cn("relative inline-flex size-4.5 shrink-0 items-center justify-center rounded-full border border-input bg-background not-dark:bg-clip-padding shadow-xs/5 outline-none transition-shadow before:pointer-events-none before:absolute before:inset-0 before:rounded-full not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_1px_--theme(--color-black/4%)] focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background aria-invalid:border-destructive/36 focus-visible:aria-invalid:border-destructive/64 focus-visible:aria-invalid:ring-destructive/48 data-disabled:cursor-not-allowed data-disabled:opacity-64 sm:size-4 dark:not-data-checked:bg-input/32 dark:aria-invalid:ring-destructive/24 dark:not-data-disabled:not-data-checked:not-aria-invalid:before:shadow-[0_-1px_--theme(--color-white/6%)] [[data-disabled],[data-checked],[aria-invalid=true]]:shadow-none data-checked:border-primary data-checked:bg-primary group/radio-group-item peer", className)}
  {...props}
>
  <span
    aria-hidden="true"
    data-slot="radio-group-indicator"
    class={cn("absolute -inset-px flex items-center justify-center rounded-full opacity-0 group-data-checked/radio-group-item:opacity-100", indicatorClass)}
  >
    <span
      data-slot="radio-group-indicator-icon"
      class="bg-primary-foreground absolute top-1/2 left-1/2 size-2 sm:size-1.5 -translate-x-1/2 -translate-y-1/2 rounded-full"
    ></span>
  </span>
</span>
```

#### src/components/ui/radio-group/radio-group.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { RadioGroupProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & RadioGroupProps

const {
  class: className,
  defaultValue,
  name,
  disabled,
  readOnly,
  required,
  ...props
} = Astro.props
---

<div
  data-slot="radio-group"
  data-default-value={defaultValue}
  data-name={name}
  data-disabled={disabled}
  data-read-only={readOnly}
  data-required={required}
  class={cn("flex flex-col gap-3 w-full", className)}
  {...props}
>
  <slot />
</div>

<script>
  import { create } from "@data-slot/radio-group"
  import type { RadioGroupController } from "@data-slot/radio-group"
  // Destroy bindings before a view transition so the next page-load binds once.
  const controllers: RadioGroupController[] = []
  const initialize = () => controllers.push(...create())
  initialize()
  document.addEventListener("astro:page-load", initialize)
  document.addEventListener("astro:before-swap", () => {
    for (const controller of controllers) controller.destroy()
    controllers.length = 0
  })
</script>
```

#### src/components/ui/radio-group/types.ts

```ts
import type { RadioGroupOptions } from '@data-slot/radio-group'

/**
 * Radio group configuration rendered as data attributes.
 */
export type RadioGroupProps = Pick<
    RadioGroupOptions,
    'defaultValue' | 'name' | 'disabled' | 'readOnly' | 'required'
>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```astro
---
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---
```

```astro
<RadioGroup defaultValue="option-one" name="example">
  <div class="flex items-center gap-3">
    <RadioGroupItem value="option-one" id="option-one" />
    <Label for="option-one">Option One</Label>
  </div>
  <div class="flex items-center gap-3">
    <RadioGroupItem value="option-two" id="option-two" />
    <Label for="option-two">Option Two</Label>
  </div>
</RadioGroup>
```

## Composition

Arrange the parts as shown for a `RadioGroup`:

```text
RadioGroup
├── RadioGroupItem
└── RadioGroupItem
```

## Examples

### With Descriptions

Radio group items with a title and supporting copy using the `Field` component.

```astro
---
import {
  Field,
  FieldContent,
  FieldDescription,
  FieldLabel,
} from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---

<RadioGroup defaultValue="starter" name="plan" class="w-full max-w-sm">
  <Field orientation="horizontal">
    <RadioGroupItem id="starter" value="starter" />
    <FieldContent>
      <FieldLabel for="starter">Starter</FieldLabel>
      <FieldDescription>Basic features for personal projects.</FieldDescription>
    </FieldContent>
  </Field>
  <Field orientation="horizontal">
    <RadioGroupItem id="pro" value="pro" />
    <FieldContent>
      <FieldLabel for="pro">Pro</FieldLabel>
      <FieldDescription>Advanced tooling for growing teams.</FieldDescription>
    </FieldContent>
  </Field>
</RadioGroup>
```

### Grid Layout

Wrap the whole row in `FieldLabel` when you want the entire card to act as the
click target.

```astro
---
import {
  Field,
  FieldContent,
  FieldDescription,
  FieldLabel,
  FieldTitle,
} from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---

<RadioGroup defaultValue="team" name="audience" class="w-full max-w-md">
  <FieldLabel class="w-full">
    <Field orientation="horizontal">
      <RadioGroupItem value="solo" />
      <FieldContent>
        <FieldTitle>Solo</FieldTitle>
        <FieldDescription>
          Best for freelancers and individual builders.
        </FieldDescription>
      </FieldContent>
    </Field>
  </FieldLabel>
  <FieldLabel class="w-full">
    <Field orientation="horizontal">
      <RadioGroupItem value="team" />
      <FieldContent>
        <FieldTitle>Team</FieldTitle>
        <FieldDescription>
          Collaboration, approvals, and shared workspace access.
        </FieldDescription>
      </FieldContent>
    </Field>
  </FieldLabel>
</RadioGroup>
```

### With FieldSet

Use `FieldSet` and `FieldLegend` to group radio items with a shared label and
description.

```astro
---
import {
  Field,
  FieldDescription,
  FieldLabel,
  FieldLegend,
  FieldSet,
} from "@/components/ui/field"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---

<div class="w-full max-w-sm">
  <FieldSet>
    <FieldLegend>Notifications</FieldLegend>
    <FieldDescription>Choose how you want to be notified.</FieldDescription>
    <RadioGroup defaultValue="email" name="notifications">
      <Field orientation="horizontal">
        <RadioGroupItem value="email" id="notifications-email" />
        <FieldLabel for="notifications-email">Email</FieldLabel>
      </Field>
      <Field orientation="horizontal">
        <RadioGroupItem value="sms" id="notifications-sms" />
        <FieldLabel for="notifications-sms">SMS</FieldLabel>
      </Field>
      <Field orientation="horizontal">
        <RadioGroupItem value="push" id="notifications-push" />
        <FieldLabel for="notifications-push">Push notification</FieldLabel>
      </Field>
    </RadioGroup>
  </FieldSet>
</div>
```

### Disabled

Use the `disabled` prop on `RadioGroupItem` to disable individual items.

```astro
---
import { Label } from "@/components/ui/label"
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
---

<RadioGroup defaultValue="pro" name="plans">
  <div class="flex items-center gap-3">
    <RadioGroupItem value="starter" id="plan-starter" />
    <Label for="plan-starter">Starter</Label>
  </div>
  <div class="flex items-center gap-3">
    <RadioGroupItem value="pro" id="plan-pro" />
    <Label for="plan-pro">Pro</Label>
  </div>
  <div class="flex items-center gap-3">
    <RadioGroupItem value="enterprise" id="plan-enterprise" disabled />
    <Label for="plan-enterprise">Enterprise (Disabled)</Label>
  </div>
</RadioGroup>
```

## API Reference

### RadioGroupProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/radio-group/types.ts"
    name="RadioGroupProps"
/>

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/radio-group/README.md) for more information on interactivity.

### Events

Dispatch these events on the radio group root. The root emits the outbound events.

#### Outbound events

| Event                | Detail                      |
| -------------------- | --------------------------- |
| `radio-group:change` | `{ value: string \| null }` |

#### Inbound events

| Event             | Detail                      |
| ----------------- | --------------------------- |
| `radio-group:set` | `{ value: string \| null }` |

Read the [installed source](/r/radio-group.json) for the complete prop types.

## When to use Radio Group

Use Radio Group when all choices should be visible and only one can be selected. Give the group a name for form data and label each item. Use a fieldset and legend when the choices answer one question.

## Keyboard and behavior

The data-slot controller maintains selection and keyboard movement. Test arrow keys and Space, then confirm the submitted form value. Disabled choices should remain understandable rather than disappearing without explanation.

## Theme and layout

Selected borders and indicators use the primary token. Check long labels and the spacing between each indicator and its text. Invalid state needs an error message connected to the group.
