Skip to content
Shaken UI
Sign in

Radio Group

Save

A single-choice group as plain radios or as selectable option cards with an icon, description, and price.

Category
Forms
Version
0.1.1
Frameworks
Next.js, Vite + React
Styling
Tailwind CSS v4

Preview

Radio Group preview
Runner
When a tool call needs approval

Installation

npx @shakenui/cli add radio-group

Review the plan first. A dry run lists every file and package without changing anything:

npx @shakenui/cli add radio-group --dry-run

Usage

radio-group-example.tsx
import { RadioGroup, RadioGroupItem } from "@/components/shaken/ui/radio-group";

function CpuIcon() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round">
      <rect x="4" y="4" width="8" height="8" rx="1.5" />
      <path d="M6.5 1.75v2M9.5 1.75v2M6.5 12.25v2M9.5 12.25v2M1.75 6.5h2M1.75 9.5h2M12.25 6.5h2M12.25 9.5h2" />
    </svg>
  );
}

function BoltIcon() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round">
      <path d="M8.75 1.75L3.5 9h4l-.75 5.25L12.5 7h-4l.25-5.25z" />
    </svg>
  );
}

function LayersIcon() {
  return (
    <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round">
      <path d="M8 2l6 3.25-6 3.25-6-3.25L8 2z" />
      <path d="M2 8.25l6 3.25 6-3.25M2 11l6 3.25L14 11" />
    </svg>
  );
}

export default function RadioGroupExample() {
  return (
    <div className="grid w-full max-w-md gap-8">
      <RadioGroup variant="cards" label="Runner" defaultValue="standard" name="runner">
        <RadioGroupItem value="standard" icon={<CpuIcon />} label="Standard" description="2 vCPU, 4 GB. Fine for most agents." meta="$0.04/min" />
        <RadioGroupItem value="burst" icon={<BoltIcon />} label="Burst" description="8 vCPU, 16 GB for builds and test runs." meta="$0.16/min" />
        <RadioGroupItem value="dedicated" icon={<LayersIcon />} label="Dedicated" description="Reserved capacity with a fixed IP." meta="Contact us" disabled />
      </RadioGroup>

      <RadioGroup label="When a tool call needs approval" defaultValue="ask" name="approval">
        <RadioGroupItem value="ask" label="Ask me every time" />
        <RadioGroupItem value="trusted" label="Allow tools I marked as trusted" description="Everything else still waits for a person." />
        <RadioGroupItem value="never" label="Never pause the run" />
      </RadioGroup>
    </div>
  );
}

API

PropTypeDefaultDescription
variant"plain" | "cards""plain"Plain radios with labels, or a card per option.
value / defaultValuestring—The selected item's value.
onValueChange(value) => void—Called when the selection changes.
label / descriptionReactNode—Names and describes the group.
label (RadioGroupItem)ReactNode—The option's name; required.
description (RadioGroupItem)ReactNode—Secondary line, linked with aria-describedby.
icon / meta (RadioGroupItem)ReactNode—Cards only: a 16px icon tile at the start and a short value such as a price at the end.
disabledbooleanfalseOn the group or one item.

Accessibility

  • Base UI renders role="radiogroup" with role="radio" items and roving focus: arrow keys move and select, Tab leaves the group.
  • Each radio is named by its label through aria-labelledby, and its description is linked with aria-describedby, so a card's long text is not read as its name.
  • In the cards variant the whole card is the click target and draws the focus ring when the radio inside has keyboard focus.
  • The selected card thickens its border to 2px, so selection does not depend on color alone.
  • The dot scales in only with motion allowed.

Files

Installed into src/components/shaken, or components/shaken when the project has no src directory.

  • src/components/shaken/ui/radio-group.tsxradio-group
  • npm packages: @base-ui/react@^1.9.0React and Tailwind CSS v4 required

Source

The exact source the CLI installs, from /r/items/react/radio-group/0.1.1.json.

ui/radio-group.tsxShow
ui/radio-group.tsx
"use client";

import { Radio as BaseRadio } from "@base-ui/react/radio";
import { RadioGroup as BaseRadioGroup } from "@base-ui/react/radio-group";
import { createContext, type ReactNode, useContext, useId } from "react";

