# Segmented

> Segmented control for React and shadcn: a thumb glides to the chosen option, with radio semantics that submit with a form.

A single-choice segmented control whose thumb glides to the selection on a spring. Base UI RadioGroup behaviour, concentric thumb, labels that never change weight.

- Tier: Free (MIT)
- Interaction: Slide
- Page: https://eased.dev/components/segmented
- Installs: @base-ui/react
- Registry: https://eased.dev/r/segmented.json

## Install

```bash
npx shadcn add https://eased.dev/r/segmented.json
```

A single-choice control with a gliding thumb, on Base UI's RadioGroup: one tab stop, arrow keys move the selection, Home/End, and form submission through `name`.

## When to use

- Picking one of two to five short options that change a value: a date range, a view density, a billing period.
- Filters and settings where every option should stay visible, so people see what they are not choosing.
- Inside forms: it submits its value through `name` like a radio group.

## When not to use

- Swapping regions of content: use [Tabs](/components/tabs), which wire up panels.
- More than five options, or long labels: use a [Radio Group](/components/radio-group) or a select.
- Options that can be on at the same time: use [Toggle Group](/components/toggle-group).

## Usage

```tsx date-range.tsx
import { useState } from "react";
import { Segmented } from "@/components/eased/segmented/segmented";

export function DateRange() {
  const [range, setRange] = useState("30d");
  return (
    <Segmented
      aria-label="Date range"
      options={[
        { value: "7d", label: "7 days" },
        { value: "30d", label: "30 days" },
        { value: "12m", label: "12 mo" },
      ]}
      value={range}
      onValueChange={setRange}
    />
  );
}
```

## Examples

### Icon-only segments

Each icon-only segment needs its own `aria-label`.

```tsx
<Segmented
  aria-label="Layout"
  options={[
    { value: "list", icon: <ListIcon />, "aria-label": "List" },
    { value: "grid", icon: <GridIcon />, "aria-label": "Grid" },
  ]}
  defaultValue="list"
/>
```

### Full width in a form

`fullWidth` splits the container equally; `size="md"` matches 36px form fields.

```tsx
<Segmented name="billing" size="md" fullWidth aria-label="Billing period" options={periods} defaultValue="yearly" />
```

## API reference

| Prop | Type | Default | Description |
|---|---|---|---|
| `options` | `{ value, label?, icon?, "aria-label"?, disabled? }[]` | | Icon-only segments need an `aria-label`. |
| `value` / `defaultValue` | `V` | first option | Controlled or uncontrolled. |
| `onValueChange` | `(value: V) => void` | | |
| `size` | `"sm" \| "md"` | `"sm"` | 32px for dense UI and headers, 36px for forms and settings. |
| `fullWidth` | `boolean` | `false` | Fill the container and split it equally. |
| `name` | `string` | | Submits the value with a form. |
| `disabled` | `boolean` | | |
| `aria-label` / `aria-labelledby` | `string` | | Name the group. |

## Keyboard

| Key | Action |
|---|---|
| `Tab` | Moves focus to the selected segment; the whole control is one tab stop. |
| `←` / `→` | Selects the previous or next segment. |
| `Home` / `End` | Selects the first or last segment. |

## Accessibility

- Base UI renders a `radiogroup` of `radio`s, so screen readers announce the group's name, the option and its position.
- Name the group with `aria-label`, or `aria-labelledby` pointing at a visible label.
- Labels only darken when selected; they never change weight, so the segments keep their width.

## Motion

The thumb glides to the new segment on the snappy spring and stays concentric with the track: its radius is the track's minus the padding and border. Reduced motion jumps the thumb to the new segment with no glide.

## Notes for AI

Use Segmented for a short, visible single choice that sets a value, controlled with `value` and `onValueChange` or submitted with `name`. Keep labels to one or two words; switch to `size="md"` beside form fields.

Do not use it to switch views (that is Tabs), and do not add your own thumb, indicator or selected-weight styles: the component draws the thumb and keeps it concentric.

## Related

- [Tabs](/components/tabs): switches a region of content.
- [Toggle Group](/components/toggle-group): several options on at once.
- [Radio Group](/components/radio-group): longer lists and labels.

## Example

`components/eased/segmented/segmented-demo.tsx`

