# Toggle and segmented control

> A pressed/unpressed button, and a segmented control for a few mutually exclusive modes.

Category: Actions · Atomic level: atom · Built on Base UI ToggleGroup
Page: https://ui.dtheo.dev/docs/components/toggle

## Install

```bash
npx dtheo@latest add toggle
# or, with the shadcn CLI
npx shadcn@latest add https://ui.dtheo.dev/r/toggle.json
```

Dependencies: @base-ui/react, clsx, tailwind-merge. Registry dependencies: utils.

## Exports

`Toggle`, `SegmentedControl`, `SegmentedControlItem`

## Use it for

- Switching a view or time window (400 ms / 1 s)
- Turning a display option on in a toolbar

## Reach for something else

- Settings that persist: use Switch
- More than five options: use Select

## Keyboard

| Keys | Action |
| --- | --- |
| Arrow keys | Move between segments |
| Enter / Space | Selects the focused segment |

## Accessibility

- Give the group an `aria-label` that names what it switches ("Time window").

## Examples

### segmented-control

```tsx
import * as React from 'react';
import { SegmentedControl, SegmentedControlItem } from '@/components/ui/toggle-group';

const windows = [
  { value: '100ms', label: '100 ms', summary: '0–100 ms: rises to 90 % of the setpoint in 38 ms.' },
  { value: '400ms', label: '400 ms', summary: '0–400 ms: peaks 17.6 % over at 61 ms, settles by 182 ms.' },
  { value: '1s', label: '1 s', summary: '0–1 s: holds inside the ±2 % band after 182 ms.' },
  { value: 'all', label: 'All', summary: '0–2.5 s: steady-state error 0.3 %, no drift.' },
];

export default function SegmentedControlExample() {
  const [value, setValue] = React.useState(['400ms']);
  const current = windows.find((w) => w.value === value[0]) ?? windows[1]!;
  return (
    <div className="flex w-full max-w-md flex-col gap-3">
      <div className="flex flex-wrap items-center justify-between gap-3">
        <div className="flex flex-col">
          <span className="text-headline">Step response</span>
          <span className="text-caption text-muted-foreground">R-0412 · Bench 3</span>
        </div>
        <SegmentedControl
          aria-label="Time window"
          value={value}
          // Clicking the pressed segment would empty the group; a time window always has one value.
          onValueChange={(next) => next.length && setValue(next)}
        >
          {windows.map((w) => (
            <SegmentedControlItem key={w.value} value={w.value}>
              {w.label}
            </SegmentedControlItem>
          ))}
        </SegmentedControl>
      </div>
      <p className="rounded-card border border-border bg-background px-3 py-2.5 text-body text-muted-foreground tabular-nums">
        {current.summary}
      </p>
    </div>
  );
}
```

## Used in

- Run review (page): https://ui.dtheo.dev/examples/run-review
- Data table pattern (template): https://ui.dtheo.dev/docs/patterns/data-table
