Skip to content
Thrum

Radio group

One choice from a small set, with every option in view: a refresh rate, a plan, a unit. Use it for two to five options that need full words. Short options that switch a view fit a segmented control; six or more fit a select; options that need a description each fit choice cards.

Install

npx shadcn@latest add https://thrumui.dev/r/radio-group.json

Adds components/ui/radio-group.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { RadioGroup } from "@/components/ui/radio-group";

Props

PropTypeDefaultDescription
RadioGroup: valuestring–The selected value, when the parent holds it. Use it with onValueChange.
RadioGroup: defaultValuestring–The value selected at first, when the group holds it itself. Without it nothing is selected.
RadioGroup: onValueChange(value: string) => void–Called with the new value when another option is picked.
RadioGroup: orientation"vertical" | "horizontal"Default "vertical"vertical stacks the options 8 pixels apart. horizontal sets them in a row that wraps. The arrow keys work the same in both.
RadioGroup: namestring–The field name when the group sits in a form. The selected value is submitted under it.
RadioGroup: disabledbooleanDefault falseEvery option is dimmed and cannot be picked.
RadioGroup: readOnlybooleanDefault falseThe selection stays focusable but cannot be changed.
RadioGroup: requiredbooleanDefault falseOne option must be picked before the form submits.
RadioGroup: soundbooleanDefault truePlay a soft click when the selection changes, if the visitor has sound on.
RadioGroup: classNamestring–Extra classes for the group, merged last. Set a width here.
RadioGroupLabelComponentProps<"div">–The group's visible name. The group points at it with aria-labelledby by itself.
RadioGroupItem: valuestringRequiredWhat onValueChange reports. Unique within the group.
RadioGroupItem: disabledbooleanDefault falseShown dimmed. It cannot be picked and the arrow keys skip it.
RadioGroupItem: size"md" | "lg"Default "md"md is a 16 pixel mark, lg is 20 pixels. Both are a 24 pixel target.
RadioGroupItem: childrenReactNode–The option's text. It sits in a label with the mark, so a press on it picks the option.
RadioGroupItem: classNamestring–Extra classes for the option's label row, merged last.

Motion and sound

Motion

  • The dot fades in and grows from half size over 150 ms while the ring takes the primary colour. The dot of the option left behind fades out the same way.
  • A second pick during the change reverses it from where it is.
  • Reduced motion: the dot and the ring change at once.

Sound

  • A soft click with a short tone when the selection changes. Picking the selected option again is silent.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • It follows the WAI-ARIA radio group pattern: the group is a radiogroup named by RadioGroupLabel or an aria-label, and each mark is a radio with aria-checked.
  • Each option is named by its own label text. Pressing the text picks the option.
  • The picked option carries a dot as well as a coloured ring, so selection never rests on colour.
  • A hidden native radio carries the value in a form.
  • The ring keeps 3 to 1 contrast against the page in both themes.
  • Long labels wrap beside their mark. Nothing overflows at 160 pixels wide.
KeysAction
TabFocus the selected option, or the first one when nothing is selected. The group is one tab stop.
↓ / →Move to the next option and select it. Disabled options are skipped, and the last wraps to the first.
↑ / ←Move to the previous option and select it.
SpaceSelect the focused option, if it is not selected yet.

Source

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

import { createContext, useContext, useId, type ComponentProps } from "react";
import { Radio as RadioPrimitive } from "@base-ui/react/radio";
import { RadioGroup as RadioGroupPrimitive } from "@base-ui/react/radio-group";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";

/** The id of the group's label, so the label part and the group find each other. */
const LabelId = createContext<string | undefined>(undefined);

export type RadioGroupProps = RadioGroupPrimitive.Props<string> & {
  /** `vertical` stacks the options, `horizontal` sets them in a row that wraps. */
  orientation?: "vertical" | "horizontal";
  /** Play a soft click when the selection changes, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * One choice from a small set, with every option in view. Each `RadioGroupItem` is one option; values are strings.
 * Name the group with `RadioGroupLabel` or an `aria-label`. From about six options up, a select takes less room.
 */
export function RadioGroup({ orientation = "vertical", sound = true, className, onValueChange, ...props }: RadioGroupProps) {
  const labelId = useId();
  return (
    <LabelId value={labelId}>
      <RadioGroupPrimitive
        data-slot="radio-group"
        data-orientation={orientation}
        // An `aria-label` names the group by itself; otherwise the label part does.
        aria-labelledby={props["aria-label"] ? undefined : labelId}
        onValueChange={(value, details) => {
          if (sound) play("select");
          onValueChange?.(value, details);
        }}
        className={cn("flex min-w-0 gap-2 text-sm", orientation === "horizontal" ? "flex-wrap items-start gap-x-4" : "flex-col items-start", className)}
        {...props}
      />
    </LabelId>
  );
}

/** The group's visible name. It is a `<div>`: every option is named by its own label. */
export function RadioGroupLabel({ className, ...props }: ComponentProps<"div">) {
  const id = useContext(LabelId);
  return <div data-slot="radio-group-label" id={id} className={cn("w-full font-medium", className)} {...props} />;
}

export type RadioGroupItemProps = RadioPrimitive.Root.Props<string> & {
  /** `md` is a 16 pixel mark, `lg` is 20. Both are a 24 pixel target. */
  size?: "md" | "lg";
};

/** One option: a radio mark and its text in a `<label>`, so a press on the text picks it too. */
export function RadioGroupItem({ size = "md", className, children, ...props }: RadioGroupItemProps) {
  return (
    <label data-slot="radio-group-item" className={cn("flex min-w-0 cursor-pointer items-start gap-2 has-data-disabled:cursor-not-allowed has-data-disabled:opacity-50", className)}>
      <RadioPrimitive.Root
        data-slot="radio"
        data-surface="control"
        className={cn(
          // The pseudo-element keeps both sizes a 24 pixel target. The 16 pixel mark sits level with the first line of 14 pixel text.
          "relative inline-flex shrink-0 items-center justify-center rounded-full border border-input bg-background after:absolute dark:border-foreground/35",
          "transition-[border-color] duration-150 ease-out motion-reduce:transition-none",
          "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
          "aria-invalid:border-destructive data-checked:border-primary",
          size === "lg" ? "size-5 after:-inset-0.5" : "mt-0.5 size-4 after:-inset-1",
        )}
        {...props}
      >
        {/* The dot marks the picked option, so selection never rests on the border colour. Kept in the page so it can fade out too. */}
        <RadioPrimitive.Indicator
          keepMounted
          className={cn(
            "rounded-full bg-primary transition-[opacity,scale] duration-150 ease-out data-unchecked:scale-50 data-unchecked:opacity-0 motion-reduce:transition-none",
            size === "lg" ? "size-2.5" : "size-2",
          )}
        />
      </RadioPrimitive.Root>
      <span className="min-w-0">{children}</span>
    </label>
  );
}

Files and dependencies

Adds
components/ui/radio-group.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engine

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor