本文へ移動
日本語

仕組み

JS measures the chosen segment's offset and width into --ds-seg-x and --ds-seg-w; the thumb follows with transform and width on a slightly overshooting curve. Each label reserves its bold width with a hidden copy, so segments never shift when the weight changes. Recolour with --ds-seg-thumb and --ds-seg-track.

向いている場面

  • Switching the range or mode of one view: Day, Week, Month.
  • Two to five short, mutually exclusive options that should all stay visible.

向かない場面

  • Switching between panels of different content: use tabs.
  • Long labels or more than five options: use a select.

使い方

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

export function RangePicker() {
  const [range, setRange] = useState('week')
  return (
    <SegmentedControl
      label="Range"
      value={range}
      onChange={setRange}
      fullWidth
      options={[
        { value: 'day', label: 'Day' },
        { value: 'week', label: 'Week' },
        { value: 'month', label: 'Month' },
        { value: 'year', label: 'Year' },
      ]}
    />
  )
}

プロパティ

プロパティ型既定値説明
options{ value, label, icon?, disabled? }[]—The segments, two to five work best.
valuestring—Controlled: the chosen value.
defaultValuestringfirst enabled optionUncontrolled: the initial value.
onChange(value: string) => void—Called when the choice changes.
size'sm' | 'md' | 'lg''md'Track height 38, 42 or 50 px.
fullWidthbooleanfalseStretches to the container and splits the width equally.
labelstring—Accessible name of the radio group.
disabledbooleanfalseDisables the whole control.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root.

アクセシビリティ

  • Role radiogroup with role radio and aria-checked on each segment.
  • Roving tabindex: one Tab stop; arrow keys move and select, skipping disabled segments; Home and End jump to the ends.
  • A visible :focus-visible ring on the focused segment.
  • Segments are at least 32px high; the large size suits touch-first screens.
  • With reduced motion the thumb jumps into place and does not squeeze.

モーション

Thumb transform and width 320ms cubic-bezier(0.32, 1.25, 0.5, 1); press scale 0.95 in 160ms; divider and text colour 160ms.

プロジェクトに追加

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

SegmentedControl.jsxsegmented-control.csstokens.cssREADME.md

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

ナビゲーション