Forms

Radio

16px radio buttons in a labelled group.

react-aria-components

Preview

Send emails from

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components
  2. Create a radio.tsx file and paste the following code into it.

    src/components/ui/radio.tsx
    "use client";
    
    import type { ReactNode } from "react";
    import {
      Radio as AriaRadio,
      RadioGroup as AriaRadioGroup,
      type RadioGroupProps as AriaRadioGroupProps,
      type RadioProps as AriaRadioProps,
    } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { Label } from "./field";
    
    export interface RadioGroupProps extends Omit<AriaRadioGroupProps, "children" | "className"> {
      label?: string;
      children: ReactNode;
      className?: string;
    }
    
    export function RadioGroup({ label, children, className, ...props }: RadioGroupProps) {
      return (
        <AriaRadioGroup {...props} className={cx("flex flex-col gap-2", className)}>
          {label && <Label>{label}</Label>}
          <div className="flex flex-col gap-2.5 group-data-[orientation=horizontal]:flex-row">{children}</div>
        </AriaRadioGroup>
      );
    }
    
    /** 16px radio. Selected state shows a 6px white dot on accent. */
    export function Radio({ children, className, ...props }: AriaRadioProps & { children?: ReactNode; className?: string }) {
      return (
        <AriaRadio {...props} className={cx("group flex cursor-default items-center gap-2 text-base text-fg data-disabled:text-fg-disabled", className)}>
          <span
            className={cx(
              "flex size-4 shrink-0 items-center justify-center rounded-full border border-line-strong bg-surface transition-colors",
              "group-data-hovered:border-fg-disabled group-data-focus-visible:shadow-focus",
              "group-data-selected:border-accent group-data-selected:bg-accent",
              "group-data-disabled:border-line group-data-disabled:bg-muted",
            )}
          >
            <span className="size-1.5 scale-0 rounded-full bg-white transition-transform group-data-selected:scale-100" />
          </span>
          {children}
        </AriaRadio>
      );
    }
  3. Create a field.tsx file and paste the following code into it.

    src/components/ui/field.tsx
    "use client";
    
    import type { ReactNode } from "react";
    import { FieldError, Label as AriaLabel, Text } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    /** Shared field chrome: 40px tall, radius 12, 1px border-strong, accent border + 3px ring on focus. */
    export const fieldBase =
      "w-full rounded-md border border-line-strong bg-surface text-base text-fg outline-none transition-[border,box-shadow] " +
      "placeholder:text-fg-muted data-hovered:border-fg-disabled " +
      "data-focused:border-accent data-focused:shadow-focus data-focus-within:border-accent data-focus-within:shadow-focus " +
      "data-invalid:border-error data-invalid:data-focused:shadow-focus-error " +
      "data-disabled:border-line data-disabled:bg-subtle data-disabled:text-fg-disabled data-disabled:placeholder:text-fg-disabled";
    
    export function Label({ className, children }: { className?: string; children: ReactNode }) {
      return <AriaLabel className={cx("text-sm font-medium text-fg", className)}>{children}</AriaLabel>;
    }
    
    export function Description({ children }: { children: ReactNode }) {
      return (
        <Text slot="description" className="text-sm text-fg-muted">
          {children}
        </Text>
      );
    }
    
    export function ErrorMessage({ children }: { children?: ReactNode }) {
      return <FieldError className="text-sm text-error-text">{children}</FieldError>;
    }
  4. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.