Glrk UI

Radio

A wrapper for Base UI Radio and RadioGroup with label/description support, controlled state, and orientation control.

RadioWrapper
options — with descriptions and disabled
value + onValueChange — controlled

Selected: email

orientation: horizontal
Primitives
RadioGroup + Radio — manual composition

Installation

npx shadcn@latest add @glrk-ui/radio

If you haven't set up the prerequisites yet, check out Prerequest section.

Copy and paste the following code into your project.

ui/radio.tsx
'use client'

import * as React from 'react'
import { RadioGroup as RadioGroupPrimitive } from '@base-ui/react/radio-group'
import { Radio as RadioPrimitive } from '@base-ui/react/radio'

import { cn, getKey, getLabel, getValue } from '@/lib/utils'

function RadioIndicator({ className, ...props }: RadioPrimitive.Root.Props) {
  return (
    <RadioPrimitive.Root
      data-slot="radio-group-item"
      className={cn(
        'group/radio-group-item peer relative flex aspect-square size-4.5 shrink-0 rounded-full border border-input outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 aria-invalid:aria-checked:border-primary dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 data-checked:border-primary data-checked:bg-primary data-checked:text-primary-foreground dark:data-checked:bg-primary',
        className,
      )}
      {...props}
    >
      <RadioPrimitive.Indicator
        data-slot="radio-group-indicator"
        className="flex size-4 items-center justify-center"
      >
        <span className="absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2 rounded-full bg-primary-foreground" />
      </RadioPrimitive.Indicator>
    </RadioPrimitive.Root>
  )
}

type RadioProps = {
  label: React.ReactNode
  description?: React.ReactNode
  wrapperCls?: string
  as?: React.ElementType
} & RadioPrimitive.Root.Props

function Radio({ label, description, wrapperCls, className, as: Comp = 'label', ...props }: RadioProps) {
  return (
    <Comp className={cn('flex cursor-pointer select-none items-start gap-2 has-[:disabled]:cursor-not-allowed', wrapperCls)}>
      <RadioIndicator className={cn(className)} {...props} />
      <p className="grid">
        <span className="text-sm font-medium leading-none">{label}</span>
        {description && <span className="mt-0.5 text-xs text-muted-foreground">{description}</span>}
      </p>
    </Comp>
  )
}

function RadioGroup({ className, ...props }: RadioGroupPrimitive.Props) {
  return (
    <RadioGroupPrimitive
      data-slot="radio-group"
      className={cn('flex flex-col gap-2', className)}
      {...props}
    />
  )
}

type radioOptionT = allowedPrimitiveT | (optionT & { description?: React.ReactNode })

type RadioWrapperProps = {
  options: radioOptionT[]
  orientation?: 'horizontal' | 'vertical'
  itemCls?: string
  as?: React.ElementType
} & Omit<RadioGroupPrimitive.Props, 'children'>

function RadioWrapper({
  options,
  orientation = 'vertical',
  itemCls,
  className,
  as,
  ...props
}: RadioWrapperProps) {
  return (
    <RadioGroup
      className={cn(orientation === 'horizontal' && 'flex-row flex-wrap', className)}
      {...props}
    >
      {options.map((opt, i) => (
        <Radio
          key={getKey(opt, i)}
          value={String(getValue(opt))}
          label={getLabel(opt)}
          description={typeof opt === 'object' ? opt.description : undefined}
          disabled={typeof opt === 'object' ? opt.disabled : undefined}
          wrapperCls={itemCls}
          as={as}
        />
      ))}
    </RadioGroup>
  )
}

export {
  RadioIndicator,
  Radio,
  RadioGroup,
  RadioWrapper,
  type RadioProps,
  type radioOptionT,
  type RadioWrapperProps,
}

Usage

Group — RadioWrapper

import { RadioWrapper } from "@/components/ui/radio"

<RadioWrapper
  defaultValue="email"
  options={[
    { value: "email", label: "Email", description: "Receive updates via email." },
    { value: "sms", label: "SMS", description: "Receive updates via text." },
    { value: "push", label: "Push notifications" },
    { value: "disabled", label: "Unavailable", disabled: true },
  ]}
/>

Controlled

const [value, setValue] = useState("email")

<RadioWrapper
  value={value}
  onValueChange={(v) => setValue(v as string)}
  options={[
    { value: "email", label: "Email" },
    { value: "sms", label: "SMS" },
    { value: "push", label: "Push" },
  ]}
/>

Horizontal orientation

<RadioWrapper
  defaultValue="light"
  orientation="horizontal"
  options={[
    { value: "light", label: "Light" },
    { value: "dark", label: "Dark" },
    { value: "system", label: "System" },
  ]}
/>

Single radio — Radio

import { RadioGroup, Radio } from "@/components/ui/radio"

<RadioGroup defaultValue="b">
  <Radio value="a" label="Option A" description="First choice." />
  <Radio value="b" label="Option B" description="Second choice." />
  <Radio value="c" label="Disabled" disabled />
</RadioGroup>

Reference

radioOptionT

Prop

Type

Radio

Prop

Type

RadioWrapper

Prop

Type