Radio Group

A set of options of which one is checked at a time, each a circle filled with the action colour when checked and named by its label.

import { Label } from "@produkto/ui/Label";
import { RadioGroup, RadioGroupItem } from "@produkto/ui/RadioGroup";

interface Item {
  value: string;
  label: string;
}

const items: Item[] = [
  { value: "item-1", label: "Item 1" },
  { value: "item-2", label: "Item 2" },
  { value: "item-3", label: "Item 3" },
];

const Example = () => (
  <RadioGroup className="w-fit" defaultValue={items[1].value}>
    {items.map((item) => (
      <div className="flex items-center gap-3" key={item.value}>
        <RadioGroupItem
          id={`radio-group-preview-${item.value}`}
          value={item.value}
        />
        <Label htmlFor={`radio-group-preview-${item.value}`}>
          {item.label}
        </Label>
      </div>
    ))}
  </RadioGroup>
);

export default Example;

Usage

import { Label } from "@produkto/ui/Label";
import { RadioGroup, RadioGroupItem } from "@produkto/ui/RadioGroup";
<RadioGroup defaultValue="default">
  <div className="flex items-center gap-3">
    <RadioGroupItem value="default" id="default" />
    <Label htmlFor="default">Item 1</Label>
  </div>
  <div className="flex items-center gap-3">
    <RadioGroupItem value="compact" id="compact" />
    <Label htmlFor="compact">Item 2</Label>
  </div>
</RadioGroup>

Examples

Sizes

size on the group reaches every item: sm 14px, base 16px (the default) or lg 20px circles, the space between them a step either side. An item may also take its own.

import { Label } from "@produkto/ui/Label";
import {
  RadioGroup,
  RadioGroupItem,
  type RadioGroupSize,
} from "@produkto/ui/RadioGroup";

const variants: { size: RadioGroupSize; label: string }[] = [
  { size: "sm", label: "Small" },
  { size: "base", label: "Base" },
  { size: "lg", label: "Large" },
];

const items: string[] = ["Item 1", "Item 2", "Item 3"];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.size}>
        <Label id={`radio-group-size-${variant.size}`}>{variant.label}</Label>
        <RadioGroup
          aria-labelledby={`radio-group-size-${variant.size}`}
          size={variant.size}
        >
          {items.map((item, index) => (
            <div className="flex items-center gap-3" key={item}>
              <RadioGroupItem
                id={`radio-group-size-${variant.size}-${index}`}
                value={item}
              />
              <Label htmlFor={`radio-group-size-${variant.size}-${index}`}>
                {item}
              </Label>
            </div>
          ))}
        </RadioGroup>
      </div>
    ))}
  </div>
);

export default Example;

Orientation

orientation sets the options in a column by default, or in a row with "horizontal", where the left and right arrow keys move along it.

import { Label } from "@produkto/ui/Label";
import {
  RadioGroup,
  RadioGroupItem,
  type RadioGroupProps,
} from "@produkto/ui/RadioGroup";

const variants: {
  orientation: RadioGroupProps["orientation"];
  key: string;
  label: string;
}[] = [
  { orientation: undefined, key: "vertical", label: "Vertical" },
  { orientation: "horizontal", key: "horizontal", label: "Horizontal" },
];

const items: string[] = ["Item 1", "Item 2", "Item 3"];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.key}>
        <Label id={`radio-group-orientation-${variant.key}`}>
          {variant.label}
        </Label>
        <RadioGroup
          aria-labelledby={`radio-group-orientation-${variant.key}`}
          orientation={variant.orientation}
        >
          {items.map((item, index) => (
            <div className="flex items-center gap-3" key={item}>
              <RadioGroupItem
                id={`radio-group-orientation-${variant.key}-${index}`}
                value={item}
              />
              <Label
                htmlFor={`radio-group-orientation-${variant.key}-${index}`}
              >
                {item}
              </Label>
            </div>
          ))}
        </RadioGroup>
      </div>
    ))}
  </div>
);

export default Example;

Disabled

disabled on an item dims it and its label, and the arrow keys pass over it, beside an item as it is by default.

import { Label } from "@produkto/ui/Label";
import { RadioGroup, RadioGroupItem } from "@produkto/ui/RadioGroup";

const Example = () => (
  <RadioGroup className="w-fit">
    <div className="flex items-center gap-3">
      <RadioGroupItem id="radio-group-enabled" value="item-1" />
      <Label htmlFor="radio-group-enabled">Item 1</Label>
    </div>
    <div className="flex items-center gap-3">
      <RadioGroupItem disabled id="radio-group-disabled" value="item-2" />
      <Label htmlFor="radio-group-disabled">Item 2</Label>
    </div>
  </RadioGroup>
);

export default Example;

Invalid

error on the group writes what is wrong under it, rings every item in the destructive colour and describes the group to screen readers, beside one without an error.

import { Label } from "@produkto/ui/Label";
import { RadioGroup, RadioGroupItem } from "@produkto/ui/RadioGroup";

const variants: { key: string; error?: string }[] = [
  { key: "valid" },
  { key: "error", error: "Description" },
];

const items: string[] = ["Item 1", "Item 2"];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.key}>
        <Label id={`radio-group-${variant.key}`}>Label</Label>
        <RadioGroup
          aria-labelledby={`radio-group-${variant.key}`}
          error={variant.error}
        >
          {items.map((item, index) => (
            <div className="flex items-center gap-3" key={item}>
              <RadioGroupItem
                id={`radio-group-${variant.key}-${index}`}
                value={item}
              />
              <Label htmlFor={`radio-group-${variant.key}-${index}`}>
                {item}
              </Label>
            </div>
          ))}
        </RadioGroup>
      </div>
    ))}
  </div>
);

export default Example;

API Reference

RadioGroup

RadioGroupProps · components/RadioGroup/RadioGroup.types.ts
PropTypeDefault
sizeRadioGroupSize"sm" | "base" | "lg""base"
errorReactNode-

RadioGroupItem

RadioGroupItemProps · also takes the props of Radix RadioGroup.Item
PropTypeDefault
sizeRadioGroupSize"sm" | "base" | "lg"-

It also takes the props of Radix RadioGroup.Root.