type ClassName<State> = string | ((state: State) => string | undefined) | undefined;

function cx(...classes: (string | false | null | undefined)[]) {
  return classes.filter(Boolean).join(" ");
}

/** Prepends base classes while keeping Base UI's state-function form of className working. */
function withBase<State>(base: string, className: ClassName<State>) {
  return typeof className === "function" ? (state: State) => cx(base, className(state)) : cx(base, className);
}

export type RadioGroupVariant = "plain" | "cards";

const VariantContext = createContext<RadioGroupVariant>("plain");

export interface RadioGroupProps<Value = string> extends BaseRadioGroup.Props<Value> {
  /** `plain` lists radios with labels; `cards` turns each option into a selectable card. */
  variant?: RadioGroupVariant;
  /** Names the group. Linked with `aria-labelledby`. */
  label?: ReactNode;
  /** A sentence under the label about the choice. */
  description?: ReactNode;
}

/**
 * One choice from a set. Arrow keys move the selection between options and Tab leaves the group,
 * as in a native radio group. Set `defaultValue` (or `value` + `onValueChange`) to an item's `value`.
 */
export function RadioGroup<Value = string>({
  variant = "plain",
  label,
  description,
  className,
  children,
  ...props
}: RadioGroupProps<Value>) {
  const id = useId();
  const labelId = `${id}-label`;
  const descriptionId = `${id}-description`;
  return (
    <VariantContext.Provider value={variant}>
      <BaseRadioGroup<Value>
        data-variant={variant}
        aria-labelledby={label != null ? labelId : undefined}
        aria-describedby={description != null ? descriptionId : undefined}
        className={withBase(cx("grid content-start", variant === "cards" ? "gap-2" : "gap-3"), className)}
        {...props}
      >
        {label != null || description != null ? (
          <div className={cx("grid gap-0.5", variant === "cards" && "mb-1")}>
            {label != null ? (
              <div id={labelId} className="text-sm leading-5 font-medium text-zinc-950 dark:text-white">
                {label}
              </div>
            ) : null}
            {description != null ? (
              <p id={descriptionId} className="text-sm leading-5 text-zinc-500 dark:text-zinc-400">
                {description}
              </p>
            ) : null}
          </div>
        ) : null}
        {children}
      </BaseRadioGroup>
    </VariantContext.Provider>
  );
}

export interface RadioGroupItemProps<Value = string> extends Omit<BaseRadio.Root.Props<Value>, "children"> {
  /** The option's name. */
  label: ReactNode;
  /** A secondary line, linked with `aria-describedby` so it is read after the name. */
  description?: ReactNode;
  /** Cards only: a 16px icon shown in a small tile at the start of the card. */
  icon?: ReactNode;
  /** Cards only: a short value at the end of the card, such as a price or a limit. */
  meta?: ReactNode;
  /** Classes for the wrapping label (the card in the cards variant). `className` styles the radio. */
  containerClassName?: string;
}

const radio = cx(
  "group/radio relative inline-flex size-4 shrink-0 cursor-pointer items-center justify-center rounded-full border outline-none",
  "after:absolute after:-inset-2 after:content-['']",
  "border-zinc-950/25 bg-white shadow-[0_1px_1px_rgb(0_0_0/0.04)] hover:border-zinc-950/40",
  "dark:border-white/25 dark:bg-black dark:shadow-none dark:hover:border-white/40",
  "data-[checked]:border-zinc-950 data-[checked]:bg-zinc-950 dark:data-[checked]:border-white dark:data-[checked]:bg-white",
  "data-[invalid]:border-red-500 dark:data-[invalid]:border-red-400",
  "transition-[background-color,border-color] duration-150 ease-out motion-reduce:transition-none",
  "data-[disabled]:cursor-not-allowed",
);

