Cómo funciona
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.
Úsalo para
- Switching the range or mode of one view: Day, Week, Month.
- Two to five short, mutually exclusive options that should all stay visible.
Busca otra opción cuando
- Switching between panels of different content: use tabs.
- Long labels or more than five options: use a select.
Uso
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' },
]}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
options | { value, label, icon?, disabled? }[] | — | The segments, two to five work best. |
value | string | — | Controlled: the chosen value. |
defaultValue | string | first enabled option | Uncontrolled: the initial value. |
onChange | (value: string) => void | — | Called when the choice changes. |
size | 'sm' | 'md' | 'lg' | 'md' | Track height 38, 42 or 50 px. |
fullWidth | boolean | false | Stretches to the container and splits the width equally. |
label | string | — | Accessible name of the radio group. |
disabled | boolean | false | Disables the whole control. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root. |
Accesibilidad
- 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.
Movimiento
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.
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.
SegmentedControl.jsxsegmented-control.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.