# Radio group

> One choice from a short list: circles that fill with ink around a dot, or with a check in an island of option rows.

Docs: https://plumbago.ochotzas.com/components/radio-group

## Installation

```sh
npx plumbago add radio-group
```

This copies `components/ui/radio-group.tsx` into your project, along with `cn`, and installs `@base-ui/react`.

## Usage

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

<span id="service-label">Service</span>
<RadioGroup defaultValue="table" aria-labelledby="service-label">
  <label className="flex items-center gap-3">
    <Radio value="table" />
    Table service
  </label>
  <label className="flex items-center gap-3">
    <Radio value="counter" />
    Order at the counter
  </label>
</RadioGroup>
```

## Examples

### Preview

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

const services = [
  { value: "table", label: "Table service" },
  { value: "counter", label: "Order at the counter" },
  { value: "qr", label: "Order by QR code" },
]

export default function RadioGroupDemo() {
  return (
    <div className="flex flex-col gap-2">
      <span id="service-label" className="text-footnote font-semibold text-ink-2">
        Service
      </span>
      <RadioGroup defaultValue="table" aria-labelledby="service-label">
        {services.map((service) => (
          <label key={service.value} className="flex h-11 cursor-pointer items-center gap-3">
            <Radio value={service.value} />
            {service.label}
          </label>
        ))}
      </RadioGroup>
    </div>
  )
}
```

### Option list

```tsx
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle } from "@/components/ui/island"
import { Radio, RadioGroup } from "@/components/ui/radio-group"

const tipping = [
  { value: "off", title: "No tips", description: "Guests aren't asked to tip." },
  { value: "suggested", title: "Suggested amounts", description: "10, 12 or 15 percent, shown on the card reader." },
  { value: "custom", title: "Any amount", description: "Guests type what they want to leave." },
]

export default function RadioGroupOptionList() {
  return (
    <RadioGroup defaultValue="suggested" aria-label="Tipping" className="w-full max-w-md gap-0">
      <Island variant="list">
        {tipping.map((option) => (
          <ListRow key={option.value} render={<label />} className="min-h-16 has-focus-visible:inset-ring-2 has-focus-visible:inset-ring-accent">
            <ListRowText>
              <ListRowTitle>{option.title}</ListRowTitle>
              <ListRowSubtitle>{option.description}</ListRowSubtitle>
            </ListRowText>
            <Radio variant="check" value={option.value} className="focus-visible:outline-none" />
          </ListRow>
        ))}
      </Island>
    </RadioGroup>
  )
}
```

### Disabled

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

export default function RadioGroupDisabled() {
  return (
    <div className="flex flex-col gap-8 sm:flex-row sm:gap-16">
      <div className="flex max-w-60 flex-col gap-2">
        <span id="payouts-label" className="text-footnote font-semibold text-ink-2">
          Payouts
        </span>
        <RadioGroup defaultValue="weekly" aria-labelledby="payouts-label">
          <label className="flex h-11 items-center gap-3 has-data-disabled:text-ink-3">
            <Radio value="daily" disabled aria-describedby="daily-note" />
            Daily
          </label>
          <label className="flex h-11 cursor-pointer items-center gap-3">
            <Radio value="weekly" />
            Weekly
          </label>
        </RadioGroup>
        <p id="daily-note" className="text-footnote text-ink-2">
          Daily payouts need a verified bank account.
        </p>
      </div>
      <div className="flex max-w-60 flex-col gap-2">
        <span id="printer-label" className="text-footnote font-semibold text-ink-2">
          Kitchen printer
        </span>
        <RadioGroup defaultValue="grill" disabled aria-labelledby="printer-label">
          <label className="flex h-11 items-center gap-3 has-data-disabled:text-ink-3">
            <Radio value="grill" />
            Grill station
          </label>
          <label className="flex h-11 items-center gap-3 has-data-disabled:text-ink-3">
            <Radio value="pastry" />
            Pastry station
          </label>
        </RadioGroup>
        <p className="text-footnote text-ink-2">The printer is offline.</p>
      </div>
    </div>
  )
}
```

## Built on Base UI

Radio group wraps these Base UI parts and keeps their props:

- [Radio](https://base-ui.com/react/components/radio)
- [Radio group](https://base-ui.com/react/components/radio-group)

## Source

```tsx
import { Radio as BaseRadio } from "@base-ui/react/radio"
import { RadioGroup as BaseRadioGroup } from "@base-ui/react/radio-group"
import { cn } from "@/lib/cn"
import type { ComponentProps } from "react"

export type RadioGroupProps = Omit<ComponentProps<typeof BaseRadioGroup>, "className"> & {
  className?: string
}

export function RadioGroup({ className, ...props }: RadioGroupProps) {
  return <BaseRadioGroup data-slot="radio-group" className={cn("flex flex-col gap-1", className)} {...props} />
}

export type RadioProps = Omit<ComponentProps<typeof BaseRadio.Root>, "className"> & {
  variant?: "dot" | "check"
  className?: string
}

const indicatorMotion =
  "transition-[scale,opacity,filter] duration-spring ease-spring data-starting-style:scale-60 data-starting-style:opacity-0 data-ending-style:scale-60 data-ending-style:opacity-0 data-ending-style:duration-base data-ending-style:ease-in motion-safe:data-starting-style:blur-[2px] motion-safe:data-ending-style:blur-[2px]"

export function Radio({ variant = "dot", className, ...props }: RadioProps) {
  return (
    <BaseRadio.Root
      data-slot="radio"
      data-variant={variant}
      className={cn(
        "relative grid shrink-0 cursor-pointer place-items-center rounded-full ring-[1.5px] ring-ring ring-inset transition-[background-color,box-shadow] duration-base ease-out [-webkit-tap-highlight-color:transparent]",
        variant === "check" ? "size-[22px]" : "size-6",
        "data-checked:bg-ink data-checked:ring-ink data-disabled:cursor-default data-disabled:opacity-40",
        "hover:not-data-checked:not-data-disabled:ring-ink-3 group-hover/row:not-data-checked:not-data-disabled:ring-ink-3",
        "after:absolute after:-inset-2.5 after:content-['']",
        className,
      )}
      {...props}
    >
      <BaseRadio.Indicator
        data-slot="radio-indicator"
        className={cn(variant === "check" ? "grid place-items-center text-on-ink" : "size-2.5 rounded-full bg-on-ink", indicatorMotion)}
      >
        {variant === "check" && (
          <svg
            aria-hidden="true"
            viewBox="0 0 12 12"
            className="size-[11px] fill-none stroke-current"
            strokeWidth="2.4"
            strokeLinecap="round"
            strokeLinejoin="round"
          >
            <path d="m2.25 6.25 2.5 2.5 5-5.5" />
          </svg>
        )}
      </BaseRadio.Indicator>
    </BaseRadio.Root>
  )
}
```
