Skip to content
English

How it works

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.

Use it for

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

Look elsewhere when

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

Usage

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

Props

PropTypeDefaultWhat it does
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.

Accessibility

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

Motion

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.

Add it to your project

Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.

SegmentedControl.jsxsegmented-control.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Navigation