本文へ移動
日本語

仕組み

A recessed track holds the toggles. In single mode one raised thumb is measured under the pressed toggle and glides to the next with a soft spring while the labels only change colour, so nothing reflows. In multiple mode each pressed toggle rises on its own and pressed neighbours drop their inner corners, fusing into one continuous chip that reads as one active span.

向いている場面

  • Text formatting such as bold, italic and underline (multiple).
  • Switching a view mode or alignment among two to five options (single).

向かない場面

  • Switching between page sections: use tabs.
  • Options in a form that is submitted later: use radio buttons or checkboxes.

使い方

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

export function FormatBar() {
  const [marks, setMarks] = useState(['bold'])
  const [view, setView] = useState('edit')
  return (
    <>
      <ToggleGroup
        label="View mode"
        value={view}
        onValueChange={setView}
        items={[
          { value: 'edit', label: 'Edit' },
          { value: 'preview', label: 'Preview' },
        ]}
      />
      <ToggleGroup
        type="multiple"
        iconOnly
        label="Text formatting"
        value={marks}
        onValueChange={setMarks}
        items={[
          { value: 'bold', label: 'Bold', icon: <BoldIcon /> },
          { value: 'italic', label: 'Italic', icon: <ItalicIcon /> },
        ]}
      />
    </>
  )
}

プロパティ

プロパティ型既定値説明
itemsArray<{ value: string, label: string, icon?: ReactNode, disabled?: boolean }>—The toggles. With iconOnly the label becomes the accessible name and tooltip.
type'single' | 'multiple''single'One pressed at a time, or any number.
valuestring | null | string[]—Controlled value: a string for single, an array for multiple.
defaultValuestring | string[]—Initial value when uncontrolled.
onValueChange(value) => void—Called with the new value.
allowEmptybooleanfalseSingle mode: pressing the pressed toggle again clears the choice.
iconOnlybooleanfalseSquare toggles showing only the icon.
size'sm' | 'md' | 'lg''md'Toggle height 32, 36 or 44px.
labelstring—Accessible name of the group.
disabledbooleanfalseDisables every toggle.

アクセシビリティ

  • The container is role="group" with an accessible name from label; every toggle is a native button with aria-pressed.
  • One toggle is in the tab order (roving tabindex); arrow keys move focus and wrap, Home and End jump, disabled toggles are skipped, and left/right follow the reading direction.
  • Space or Enter presses the focused toggle; icon-only toggles get their label as aria-label and title.
  • Pressed state is shown by a raised surface and darker text, not by colour alone; toggles are 32px or taller.
  • Reduced motion stops the thumb gliding and the press scale; only colours change.

モーション

The single-mode thumb glides in 360ms with a soft spring (slight overshoot). Pressing scales a toggle to 96% for 100ms; corners and surfaces change in 160ms.

プロジェクトに追加

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

ToggleGroup.jsxtoggle-group.csstokens.cssREADME.md

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

ボタン
「オーロラ縁ボタン」は Pro に含まれます。解除するとプレビューが表示されます。
「かじりボタン」は Pro に含まれます。解除するとプレビューが表示されます。