# Segmented control

Picks one option from a small set, and applies it straight away.

```tsx
import { SegmentedControl } from '@halo-ds/react';
```

## When to use it
Use a segmented control to switch how something is shown, or to pick one setting from 2 to 5 options, like Day, Week or Month. Segment is one option, with text or an icon; Segmented control holds them on a grey track in a slot, Hug or Fill.

## Do
- Keep it to 2 to 5 options.
- Use short labels of one word where you can.
- Always have one option Selected.
- Apply the change straight away.

## Don't
- Use it for actions; use Buttons.
- Use it in a form that is sent later; use Radio.
- Mix text and icon-only options.
- Wrap it onto two lines; use a Select instead.

## Segmented control, Tabs or Radio?
All three pick one option. Pick by what changes, and when.
- **Segmented control**: Changes how the same content is shown. Applies straight away. 2 to 5 short options.
- **Tabs or Radio**: Tabs: switch between different content sections. Radio: a choice in a form, sent with the form. Radio also fits longer labels and descriptions.

## Props
### SegmentedControl props
| Prop | Type | What it does |
| --- | --- | --- |
| `options` (required) | `SegmentedOption[]` |  |
| `value` | `string \| undefined` |  |
| `defaultValue` | `string \| undefined` |  |
| `onValueChange` | `((value: string) => void) \| undefined` |  |
| `size` | `"sm" \| "md" \| undefined` | 28 or 36px segments. Figma: Size. |
| `fullWidth` | `boolean \| undefined` | Fit the options, or share the full width equally. Figma: Width. |
| `aria-label` (required) | `string` | Names the group for screen readers, like "View". |
| `className` | `string \| undefined` |  |
| `classNames` | `Partial<Record<SegmentedControlPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<SegmentedControlPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { useState } from 'react';
import { SegmentedControl } from '@halo-ds/react';

export default function Example() {
  const [view, setView] = useState('month');
  return (
    <SegmentedControl
      aria-label="View"
      value={view}
      onValueChange={setView}
      options={[
        { value: 'day', label: 'Day' },
        { value: 'week', label: 'Week' },
        { value: 'month', label: 'Month' },
      ]}
    />
  );
}
```

### Full width
The options share the width, for phones and panels.
```tsx
import { SegmentedControl } from '@halo-ds/react';

export default function Demo() {
  return (
    <div style={{ width: 340 }}>
      <SegmentedControl
        fullWidth
        aria-label="Billing"
        defaultValue="yearly"
        options={[
          { value: 'monthly', label: 'Monthly' },
          { value: 'yearly', label: 'Yearly' },
        ]}
      />
    </div>
  );
}
```

### Icons
Icon-only options need an aria-label each.
```tsx
import { SegmentedControl } from '@halo-ds/react';
import { List, SquaresFour } from '@phosphor-icons/react';

export default function Demo() {
  return (
    <SegmentedControl
      aria-label="Layout"
      defaultValue="list"
      options={[
        { value: 'list', icon: <List />, 'aria-label': 'List view' },
        { value: 'grid', icon: <SquaresFour />, 'aria-label': 'Grid view' },
      ]}
    />
  );
}
```

More: https://halodesignsystem.dev/components/segmented-control
