# Segmented buttons: accessibility

### Keyboard

Tab reaches every segment. Try a single choice, and a price filter where several can stay selected.

| On | Keys | What it does |
|---|---|---|
| Segments | `Tab` `Shift+Tab` | Goes to the next or the previous segment, including across segmented buttons. |
| Segments | `Left` `Right` `Up` `Down` | Leaves the focus on the same segment: the arrow keys do not move between segments, as M3 asks. |
| Single-select segment | `Enter` `Space` | Selects it and clears the other choice. |
| Multi-select segment | `Enter` `Space` | Selects it, or clears it when it is selected; the others keep their state. |

### Focus

Focus follows the segments’ order in both reading directions. Tab does not reach the segmented button itself.

### Screen readers

What the browser tells assistive technology of the example above. How a screen reader says it is its own choice.

| Part | Role | Name | State |
|---|---|---|---|
| Segmented button | `group` | `ariaLabel` |  |
| Segment | `button` | Its text, or `ariaLabel` | Pressed or not pressed |

### Touch targets

Each segment can be pressed over its own box only: 40px tall at the default density, 36 and 32px at comfortable and compact, under the 48px M3 asks for.

### Display

- A selected segment shows a check mark as well as its color, so color is not the only sign of selection.

### Best practices

Name the segmented button for the choice it holds, and keep each segment’s text short.

Vanilla:

```js
import { createSegmentedButton } from 'material/components/segmented-button';

const segmentedButton = createSegmentedButton({
  ariaLabel: 'Places',
  segments: [
    { text: 'Restaurants', value: 'restaurants', selected: true },
    { text: 'Bars', value: 'bars' },
  ],
});
document.body.append(segmentedButton.element);
```

Web Components:

```html
<m-segmented-button value="restaurants" aria-label="Places">
  <m-segment value="restaurants">Restaurants</m-segment>
  <m-segment value="bars">Bars</m-segment>
</m-segmented-button>
```

React:

```tsx
import { useState } from 'react';
import { SegmentedButton, Segment } from 'material/react/segmented-button';

export function Example() {
  const [value, setValue] = useState("restaurants");
  return (
    <SegmentedButton value={value} onChange={(event) => setValue(event.detail.value)} ariaLabel="Places">
      <Segment value="restaurants">Restaurants</Segment>
      <Segment value="bars">Bars</Segment>
    </SegmentedButton>
  );
}
```

Vue:

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { MSegmentedButton, MSegment } from 'material/vue/segmented-button';

const value = ref("restaurants");
</script>

<template>
  <MSegmentedButton v-model="value" aria-label="Places">
    <MSegment value="restaurants">Restaurants</MSegment>
    <MSegment value="bars">Bars</MSegment>
  </MSegmentedButton>
</template>
```

Svelte:

```svelte
<script lang="ts">
  import SegmentedButton from 'material/svelte/segmented-button';

  let value = $state("restaurants");
</script>

<SegmentedButton bind:value ariaLabel="Places">
  <m-segment value="restaurants">Restaurants</m-segment>
  <m-segment value="bars">Bars</m-segment>
</SegmentedButton>
```

Solid:

```tsx
import { createSignal } from 'solid-js';
import { SegmentedButton, Segment } from 'material/solid/segmented-button';

export function Example() {
  const [value, setValue] = createSignal("restaurants");
  return (
    <SegmentedButton value={value()} onChange={(event) => setValue(event.detail.value)} ariaLabel="Places">
      <Segment value="restaurants">Restaurants</Segment>
      <Segment value="bars">Bars</Segment>
    </SegmentedButton>
  );
}
```

- Give the segmented button `ariaLabel`: its group is otherwise named Segmented button.
- Give an icon-only segment `ariaLabel` in the factory; the web component cannot name one yet.
- For a new design, prefer the connected button group, which M3 Expressive recommends in its place.

### Known gaps

- Every segment is a Tab stop and the arrow keys do nothing; M3 asks for one stop and the arrows between segments. [#834](https://github.com/floor/material/issues/834)
- Segments are pressed buttons; M3 asks for radios in a single-select segmented button and checkboxes in a multi-select one. [#835](https://github.com/floor/material/issues/835)
- Segments have no 48px touch target. [#836](https://github.com/floor/material/issues/836)
- `<m-segment>` has no `aria-label`, so an icon-only segment of the web component has no name. [#832](https://github.com/floor/material/issues/832)
