# Segmented control

A single choice among two to five options: a solid ink thumb that slides between them.

Live variants: https://design.arcadia.is/components/segmented-control

```tsx
import { SegmentedControl } from "@arcadia/ui";
```

## In context

Choose a theme. The pill moves to the selected option.

```tsx
import { useState } from "react";
import { SegmentedControl } from "@arcadia/ui";

function ThemeSetting() {
  const [theme, setTheme] = useState("system");
  return (
    <div {...sx("cmp-setting", cmp.form, cmp.setting, styles.themeSetting)}>
      <span {...sx("cmp-setting-text", cmp.settingText)}>
        <span {...sx("cmp-setting-label", cmp.settingLabel)}>Theme</span>
        <span {...sx("cmp-setting-hint", cmp.settingHint)} aria-live="polite">{THEME_HINTS[theme]}</span>
      </span>
      <SegmentedControl
        aria-label="Theme"
        density="compact"
        items={THEMES}
        value={theme}
        onValueChange={setTheme}
      />
    </div>
  );
}
```

## When to use

- Pick a setting, filter or view from visible choices.
- Add a hint when a choice needs explaining.

## When not to use

- Use `Menu` for more than five choices.
- Use `Tabs` when each option opens a panel.
- Use `Switch` to turn one setting on or off.

## Usage

```tsx
import { SegmentedControl } from "@arcadia/ui";

<SegmentedControl
  aria-label="View"
  defaultValue="list"
  items={[
    { value: "list", label: "List" },
    { value: "grid", label: "Grid" },
  ]}
/>
```

Show two to five choices. Use a menu for longer lists.

Use compact density for Body labels beside leading icons.

Use `labels="chosen"` at the end of a toolbar group.

## Examples

### Only the chosen label

Show the selected label and leave the other choices as icons.

```tsx
import { useState } from "react";
import { SegmentedControl } from "@arcadia/ui";

function Chosen() {
  const [where, setWhere] = useState<"local" | "remote">("local");
  return (
    <SegmentedControl
      size="sm"
      labels="chosen"
      aria-label="Where the run happens"
      items={WHERE}
      value={where}
      onValueChange={setWhere}
    />
  );
}
```

### Icon only

Keep an accessible label even when only the icon shows.

```tsx
import { SegmentedControl } from "@arcadia/ui";

<SegmentedControl
  size="sm"
  aria-label="Theme"
  defaultValue="system"
  items={THEMES.map((t) => ({ ...t, iconOnly: true }))}
/>
```

### A hint per choice

Put a short explanation or shortcut on an unfamiliar choice.

```tsx
import { SegmentedControl } from "@arcadia/ui";

<SegmentedControl aria-label="When you send" defaultValue="queue"
  items={[
    { value: "queue", label: "Queue", hint: "Sent when the agent finishes." },
    { value: "steer", label: "Steer", hint: "Read at the agent's next step, without stopping it." },
  ]} />
```

### Stretch

Give each choice an equal share of the available width.

```tsx
import { SegmentedControl } from "@arcadia/ui";

<SegmentedControl stretch aria-label="Mode" items={MODES} defaultValue="plan" />
```

### Wrapping

Wrap choices onto another row rather than cutting them off.

```tsx
import { SegmentedControl } from "@arcadia/ui";

<div {...sx("cmp-segmented-wrap", styles.narrow)}>
  <SegmentedControl
    wrap size="sm" aria-label="Filter notifications" defaultValue="all"
    items={[
      { value: "all", label: "All" },
      { value: "waiting", label: "Needs you" },
      { value: "direct", label: "For you" },
      { value: "ambient", label: "Away" },
    ]}
  />
</div>
```

### Inside a Settings card

Drop the track outline when the settings card supplies its frame.

```tsx
import { SegmentedControl, SettingsGroup, SettingsRow } from "@arcadia/ui";

<SettingsGroup label="Appearance">
  <SettingsRow label="Theme" control={<SegmentedControl inCard aria-label="Theme" defaultValue="system" items={THEMES} />} />
</SettingsGroup>
```

## Matrix

Kind and size

Rows: Text, Icon and text, Icon only, Disabled item, Disabled. Columns: Small, Medium, Hover and focus.

## Customising

Set layout and override the segmented control’s documented knobs.

```tsx
import * as stylex from "@stylexjs/stylex";

const styles = stylex.create({
  roomy: { "--segmented-pad": "var(--space-1)" },
});

<SegmentedControl items={items} rootStyle={styles.roomy} />
```

- `--segmented-item-h` (`Ref<Control>`, default `var(--control-sm)`)
- `--segmented-item-px` (`Ref<Space>`, default `var(--space-2-5)`)
- `--segmented-pad` (`Ref<NestInset>`, default `var(--space-1)`)

Segmented tabs take these same knobs on their `TabsList`.

## Accessibility

A toggle group: one tab stop, with arrow keys moving between the options. Give the control an `aria-label` (or `aria-labelledby` on a visible label, as in a settings row) so it is announced as "Theme" rather than a bare group. Under forced colours, which drop every fill and shadow, the track keeps its edge in the system's ink and the chosen segment stands on the system's highlight. A segment's height is a least height, and the thumb stands as tall as the segments, so a person's own text spacing grows both instead of clipping a label. In a narrow settings row, or wherever a caller sets `wrap`, choices that do not fit carry onto another line. The selected choice keeps its own highlight there, and the arrow keys still move through the choices in reading order. With `labels="chosen"` each segment's label stays in the accessible name while only the chosen one is drawn, so a listener hears every option and which one is pressed.

- ← + →: Move focus between options
- Enter + Space: Choose the focused option

## API reference

### SegmentedControl

- `items` (`readonly SegmentedItem<V>[]`, required)
- `value` (`V`)
- `defaultValue` (`V`)
- `onValueChange` (`(value: V) => void`)
- `size` (`"sm" | "md"`, default `"md"`)
- `density` (`"standard" | "compact"`)
- `stretch` (`boolean`)
- `labels` (`"all" | "chosen"`, default `"all"`)
- `inCard` (`boolean`, default `false`)
- `wrap` (`boolean`, default `false`)
- `disabled` (`boolean`)
- `rootStyle` (`SegmentedStyle`)

Rest props go to `<div>` props.
