# Segmented Control

One choice from a few, as joined segments: Day · Week · Month. Built on ui-sdk's SegmentedControl, a radio group underneath: Tab reaches the chosen segment and the arrow keys move the choice. For switching between views with icons, use ViewSwitch.

- Beta: ready to use; its API can still change in a minor release.
- Category: Selection

```tsx
import { SegmentedControl, SegmentedControlItem } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it for one choice from a few short options seen side by side, like Day, Week and Month on a calendar, or a range on a report.
- Do: Name it with `aria-label`, like Range. Tab reaches the chosen segment and the arrow keys move the choice.
- Do: Use `md` in forms and dialogs, `sm` in toolbars and `xs` in section headers.
- Do: Put a count after a segment's label as a child when it helps the choice, like open tasks per status.
- Don't: Use it to switch between views of the same data, like an opportunities list and board. Use `ViewSwitch`, which shows the views as icons.
- Don't: Use it for more options than fit on one line at narrow widths. Segments don't wrap or shrink; use `Select`.

## Keyboard

| Keys | What they do |
| --- | --- |
| Tab | Lands on the checked item, and the next Tab leaves. |
| Arrow keys | Check the next item, skipping disabled ones and wrapping. |

## Related

- [View Switch](./view-switch.md)
- [Tabs](./tabs.md)
- [Radio](./radio.md)
- [Button Group](./button-group.md)

## Examples

What the examples import:

```tsx
import { Calendar, CalendarDays, CalendarRange } from "lucide-react";
import { Fragment } from "react";
import { SegmentedControl, SegmentedControlItem } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const sizes = ["md", "sm", "xs"] as const;

const ranges = ["Day", "Week", "Month"] as const;
```

### Sizes

md 36 for forms and dialogs, sm 32 for toolbars, xs 28 for section headers. Hover a segment, Tab in and use the arrow keys.

```tsx
<div className="grid grid-cols-[auto_auto] items-center justify-start justify-items-start gap-x-6 gap-y-4">
  {sizes.map(size => (
    <Fragment key={size}>
      <span className="text-small text-neutral-text-muted">{size}</span>
      <SegmentedControl
        defaultValue="Week"
        aria-label={`Range, ${size}`}
        size={size}
      >
        {ranges.map(range => (
          <SegmentedControlItem key={range} value={range}>
            {range}
          </SegmentedControlItem>
        ))}
      </SegmentedControl>
    </Fragment>
  ))}
</div>
```

### With icons

An icon leads the label: 16 px at md, 14 px at sm and xs.

```tsx
<SegmentedControl defaultValue="day" aria-label="Calendar range">
  <SegmentedControlItem value="day" icon={<Calendar />}>
    Day
  </SegmentedControlItem>
  <SegmentedControlItem value="week" icon={<CalendarRange />}>
    Week
  </SegmentedControlItem>
  <SegmentedControlItem value="month" icon={<CalendarDays />}>
    Month
  </SegmentedControlItem>
</SegmentedControl>
```

### Disabled

A whole control, or one segment, can be disabled. A disabled segment is skipped by the arrow keys.

```tsx
<div className="flex flex-col items-start gap-4">
  <SegmentedControl
    defaultValue="Week"
    aria-label="Range, disabled"
    disabled
  >
    {ranges.map(range => (
      <SegmentedControlItem key={range} value={range}>
        {range}
      </SegmentedControlItem>
    ))}
  </SegmentedControl>
  <SegmentedControl defaultValue="Day" aria-label="Range, one disabled">
    {ranges.map(range => (
      <SegmentedControlItem
        key={range}
        value={range}
        disabled={range === "Month"}
      >
        {range}
      </SegmentedControlItem>
    ))}
  </SegmentedControl>
</div>
```

## Props

SegmentedControl also takes ui-sdk's value, defaultValue, onValueChange, disabled, readOnly, required, name, form, loop and orientation. SegmentedControlItem takes value and disabled.

### SegmentedControl

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `"xs" \| "sm" \| "md"` | `"md"` | md 36 (forms, dialogs) by default · sm 32 (toolbars) · xs 28 (section headers). |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### SegmentedControlItem

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` |  | What it shows inside. |
| `icon` | `ReactNode` |  | A leading icon: 16 px at md, 14 px at sm and xs. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
