Forms

Radio

Radio pairs a built-in radio control with inline `text` and optional `helpText`. Always render inside `RadioGroup` for accessible single-select behavior.

Installation

npx shadcn add https://rck.vibeboxph.com//r/radio.json

Live preview & controls

Controls
<RadioGroup label="Newsletter" defaultValue="weekly">
  <Radio value="daily" text="Daily digest" />
  <Radio value="weekly" text="Weekly roundup" />
  <Radio value="never" text="No emails" />
</RadioGroup>

Anatomy

  • RadioControlThe bare radio toggle.
  • RadioLabeled option with inline text and help text.

Props

PropTypeDefaultDescription
value*stringOption value within the parent RadioGroup.
text*React.ReactNodeInline label beside the radio control.
helpTextReact.ReactNodeOption-level help text.
controlPosition"start" | "end"startInline-start/end placement.
disabledbooleanDisables this option.

Accessibility

  • Must be used inside `RadioGroup` which provides `role="radiogroup"` and arrow-key navigation.

Source

registry/rck/ui/radio.tsx
"use client";

import * as React from "react";
import * as RadioGroupPrimitive from "@/registry/rck/primitives/radio-group";
import { Circle } from "@/lib/icons";
import { cn } from "@/lib/utils";
import { BooleanFieldLayout, type ControlPosition } from "@/registry/rck/ui/field-chrome";

export type RadioControlProps = React.ComponentPropsWithoutRef<
  typeof RadioGroupPrimitive.Item
>;

/** Bare radio control — compose with `Radio` inside `RadioGroup`. */
export const RadioControl = React.forwardRef<
  React.ComponentRef<typeof RadioGroupPrimitive.Item>,
  RadioControlProps
>(({ className, ...props }, ref) => (
  <RadioGroupPrimitive.Item
    ref={ref}
    data-slot="radio-control"
    className={cn(
      "group border-border-strong bg-background aspect-square size-4 shrink-0 rounded-full border shadow-sm " +
        "focus-visible:ring-ring transition-colors focus-visible:ring-2 focus-visible:outline-none " +
        "focus-visible:ring-offset-background focus-visible:ring-offset-2 " +
        "disabled:cursor-not-allowed disabled:opacity-50 " +
        "data-[state=checked]:border-primary data-[state=checked]:text-primary",
      className,
    )}
    {...props}
  >
    <RadioGroupPrimitive.Indicator
      data-slot="radio-indicator"
      className="flex items-center justify-center"
    >
      <Circle className="size-2 fill-current text-current" aria-hidden="true" />
    </RadioGroupPrimitive.Indicator>
  </RadioGroupPrimitive.Item>
));
RadioControl.displayName = "RadioControl";

export function Radio({
  className,
  text,
  helpText,
  controlPosition = "start",
  id,
  disabled,
  ...props
}: RadioControlProps & {
  text: React.ReactNode;
  helpText?: React.ReactNode;
  controlPosition?: ControlPosition;
  id?: string;
}) {
  return (
    <BooleanFieldLayout
      text={text}
      helpText={helpText}
      controlPosition={controlPosition}
      disabled={disabled}
      id={id}
      className={className}
    >
      {({ id: fieldId, describedBy, disabled: fieldDisabled }) => (
        <RadioControl
          id={fieldId}
          disabled={fieldDisabled}
          aria-describedby={describedBy}
          {...props}
        />
      )}
    </BooleanFieldLayout>
  );
}

/** @deprecated Use `RadioControl` instead. */
export const RadioGroupItem = RadioControl;

/** @deprecated Use `Radio` instead. */
export const RadioGroupField = Radio;
registry/rck/ui/field-chrome.tsx
import * as React from "react";
import { cva, type VariantProps } from "@/lib/cva";
import { cn } from "@/lib/utils";

