# Segmented control

> A compact single-choice control for switching between a few views or modes.

Use a segmented control to switch between two to five short options that change a view in place, such as Monthly and Yearly prices or List and Board. For a choice inside a form with longer labels, use a [radio group](/docs/components/radio); for switching between panels of content, use [tabs](/docs/components/tabs).

Pro example source is available to licensed users. See [Pro access](/docs/pro/access).

## Installation

Pro components install from the Aloxe registry with the shadcn CLI. Once per project, add the registry to `components.json` and put a [registry token](/account/tokens) in the `ALOXE_TOKEN` environment variable. [Registry](/docs/pro/registry) has the details.

```json
{
  "registries": {
    "@aloxe": {
      "url": "https://<aloxe-site>/r/{name}.json",
      "headers": { "Authorization": "Bearer ${ALOXE_TOKEN}" }
    }
  }
}
```

Then add the component:

```bash
npx shadcn@latest add @aloxe/segmented-control
```

The CLI copies the source into `components/ui/aloxe/`, so it never overwrites your own `ui` components. It also installs `@aloxe/react`, `@aloxe/tokens` and `@base-ui/react`.

Import the tokens once in your global stylesheet, after Tailwind, and add an `@source` for the copied files and one for `@aloxe/react`, so Tailwind generates their classes. `@source` paths are relative to the stylesheet; these assume `src/app/globals.css` and `src/components/ui/aloxe/`.

```css
@import "tailwindcss";
@import "@aloxe/tokens/aloxe.css";
@import "@aloxe/tokens/tailwind.css";
@source "../components/ui/aloxe";
@source "../../node_modules/@aloxe/react/dist";
```

[Installation](/docs/getting-started/installation) covers the theme and brand attributes.

## Usage

```tsx
import { SegmentedControl, Segment } from "@/components/ui/aloxe/segmented-control";

<SegmentedControl aria-label="Billing period" defaultValue="yearly" name="billing">
  <Segment value="monthly">Monthly</Segment>
  <Segment value="yearly">Yearly</Segment>
</SegmentedControl>
```

The control needs a name: `aria-label`, or `aria-labelledby` pointing at a visible label. TypeScript requires one of the two. Keep each label to one or two words.

## Examples

### Controlled, with icons and a visible label

Pass `value` and `onValueChange`; the value type follows your segments. Icons go before the label at 16 px.

Pro example source is available to licensed users. See [Pro access](/docs/pro/access).

### Disabled

A `disabled` segment is dimmed and skipped by the arrow keys. `disabled` on the control dims every segment.

Pro example source is available to licensed users. See [Pro access](/docs/pro/access).

## Accessibility

| Key | Action |
| --- | --- |
| Tab | Moves focus into the control, onto the selected segment, and out again. |
| Right and Down | Selects the next segment, wrapping at the end. |
| Left and Up | Selects the previous segment, wrapping at the start. |

- Follows the WAI-ARIA radio group pattern: the control is a `radiogroup` and each segment is a `radio`, so screen readers announce "2 of 3, selected".
- The selected segment differs by its raised surface, its shadow in light mode and a hairline ring in dark mode, not by colour alone.
- Segments are 28 px tall in a 32 px track. For touch-first layouts, stretch the control with `className="w-full"` so each segment is wide enough to tap.

## API reference

Every part accepts `className` and renders a `data-aloxe` attribute you can target in CSS, such as `[data-aloxe="segment"]`.

**SegmentedControl** (extends Base UI RadioGroup)

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `aria-label` | `string` |  |  | Names the radio group. Required unless you pass aria-labelledby. |
| `aria-labelledby` | `string` |  |  | The id of a visible label. Required unless you pass aria-label. |
| `value` | `Value` |  |  | Controlled value. Pair with onValueChange. |
| `defaultValue` | `Value` |  |  | Initial value when uncontrolled. |
| `onValueChange` | `(value: Value, details) => void` |  |  | Called when the user picks a segment. The value type follows your segments. |
| `name` | `string` |  |  | Form field name. The selected value is submitted with the form. |
| `disabled` | `boolean` | `false` |  | Disables every segment. |
| `className` | `string` |  |  | Classes for the track, such as w-full to stretch the segments. |

**Segment** (extends Base UI Radio.Root)

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `value` | `Value` |  | Yes | The value this segment selects. |
| `children` | `ReactNode` |  | Yes | The label, one or two words. An icon may go before it. |
| `disabled` | `boolean` | `false` |  | Dims the segment. The arrow keys skip it. |
| `className` | `string` |  |  | Merged with the default classes. |

## Figma

Matches the **Segmented control** and **Segment** components in the Pro Figma file. Segment `State=Selected` is the control's `value`; `Disabled` maps to `disabled`.

See [Figma](/docs/getting-started/figma) for where to get the file and how its names map to code.

## Pro

Segmented control is part of Aloxe Pro. The previews on this page are open to everyone; the Code tab shows the example source once you sign in with a Pro license. See [Access and licenses](/docs/pro/access) and [Pricing](/pricing#pro).
