# Radio Group

A set of options where exactly one can be selected.

Docs: https://corsairui.vercel.app/docs/radio-group

## Install

```bash
pnpm dlx shadcn@latest add @corsair-ui/radio-group
```

Also adds: `utils`.
npm: `@radix-ui/react-radio-group`, `clsx`, `lucide-react`, `tailwind-merge`.

## API

Built on Radix Radio Group (https://www.radix-ui.com/primitives/docs/components/radio-group); its props pass through.

## Styling

Restyle it in the project that installs it: `className` on each part, the theme variables, and these attributes with arbitrary variants (`[&_[data-slot=…]]:…`, `data-[state=…]:…`).

data-slot: `radio-group`, `radio-group-indicator`, `radio-group-item`

## Accessibility

- Arrow keys move between options; the group takes a single tab stop.
- Name the group with aria-label or a FieldSet and FieldLegend.

## Examples

On the docs page: Default, Horizontal, with a disabled option. The usage examples in the source comments below are the reference.

## Source

What `shadcn add` writes into the project:

### radio-group.tsx

```tsx
"use client";

import * as RadioGroupPrimitive from "@radix-ui/react-radio-group";
import { CircleIcon } from "lucide-react";
import type { ComponentProps } from "react";

import { cn } from "@/registry/default/lib/utils";

/** Give the group an accessible name: an `aria-label`, or wrap it in a FieldSet with a FieldLegend. */
function RadioGroup({ className, ...props }: ComponentProps<typeof RadioGroupPrimitive.Root>) {
  return (
    <RadioGroupPrimitive.Root
      data-slot="radio-group"
      className={cn("grid gap-3", className)}
      {...props}
    />
  );
}

function RadioGroupItem({ className, ...props }: ComponentProps<typeof RadioGroupPrimitive.Item>) {
  return (
    <RadioGroupPrimitive.Item
      data-slot="radio-group-item"
      className={cn(
        "peer border-input bg-field text-primary aspect-square size-4 shrink-0 cursor-pointer rounded-full border",
        "transition-[color,border-color,box-shadow] outline-none motion-reduce:transition-none",
        "data-[state=checked]:border-primary",
        "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
        "aria-[invalid=true]:border-destructive aria-[invalid=true]:ring-destructive/20",
        "disabled:cursor-not-allowed disabled:opacity-50",
        className
      )}
      {...props}
    >
      {/* The dot grows in when picked and shrinks out when another option is. */}
      <RadioGroupPrimitive.Indicator
        data-slot="radio-group-indicator"
        className={cn(
          "flex size-full items-center justify-center",
          "zoom-in-0 fade-in-0 zoom-out-0 fade-out-0 ease-out",
          "motion-safe:data-[state=checked]:animate-in motion-safe:data-[state=unchecked]:animate-out"
        )}
      >
        <CircleIcon className="fill-primary stroke-primary size-2" />
      </RadioGroupPrimitive.Indicator>
    </RadioGroupPrimitive.Item>
  );
}

export { RadioGroup, RadioGroupItem };
```