/** One option. In the cards variant the whole card is the click target and shows the focus ring. */
export function RadioGroupItem<Value = string>({
  label,
  description,
  icon,
  meta,
  containerClassName,
  className,
  "aria-describedby": describedBy,
  ...props
}: RadioGroupItemProps<Value>) {
  const variant = useContext(VariantContext);
  const id = useId();
  const labelId = `${id}-label`;
  const descriptionId = `${id}-description`;
  const cards = variant === "cards";

  const control = (
    <BaseRadio.Root<Value>
      aria-labelledby={labelId}
      aria-describedby={cx(description != null && descriptionId, describedBy) || undefined}
      className={withBase(
        cx(
          radio,
          cards ? "focus-visible:outline-none" : "focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500",
        ),
        className,
      )}
      {...props}
    >
      <BaseRadio.Indicator
        keepMounted
        className={cx(
          "size-1.5 rounded-full bg-white dark:bg-black",
          "scale-50 opacity-0 transition-[scale,opacity] duration-100 ease-out motion-reduce:transition-none",
          "data-[checked]:scale-100 data-[checked]:opacity-100 data-[checked]:duration-200 data-[checked]:ease-[cubic-bezier(0.22,1,0.36,1)]",
        )}
      />
    </BaseRadio.Root>
  );

  if (!cards) {
    return (
      <label
        className={cx(
          "group/item grid cursor-pointer grid-cols-[auto_1fr] items-start gap-x-2.5 gap-y-0.5 has-[[data-disabled]]:cursor-not-allowed",
          containerClassName,
        )}
      >
        <span className="flex h-5 items-center">{control}</span>
        <span id={labelId} className="text-sm leading-5 text-zinc-950 select-none group-has-[[data-disabled]]/item:opacity-50 dark:text-white">
          {label}
        </span>
        {description != null ? (
          <span
            id={descriptionId}
            className="col-start-2 text-sm leading-5 text-zinc-500 group-has-[[data-disabled]]/item:opacity-50 dark:text-zinc-400"
          >
            {description}
          </span>
        ) : null}
      </label>
    );
  }

  return (
    <label
      className={cx(
        "group/card relative flex cursor-pointer items-start gap-3 rounded-xl border p-3.5 select-none",
        "border-zinc-950/10 bg-white shadow-[0_1px_2px_rgb(0_0_0/0.04)] hover:border-zinc-950/20",
        "dark:border-white/10 dark:bg-black dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)] dark:hover:border-white/20",
        // Selected: the border thickens to 2px (an inset ring, so nothing shifts) and the card takes the selected tint.
        "has-[[data-checked]]:border-zinc-950 has-[[data-checked]]:bg-zinc-950/[0.02] has-[[data-checked]]:shadow-[inset_0_0_0_1px_rgb(9_9_11)]",
        "dark:has-[[data-checked]]:border-white dark:has-[[data-checked]]:bg-white/[0.04] dark:has-[[data-checked]]:shadow-[inset_0_0_0_1px_rgb(255_255_255)]",
        "transition-[background-color,border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none",
        "has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-solid has-[:focus-visible]:outline-offset-2 has-[:focus-visible]:outline-sky-500",
        "has-[[data-disabled]]:cursor-not-allowed has-[[data-disabled]]:opacity-50 has-[[data-disabled]]:hover:border-zinc-950/10 dark:has-[[data-disabled]]:hover:border-white/10",
        containerClassName,
      )}
    >
      {icon != null ? (
        <span
          aria-hidden="true"
          className={cx(
            "flex size-8 shrink-0 items-center justify-center rounded-lg border border-zinc-950/10 text-zinc-600 dark:border-white/10 dark:text-zinc-400",
            "transition-colors duration-150 ease-out motion-reduce:transition-none",
            "group-has-[[data-checked]]/card:text-zinc-950 dark:group-has-[[data-checked]]/card:text-white [&_svg]:size-4",
          )}
        >
          {icon}
        </span>
      ) : null}
      <span className="grid min-w-0 flex-1 gap-0.5">
        <span id={labelId} className="text-sm leading-5 font-medium text-zinc-950 dark:text-white">
          {label}
        </span>
        {description != null ? (
          <span id={descriptionId} className="text-sm leading-5 text-zinc-500 dark:text-zinc-400">
            {description}
          </span>
        ) : null}
      </span>
      {meta != null ? (
        <span className="shrink-0 text-sm leading-5 text-zinc-600 tabular-nums dark:text-zinc-400">{meta}</span>
      ) : null}
      <span className="flex h-5 shrink-0 items-center">{control}</span>
    </label>
  );
}