export const fieldWrapperVariants = cva(
  "flex w-full items-center gap-2 rounded-md border bg-background px-3 shadow-sm " +
    "transition-colors has-[input:disabled]:cursor-not-allowed has-[input:disabled]:opacity-50 " +
    "has-[textarea:disabled]:cursor-not-allowed has-[textarea:disabled]:opacity-50",
  {
    variants: {
      size: {
        sm: "h-8 text-[10px]",
        md: "h-[39px] text-[11px]",
        lg: "h-11 text-xs",
      },
      invalid: {
        true: "border-destructive focus-within:ring-2 focus-within:ring-destructive/40",
        false:
          "border-border-strong focus-within:border-primary focus-within:ring-2 focus-within:ring-ring/30",
      },
    },
    defaultVariants: {
      size: "md",
      invalid: false,
    },
  },
);

/** Built-in field label — shared by Input, Textarea, and boolean field wrappers. */
export function FieldLabel({
  className,
  required,
  children,
  ...props
}: React.LabelHTMLAttributes<HTMLLabelElement> & { required?: boolean }) {
  return (
    <label
      data-slot="field-label"
      className={cn("text-foreground text-sm leading-none font-medium", className)}
      {...props}
    >
      {children}
      {required && <span className="text-destructive ml-0.5">*</span>}
    </label>
  );
}

export function FieldMessage({
  className,
  invalid,
  ...props
}: React.HTMLAttributes<HTMLParagraphElement> & { invalid?: boolean }) {
  return (
    <p
      data-slot="field-message"
      className={cn(
        "text-xs",
        invalid ? "text-destructive" : "text-muted-foreground",
        className,
      )}
      {...props}
    />
  );
}

export type ControlPosition = "start" | "end";

export interface BooleanGroupOption {
  value: string;
  /** Inline label beside the control. */
  text: string;
  disabled?: boolean;
  helpText?: React.ReactNode;
}

export interface BooleanFieldLayoutProps {
  label?: React.ReactNode;
  text?: React.ReactNode;
  helpText?: React.ReactNode;
  error?: React.ReactNode;
  required?: boolean;
  /** Places the control at inline-start (default) or inline-end — flips automatically in RTL. */
  controlPosition?: ControlPosition;
  disabled?: boolean;
  id?: string;
  className?: string;
  containerClassName?: string;
  children: (args: {
    id: string;
    describedBy?: string;
    disabled?: boolean;
  }) => React.ReactNode;
}

export function BooleanFieldLayout({
  label,
  text,
  helpText,
  error,
  required,
  controlPosition = "start",
  disabled,
  id: providedId,
  className,
  containerClassName,
  children,
}: BooleanFieldLayoutProps) {
  const generatedId = React.useId();
  const id = providedId ?? generatedId;
  const helpTextId = helpText && !error ? `${id}-help-text` : undefined;
  const errorId = error ? `${id}-error` : undefined;
  const describedBy = cn(helpTextId, errorId) || undefined;
  const control = children({ id, describedBy, disabled });

  return (
    <div
      data-slot="boolean-field"
      className={cn("flex flex-col gap-1.5", containerClassName)}
    >
      {label && (
        <FieldLabel htmlFor={id} required={required}>
          {label}
        </FieldLabel>
      )}
      <div
        className={cn(
          "flex w-full items-start gap-2",
          controlPosition === "end" && "flex-row-reverse justify-between",
          className,
        )}
      >
        {control}
        {text && (
          <label
            htmlFor={id}
            className={cn(
              "text-foreground text-sm leading-none font-medium",
              controlPosition === "end" && "flex-1",
              disabled && "cursor-not-allowed opacity-50",
            )}
          >
            {text}
          </label>
        )}
      </div>
      {helpText && !error && <FieldMessage id={helpTextId}>{helpText}</FieldMessage>}
      {error && (
        <FieldMessage id={errorId} invalid role="alert">
          {error}
        </FieldMessage>
      )}
    </div>
  );
}

export type FieldWrapperVariantProps = VariantProps<typeof fieldWrapperVariants>;

Related examples

Browse the full examples gallery