Ir al contenido
Español

Cómo funciona

Renders mutually exclusive options from an options array, controlled or uncontrolled. Each option is a native radio that fills the option transparently and shares one name, so Tab, arrow keys and screen reader positions are the browser's own; no keyboard code is written. Selecting only changes colour and inset shadows, so sizes never move. Dot style: the ring fills with the accent and a white dot springs in. Card style: filled --ds-surface-2 tiles with no outline; the selected tile turns accent-soft with a 1.5px accent ring and keeps the radio mark (top-right in a horizontal row), so selection is not colour alone.

Úsalo para

  • Choosing one of 2 to 6 exclusive options that should all be visible: payment method, delivery speed, invoice type.
  • Options that carry a description or price and deserve more room: use card style for plans or shipping.
  • Form fields that only apply on submit; use a segmented control when a click switches a view at once.

Busca otra opción cuando

  • Several answers allowed: use checkboxes.
  • More than 6 options or very little room: use a select.
  • A plain on/off with instant effect: use a switch.
  • Switching page content rather than filling a form: use tabs.

Uso

import { useState } from 'react'
import { RadioGroup } from './RadioGroup.jsx'

const SPEEDS = [
  { value: 'standard', label: 'Standard', description: '3–5 days · $4' },
  { value: 'express', label: 'Express', description: '1–2 days · $9' },
  { value: 'same', label: 'Same day', description: 'Today · $15' },
]

// Card style, controlled: delivery speed
export function SpeedPicker() {
  const [speed, setSpeed] = useState('express')
  return <RadioGroup label="Delivery speed" variant="card" direction="horizontal" options={SPEEDS} value={speed} onChange={setSpeed} />
}

// Dot style, uncontrolled: submits pay=card with the form
export function PayMethod() {
  return (
    <RadioGroup
      label="Payment method"
      name="pay"
      defaultValue="balance"
      required
      options={[
        { value: 'balance', label: 'Balance', description: '$86.50 available' },
        { value: 'card', label: 'Card' },
        { value: 'bank', label: 'Bank transfer', description: 'Under maintenance', disabled: true },
      ]}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
options{ label, value, description?, disabled? }[]—The options; value is a string or a number.
valuestring | number | null—Controlled selected value; null means controlled with nothing selected.
defaultValuestring | number—Initially selected value when uncontrolled; omit for none.
onChange(value) => void—Called when the selection changes, by click or arrow key.
variant'dot' | 'card''dot'Dot style (ring + text) or card style (filled tile, whole tile clickable; selected turns accent-soft with an accent ring). In a horizontal row of cards the mark sits in the top-right corner.
direction'vertical' | 'horizontal''vertical'Vertical or horizontal; dot style wraps when it does not fit, cards share the row equally.
size'sm' | 'md' | 'lg''md'Ring 18 / 20 / 24px, text 13 / 14 / 16px, card padding 10 / 14 / 16px and radius 12 / 14 / 16px. Use lg on mobile.
disabledbooleanfalseDisables the whole group; to disable one option, set it in options.
invalidbooleanfalseError state: unselected rings (card rings and a faint tint in card style) turn danger-colored, and the root gets aria-invalid.
requiredbooleanfalseRequired: set on the native radios and aria-required, so the browser blocks submit.
namestring—Form field name; generated when omitted, so the group stays mutually exclusive and arrow keys work.
labelstring—Accessible name of the group; with a visible heading, pass aria-labelledby instead.
...restHTMLAttributes<HTMLDivElement>—Other attributes (className, style, aria-*) go to the root <div role="radiogroup">.

Accesibilidad

  • The root is role="radiogroup" named by label or aria-labelledby; each option is a native radio, so screen readers announce position such as "2 of 3".
  • Keyboard is native: Tab lands on the selected option (the first when none), arrow keys move and select, disabled options are skipped.
  • Label and description are linked with aria-labelledby and aria-describedby; clicking the text or anywhere on a card selects it.
  • A soft focus halo grows around the ring (dot style) or the whole card; forced-colors mode uses borders, outlines and the system highlight color.
  • Selection is not color alone: the ring fills and a dot appears, and card tiles gain a ring; errors add aria-invalid. On touch, options are at least 44px tall.

Movimiento

The ring fills in 160ms, then the white dot springs from 0 to 1 in 380ms (40ms later) and leaves in 160ms. Pressing dips the ring to 0.86 and it springs back on release; a card presses to 0.98. Tile fills change in 160ms and the focus halo grows in over 220ms.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

RadioGroup.jsxradio-group.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Formularios
Cuadrícula de disponibilidad viene con Pro. La vista previa aparece al desbloquearlo.
Operaciones booleanas viene con Pro. La vista previa aparece al desbloquearlo.
Selector en cascada viene con Pro. La vista previa aparece al desbloquearlo.