# View Switch

Switches between views of the same data, like list and board, with icons only. Built on ui-sdk's SegmentedControl: Tab reaches the chosen view and the arrow keys move it, and the white segment slides across. For choices in forms and filters, use SegmentedControl.

- Beta: ready to use; its API can still change in a minor release.
- Category: Selection

```tsx
import { ViewSwitch } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it to switch between views of the same data, like opportunities as a list or a board, or a chart as bars or a pie.
- Do: Give every option a `label` that names the view, like List or Board. The segment shows only the icon, so the label is its accessible name.
- Do: Name the whole switch with `aria-label`, like View, and use `xs` in section headers and `sm` everywhere else.
- Don't: Use it for choices in forms and filters, like open or closed matters. Use `SegmentedControl`, whose segments carry text.

## Keyboard

| Keys | What they do |
| --- | --- |
| Tab | Lands on the chosen view, and the next Tab leaves. |
| Arrow keys | Choose the next view, wrapping at the ends. |

## Related

- [Segmented Control](./segmented-control.md)
- [Tabs](./tabs.md)

## Examples

What the examples import:

```tsx
import { ChartBar, ChartPie, SquareKanban, TableOfContents } from "lucide-react";
import { ViewSwitch } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const views = [
  { value: "list", label: "List", icon: <TableOfContents /> },
  { value: "board", label: "Board", icon: <SquareKanban /> },
];
```

### Sizes

sm (32 px, 26 px segments, 16 px icons) by default; xs (28, 22, 14) in section headers. Click the other view to watch the white segment slide, or Tab in and use the arrow keys: the whole switch shows focus.

```tsx
<div className="flex items-center gap-6">
  <ViewSwitch
    options={views}
    defaultValue="list"
    aria-label="Opportunities view"
  />
  <ViewSwitch
    options={views}
    defaultValue="board"
    aria-label="Tasks view"
    size="xs"
  />
</div>
```

### More views

Any number of views: here a chart switches between bar and pie.

```tsx
<ViewSwitch
  options={[
    { value: "bar", label: "Bar chart", icon: <ChartBar /> },
    { value: "pie", label: "Pie chart", icon: <ChartPie /> },
    { value: "table", label: "Table", icon: <TableOfContents /> },
  ]}
  defaultValue="bar"
  aria-label="Chart type"
/>
```

### Disabled

```tsx
<ViewSwitch
  options={views}
  defaultValue="list"
  aria-label="View, disabled"
  disabled
/>
```

## Props

Also takes ui-sdk's SegmentedControl props: value, defaultValue, onValueChange, disabled, readOnly, name and loop.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `options` (required) | `ViewSwitchOption<T>[]` |  | The views: value, label (its accessible name) and icon. |
| `size` | `"sm" \| "xs"` | `"sm"` | sm (32 px) by default; xs (28 px) in section headers. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
