# Segmented Control

## Usage

`SegmentedControl` lays a small set of mutually exclusive options out in one row, with exactly one of them selected at all times. It looks like a tab bar but behaves like a radio group: the choice is a value or a mode, not a page. Because every option is visible at once, the user can compare them without opening anything first.

```vue
<script setup lang="ts">
import { SegmentedControl, SegmentedControlItem } from '@astryx-vue/core/SegmentedControl'
import { ref } from 'vue'

const view = ref('overview')
</script>

<template>
  <SegmentedControl v-model="view" label="Workspace view">
    <SegmentedControlItem label="Overview" value="overview" />
    <SegmentedControlItem label="Activity" value="activity" />
    <SegmentedControlItem label="Members" value="members" />
  </SegmentedControl>
</template>
```

The selection is bound two ways with `v-model`. The control renders as a `role="radiogroup"` container, and `label` goes to `aria-label` — it names the group for screen readers and is never drawn on screen. Each `SegmentedControlItem` renders as a `role="radio"` button whose `aria-checked` reports whether it is the selected one.

Keyboard behavior follows the radio group convention: the left and right arrows move between segments, `Home` and `End` jump to the first and last enabled segment, and movement wraps at the ends. Selection follows focus, so the segment the arrow keys land on becomes the selected one. Only one segment is in the tab order at a time (a roving tabindex), so Tab moves past the whole control instead of stopping on every segment. Disabled segments are skipped by both the arrow keys and the tab order.

`size` offers `sm`, `md`, and `lg`, defaulting to `md`. `layout` defaults to `hug`, where the control and each segment take their content width and never exceed the container: when the container is too narrow the labels truncate with an ellipsis instead of pushing the control past the edge. Switch it to `fill` and the segments share the full width evenly, which suits a narrow card.

`isDisabled` disables the whole control: no segment can be selected and the control leaves the tab order. When you also need to say why, use `disabledMessage` rather than wrapping the control in a `Tooltip` — a disabled control swallows the pointer events an external tooltip needs. With a message the control stays reachable through `aria-disabled`, so both mouse and keyboard users can discover the reason, while selection stays blocked. To disable one segment on its own, set `isDisabled` on that `SegmentedControlItem`.

A segment can show an icon before its label by putting it in the `#icon` slot; the icon follows the size of the control. Set `isLabelHidden` to show the icon alone, and the `label` becomes the segment's `aria-label` so its name is not lost.

### Anatomy

- **Control**: Container for the mutually exclusive segment choices.
- **Segment**: Individual choice within the control.
- **Label**: Visible text identifying a segment when its label is not hidden.
- **Icon**: Optional caller-supplied icon shown inside a segment.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Use it to switch between two to five mutually exclusive views or modes where every option should be visible at once. |
| Do | Always set `label` to say in a sentence what the group controls, such as "Workspace view". Screen reader users need it to know what they are choosing between. |
| Do | Keep the labels short. Labels truncate in narrow containers, and two or three words stay readable on a phone. |
| Do | Explain a disabled control with `disabledMessage`, so keyboard users can read the reason too. |
| Don't | Use it for page-level navigation. That is what `TabList` is for: a tab list switches a content region, while a segmented control is an input that always has exactly one selected option. |
| Don't | Use it for a simple on/off state. A switch is toggled independently, whereas a segmented control enforces one selection from a group. |
| Don't | Wrap a disabled segmented control in a `Tooltip`. A disabled control does not emit the pointer events the wrapper listens for; use `disabledMessage` instead. |
| Don't | Cram in more than five segments, or mix labels of very different lengths. The more options there are, the more the user has to read before choosing. |

## Examples

### Disabled item

Segmented control with an individually disabled option for unavailable choices.

```vue
<script setup lang="ts">
import { SegmentedControl, SegmentedControlItem } from '@astryx-vue/core/SegmentedControl'
import { ref } from 'vue'

const value = ref('hourly')
</script>

<template>
  <SegmentedControl v-model="value" label="Data granularity">
    <SegmentedControlItem value="hourly" label="Hourly" />
    <SegmentedControlItem value="daily" label="Daily" />
    <SegmentedControlItem value="weekly" label="Weekly" is-disabled />
  </SegmentedControl>
</template>
```

### Fill layout

Segmented control that stretches segments equally to fill the available width, useful for fixed-width containers.

```vue
<script setup lang="ts">
import { SegmentedControl, SegmentedControlItem } from '@astryx-vue/core/SegmentedControl'
import { ref } from 'vue'

const value = ref('weekly')
</script>

<template>
  <div style="width: 400px">
    <SegmentedControl v-model="value" label="Time range" layout="fill">
      <SegmentedControlItem value="daily" label="Daily" />
      <SegmentedControlItem value="weekly" label="Weekly" />
      <SegmentedControlItem value="monthly" label="Monthly" />
    </SegmentedControl>
  </div>
</template>
```

### Icon only

Compact segmented control with hidden labels, showing only icons for space-constrained layouts.

