本文へ移動
日本語

仕組み

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.

向いている場面

  • 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.

向かない場面

  • 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.

使い方

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 },
      ]}
    />
  )
}

プロパティ

プロパティ型既定値説明
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">.

アクセシビリティ

  • 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.

モーション

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.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

RadioGroup.jsxradio-group.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。