# Radio Group

Drawn rings; choosing one inks a dot in the middle with a tight spiral.

Docs: https://ballpoint.st9wd.com/docs/radio-group

```bash
npx shadcn@latest add @ballpoint/radio-group
```

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

<RadioGroup defaultValue="blue">
  <Label>
    <RadioGroupItem value="blue" />
    Blue ballpoint
  </Label>
  <Label>
    <RadioGroupItem value="pencil" />
    Pencil
  </Label>
</RadioGroup>
```

Props:

- `orientation`: `"vertical" | "horizontal"` (default `"vertical"`). The options as a column, or in a row that wraps.
- `RadioGroupItem disabled`: `boolean`. Dims the option and takes it out of the choice.
- `RadioGroupItem seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `passes`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's Radio: https://base-ui.com/react/components/radio

## Example

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

export default function RadioGroupDemo() {
  return (
    <div className="flex w-full max-w-lg flex-col gap-10">
      <RadioGroup defaultValue="blue" aria-label="Ink">
        {[
          ["blue", "Blue ballpoint"],
          ["black", "Black fineliner"],
          ["pencil", "Pencil"],
        ].map(([value, label]) => (
          <Label key={value}>
            <RadioGroupItem value={value} seed={`radio-${value}`} />
            {label}
          </Label>
        ))}
      </RadioGroup>
      <RadioGroup defaultValue="cream" aria-label="Paper" className="grid-cols-1 gap-5 sm:grid-cols-2">
        {[
          ["cream", "Cream", "Warm, like a notebook."],
          ["night", "Night", "Navy, for the lamp."],
        ].map(([value, title, description]) => (
          <FieldLabel key={value}>
            <Field orientation="horizontal">
              <RadioGroupItem value={value} seed={`paper-${value}`} />
              <FieldContent>
                <FieldTitle>{title}</FieldTitle>
                <FieldDescription>{description}</FieldDescription>
              </FieldContent>
            </Field>
          </FieldLabel>
        ))}
      </RadioGroup>
    </div>
  );
}
```

## States

A disabled option, and a group with no choice made yet and an error.

```tsx
import { Field, FieldDescription, FieldError, FieldLabel, FieldLegend, FieldSet } from "@/components/ui/field";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";

export default function RadioGroupStates() {
  return (
    <div className="grid w-full max-w-md gap-8">
      <RadioGroup defaultValue="day" aria-label="Time" className="flex flex-wrap gap-6">
        <Label>
          <RadioGroupItem value="day" seed="rs-day" />
          Day
        </Label>
        <Label>
          <RadioGroupItem value="night" seed="rs-night" />
          Night
        </Label>
        <Label>
          <RadioGroupItem value="dusk" disabled seed="rs-dusk" />
          Dusk
        </Label>
      </RadioGroup>
      <FieldSet data-invalid>
        <FieldLegend variant="label">Delivery</FieldLegend>
        <FieldDescription>Choose one before you continue.</FieldDescription>
        <RadioGroup aria-label="Delivery" aria-invalid>
          {["Post", "Courier"].map((option) => (
            <Field key={option} orientation="horizontal" data-invalid>
              <RadioGroupItem value={option.toLowerCase()} id={`rs-${option}`} aria-invalid seed={`rs-${option}`} />
              <FieldLabel htmlFor={`rs-${option}`}>{option}</FieldLabel>
            </Field>
          ))}
        </RadioGroup>
        <FieldError>Pick a delivery option.</FieldError>
      </FieldSet>
    </div>
  );
}
```

## Choice cards

Wrap a Field in FieldLabel and the whole row is the target.

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

const papers = [
  ["cream", "Cream", "Warm, like old letters."],
  ["white", "White", "Crisp, for drawings."],
  ["legal", "Legal pad", "Yellow, for lists and plans."],
] as const;

export default function RadioGroupCards() {
  return (
    <RadioGroup defaultValue="cream" aria-label="Paper" className="max-w-md">
      {papers.map(([value, title, note]) => (
        <FieldLabel key={value} htmlFor={`rc-${value}`}>
          <Field orientation="horizontal">
            <FieldContent>
              <FieldTitle>{title}</FieldTitle>
              <FieldDescription>{note}</FieldDescription>
            </FieldContent>
            <RadioGroupItem value={value} id={`rc-${value}`} seed={`rc-${value}`} />
          </Field>
        </FieldLabel>
      ))}
    </RadioGroup>
  );
}
```

## Horizontal

The options in a row that wraps, under a heading.

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

const sizes = ["A6", "A5", "A4", "Letter"];

export default function RadioGroupHorizontal() {
  return (
    <div className="flex flex-col gap-3">
      <span id="rh-size" className="font-bold">
        Notebook size
      </span>
      <RadioGroup orientation="horizontal" defaultValue="A5" aria-labelledby="rh-size">
        {sizes.map((size) => (
          <Label key={size}>
            <RadioGroupItem value={size} seed={`rh-${size}`} />
            {size}
          </Label>
        ))}
      </RadioGroup>
    </div>
  );
}
```

## A scale

A 1 to 5 rating with a word at each end, read back as you choose.

```tsx
"use client";

import { useState } from "react";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";

export default function RadioGroupScale() {
  const [score, setScore] = useState("");
  return (
    <div className="flex w-full max-w-md flex-col gap-3">
      <span id="rsc-q" className="font-bold">
        How did the pen feel?
      </span>
      <RadioGroup orientation="horizontal" value={score} onValueChange={(v) => setScore(v as string)} aria-labelledby="rsc-q" className="justify-between gap-0">
        {[1, 2, 3, 4, 5].map((n) => (
          <Label key={n} className="flex-col gap-2">
            <RadioGroupItem value={String(n)} seed={`rsc-${n}`} />
            {n}
          </Label>
        ))}
      </RadioGroup>
      <div className="flex justify-between text-sm text-ink-3">
        <span>Scratchy</span>
        <span>Smooth</span>
      </div>
      <p className="text-ink-2">{score ? `You gave it a ${score}.` : "Pick one."}</p>
    </div>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