```vue
<script setup lang="ts">
import { Icon } from '@astryx-vue/core/Icon'
import { SegmentedControl, SegmentedControlItem } from '@astryx-vue/core/SegmentedControl'
import { ref } from 'vue'

const value = ref('grid')
</script>

<template>
  <SegmentedControl v-model="value" label="View mode" size="sm">
    <SegmentedControlItem value="grid" label="Grid" is-label-hidden>
      <template #icon>
        <Icon icon="i-heroicons-squares-2x2" />
      </template>
    </SegmentedControlItem>
    <SegmentedControlItem value="list" label="List" is-label-hidden>
      <template #icon>
        <Icon icon="i-heroicons-list-bullet" />
      </template>
    </SegmentedControlItem>
  </SegmentedControl>
</template>
```

### With icons

Segmented control with icon and label pairs for a view mode switcher.

```vue
<script setup lang="ts">
import { Icon } from '@astryx-vue/core/Icon'
import { SegmentedControl, SegmentedControlItem } from '@astryx-vue/core/SegmentedControl'
import { ref } from 'vue'

const value = ref('grid')
</script>

<template>
  <SegmentedControl v-model="value" label="View mode">
    <SegmentedControlItem value="grid" label="Grid">
      <template #icon>
        <Icon icon="i-heroicons-squares-2x2" />
      </template>
    </SegmentedControlItem>
    <SegmentedControlItem value="list" label="List">
      <template #icon>
        <Icon icon="i-heroicons-list-bullet" />
      </template>
    </SegmentedControlItem>
    <SegmentedControlItem value="table" label="Table">
      <template #icon>
        <Icon icon="i-heroicons-table-cells" />
      </template>
    </SegmentedControlItem>
  </SegmentedControl>
</template>
```

## SegmentedControl Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `modelValue` | `string` | - | The selected segment's value. Required, and bound with `v-model`. The segment whose `value` matches it is the selected one. |
| `label` | `string` | - | Accessible name for the radio group, applied as `aria-label`. Never rendered visually. Required. |
| `size` | `SegmentedControlSize` | `'md'` | Size of the control, which sets the height of every segment. Optional: `sm`, `md`, `lg`. |
| `layout` | `SegmentedControlLayout` | `'hug'` | How the segments spend the available width. `hug` sizes to content, capped at the container so narrow labels truncate; `fill` stretches the segments equally across the container. |
| `isDisabled` | `boolean` | `false` | Whether the entire control is disabled. No segment can be selected, and the control leaves the tab order. |
| `disabledMessage` | `string` | - | Why the control is disabled, shown in a tooltip on hover and on keyboard focus. Together with `isDisabled` the control keeps `aria-disabled` instead of dropping out of the tab order, so the reason reaches keyboard and assistive technology while selection stays blocked. Use it instead of wrapping a disabled control in a Tooltip. |

## SegmentedControl events

| Name | Description |
| --- | --- |
| `update:modelValue` | Fired when the selection changes, backing the `v-model` binding. Clicking the already selected segment does not fire it. |
| `change` | Fired when the selection changes, for work that follows a switch such as loading data. |

## SegmentedControl Slots

| Name | Description |
| --- | --- |
| `default` | The `SegmentedControlItem` children, in the order they appear. |

## SegmentedControlItem Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | - | Unique value for this segment, matched against the control's `v-model`. Required. |
| `label` | `string` | - | The segment's name, rendered as its visible text and used as the accessible name when the label is hidden. Required. |
| `isLabelHidden` | `boolean` | `false` | Whether the label is visually hidden, leaving only the icon. The label still becomes `aria-label`. |
| `isDisabled` | `boolean` | `false` | Whether this single segment is disabled. It keeps its place in the row but cannot be selected, and the arrow keys and tab order skip it. |

## SegmentedControlItem Slots

| Name | Description |
| --- | --- |
| `icon` | Artwork shown before the label, usually an icon class or your own SVG. It is hidden from assistive technology, since the segment already carries a name. |

## Native attributes

The root element is a `div`, and each segment is a `button`.

The following go to the root element:

- HTML attributes.
- `aria-*` attributes (<code>aria-label</code>, <code>aria-disabled</code>, and <code>aria-describedby</code> are derived from `label` and `isDisabled` and cannot be overridden).
- `data-*` attributes.
- `class`.
- `style`.

A `class` on `SegmentedControlItem` is merged with the component's own class names. `role`, `aria-checked`, `aria-disabled`, `aria-label` (written only when `isLabelHidden` is set), `tabindex`, and `data-value` are computed by the component and override native attributes of the same name.

## Accessibility

### Text label

- Criterion: 1.4.3 Contrast (Minimum)
- Check: 4.5:1
- States: Rest, Hover, Pointer down, Selected

Each label must have at least 4.5:1 contrast with its segment background. Check unselected, Hover, Pointer down, and selected colors as they appear on screen. For Hover and Pointer down, measure the final background after the overlay is applied.

### Essential icon

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Icon only

When a segment has no visible label, its icon must have at least 3:1 contrast with the segment background. An icon beside a visible label does not need its own check.

### Selected state indicator

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1 if relied upon
- States: Selected

The selected background must reach 3:1 only when users need it to tell selected from unselected. Label color and weight also show selection.

### Visible control boundary

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1 if needed
- States: Rest

The control edge or segment borders need at least 3:1 contrast when users need them to see the choices.

### Keyboard focus indicator

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Focus visible

The focus outline must have at least 3:1 contrast with the area around the segment. Check it on the track and selected background.

### Disabled appearance

- Criterion: 1.4.3 and 1.4.11 exceptions
- Check: Not required
- States: Disabled

Disabled controls do not need to meet these contrast ratios.