```tsx
"use client";

import { motion } from "motion/react";
import { useState } from "react";
import { Badge } from "../badge/badge";
import { DemoFrame } from "../lib/demo-frame";
import { smooth, useSafeReducedMotion } from "../lib/motion";
import { NumberRoll } from "../number-roll/number-roll";
import { TextSwap } from "../text-swap/text-swap";
import { Segmented } from "./segmented";

type Range = "24h" | "7d" | "30d" | "12m";
type Metric = "visitors" | "views";

const RANGES = [
  { value: "24h", label: "24h" },
  { value: "7d", label: "7 days" },
  { value: "30d", label: "30 days" },
  { value: "12m", label: "12 mo" },
] as const;

const METRICS = [
  { value: "visitors", label: "Visitors" },
  { value: "views", label: "Views" },
] as const;

// Twelve buckets per range, as a share of the range's busiest bucket.
const DATA: Record<Range, { total: number; delta: number; bars: number[]; axis: [string, string] }> = {
  "24h": { total: 3_842, delta: 0.081, bars: [18, 12, 9, 14, 38, 62, 81, 100, 92, 74, 55, 31], axis: ["00:00", "22:00"] },
  "7d": { total: 26_190, delta: -0.034, bars: [72, 80, 77, 91, 100, 46, 39, 70, 84, 88, 95, 52], axis: ["Oct 6", "Oct 17"] },
  "30d": { total: 118_604, delta: 0.124, bars: [41, 48, 52, 50, 61, 66, 63, 72, 79, 85, 93, 100], axis: ["Sep 18", "Oct 17"] },
  "12m": { total: 1_204_330, delta: 0.468, bars: [22, 26, 31, 30, 38, 44, 52, 57, 63, 74, 88, 100], axis: ["Nov", "Oct"] },
};

const pct = new Intl.NumberFormat("en-US", { style: "percent", maximumFractionDigits: 1, signDisplay: "always" });

export function SegmentedDemo() {
  const reduced = useSafeReducedMotion();
  const [range, setRange] = useState<Range>("7d");
  const [metric, setMetric] = useState<Metric>("visitors");
  const data = DATA[range];
  const scale = metric === "views" ? 2.7 : 1;
  const total = Math.round(data.total * scale);

  return (
    <DemoFrame
      title="Traffic"
      actions={<Segmented aria-label="Metric" options={METRICS} value={metric} onValueChange={setMetric} />}
    >
      <div className="flex min-h-0 flex-1 flex-col gap-5 p-4">
        <Segmented aria-label="Date range" options={RANGES} value={range} onValueChange={setRange} fullWidth />

        <div className="flex items-baseline justify-between gap-3">
          <div className="min-w-0">
            <p className="text-ui-sm text-text-muted">
              <TextSwap>{metric === "views" ? "Page views" : "Unique visitors"}</TextSwap>
            </p>
            <p className="text-3xl font-semibold text-text tabular-nums">
              <NumberRoll value={total} locale="en-US" />
            </p>
          </div>
          <Badge tone={data.delta < 0 ? "danger" : "success"}>
            <TextSwap>{pct.format(data.delta)}</TextSwap>
          </Badge>
        </div>

        <div className="flex min-h-0 flex-1 flex-col gap-2">
          <div className="flex min-h-0 flex-1 items-end gap-1.5" aria-hidden>
            {data.bars.map((value, i) => (
              <motion.div
                key={i}
                initial={false}
                // clip-path, not scaleY, so the rounded top never squashes. 4px is --eased-radius-control-2xs:
                // Motion cannot interpolate a var() inside clip-path.
                animate={{ clipPath: `inset(${100 - value}% 0 0 0 round 4px 4px 0 0)` }}
                transition={reduced ? { duration: 0 } : smooth}
                className={`h-full flex-1 ${i === data.bars.length - 1 ? "bg-brand" : "bg-surface-active"}`}
              />
            ))}
          </div>
          <div className="flex justify-between text-ui-xs text-text-muted tabular-nums">
            <TextSwap>{data.axis[0]}</TextSwap>
            <TextSwap>{data.axis[1]}</TextSwap>
          </div>
        </div>
      </div>
    </DemoFrame>
  );
}
```
