Radio Group

The Radio Group component allows users to select a single option from a list of mutually exclusive choices. It’s commonly used in forms for selecting a single option from a set.

Usage

<script>
  import { RadioGroup, RadioGroupItem } from "$lib/components/ui/radio-group";
</script>

<RadioGroup value="option-one">
  <div class="flex items-center space-x-2">
    <RadioGroupItem value="option-one" id="option-one" />
    <label for="option-one">Option One</label>
  </div>
  <div class="flex items-center space-x-2">
    <RadioGroupItem value="option-two" id="option-two" />
    <label for="option-two">Option Two</label>
  </div>
</RadioGroup>

Examples

Basic Radio Group

A simple radio group with a few options.

Radio Group with Default Value

A radio group with a preset selected value.

Disabled Radio Group

A radio group with disabled options.

Horizontal Radio Group

A radio group with options aligned horizontally.

Radio Group in a Form

Using radio group in a form with a submit button.

Notification preferences

Radio Group with Descriptions

A radio group with additional descriptions for each option.

Pay with your credit or debit card.

Pay with your PayPal account.

Pay with Apple Pay.

API Reference

RadioGroup

PropertyTypeDefaultDescription
valuestringundefinedThe value of the selected radio item.
onValueChangefunctionundefinedFunction called when the value changes.
disabledbooleanfalseWhen true, prevents the user from interacting with the radio group.
requiredbooleanfalseWhen true, indicates that the user must select an option before the form can be submitted.
namestringundefinedThe name of the radio group when used in a form.
classstring"grid gap-2"Additional CSS classes to apply to the radio group.
refHTMLDivElementnullReference to the underlying HTML div element.

RadioGroupItem

PropertyTypeDefaultDescription
valuestringrequiredThe value of the radio item. Must be a unique string within the radio group.
disabledbooleanfalseWhen true, prevents the user from interacting with the radio item.
requiredbooleanfalseWhen true, indicates that the user must select this radio item before the form can be submitted.
classstringundefinedAdditional CSS classes to apply to the radio item.
refHTMLButtonElementnullReference to the underlying HTML button element.

Implementation

The Radio Group component is built on top of bits-ui primitives. It provides custom styling for checked and unchecked states, with appropriate focus and hover effects:

// RadioGroup styling
"grid gap-2"

// RadioGroupItem styling
"border-primary text-primary focus-visible:ring-ring aspect-square size-4 rounded-full border shadow focus:outline-none focus-visible:ring-1 disabled:cursor-not-allowed disabled:opacity-50"

The radio group items display a filled circle when selected, which is implemented using the Circle icon from Lucide.

This component is built with accessibility in mind, supporting keyboard navigation, screen readers, and proper ARIA attributes. The radio group ensures that only one option can be selected at a time, making it ideal for mutually exclusive choices in forms.