# Tabs

Tabs organize content into separate views where only one view is visible at a time. Users switch between views by selecting tab buttons, enabling efficient navigation within related content sections without leaving the current page. Built on Joy UI's Tabs, TabList, Tab, and TabPanel components, Tabs support horizontal and vertical orientations, multiple visual variants, semantic colors, and icon decorators.

Tabs follow a composition pattern: the `Tabs` container manages state, `TabList` holds the tab buttons, `Tab` is each individual button, and `TabPanel` displays the content for the active tab.

```
<Canvas of={Tabs.Playground} />
```

| Field   | Description | Default   |
| ------- | ----------- | --------- |
| color   | —           | "primary" |
| size    | —           | "md"      |
| variant | —           | "plain"   |

## Usage

```tsx
import { Tabs, TabList, Tab, TabPanel } from '@ceed/cds';

function MyComponent() {
  return (
    <Tabs defaultValue={0}>
      <TabList>
        <Tab>First Tab</Tab>
        <Tab>Second Tab</Tab>
        <Tab>Third Tab</Tab>
      </TabList>
      <TabPanel value={0}>Content for the first tab</TabPanel>
      <TabPanel value={1}>Content for the second tab</TabPanel>
      <TabPanel value={2}>Content for the third tab</TabPanel>
    </Tabs>
  );
}
```

## Features

### Basic Tabs

Default horizontal tab navigation with tab panels.

```tsx
<Tabs {...args}>
    <TabList>
      <Tab color={color} variant={variant}>
        Tab1
      </Tab>
      <Tab color={color} variant={variant}>
        Tab2
      </Tab>
      <Tab color={color} variant={variant}>
        Tab3
      </Tab>
    </TabList>
    <TabPanel value={0}>Tab list1</TabPanel>
    <TabPanel value={1}>Tab list2</TabPanel>
    <TabPanel value={2}>Tab list3</TabPanel>
  </Tabs>
```

### Variants

Tabs support `plain`, `outlined`, `soft`, and `solid` visual styles. Apply the variant to both TabList and individual Tab components for a consistent appearance.

```
<Canvas of={Tabs.Variants} />
```

### Sizes

Tabs come in `sm`, `md`, and `lg` sizes. Set the `size` prop on the Tabs container to affect all child components uniformly.

```
<Canvas of={Tabs.Sizes} />
```

### Colors

Apply semantic colors (`primary`, `neutral`, `success`, `warning`, `danger`) to individual tabs for color-coded navigation.

```
<Canvas of={Tabs.Colors} />
```

### With Decorators

Add icons or other elements before or after the tab label using `startDecorator` and `endDecorator` props.

```tsx
<Tabs {...args}>
    <TabList>
      <Tab startDecorator={<Call />} color={color} variant={variant}>
        Tab1
      </Tab>
      <Tab endDecorator={<Sms />} color={color} variant={variant}>
        Tab2
      </Tab>
      <Tab startDecorator={<Email />} endDecorator={<MoreVert />} color={color} variant={variant}>
        Tab3
      </Tab>
    </TabList>
    <TabPanel value={0}>Tab list1</TabPanel>
    <TabPanel value={1}>Tab list2</TabPanel>
    <TabPanel value={2}>Tab list3</TabPanel>
  </Tabs>
```

### Vertical Orientation

Set `orientation="vertical"` on the Tabs container for sidebar-style navigation layouts.

```
<Canvas of={Tabs.Vertical} />
```

### Disabled Tabs

Individual tabs can be disabled with the `disabled` prop while keeping other tabs interactive.

```
<Canvas of={Tabs.Disabled} />
```

### Controlled Tabs

Use the `value` and `onChange` props for programmatic control of the active tab.

```
<Canvas of={Tabs.Controlled} />
```

## Common Use Cases

### Settings Page

```tsx
function SettingsPage() {
  return (
    <Tabs defaultValue={0}>
      <TabList>
        <Tab startDecorator={<PersonIcon />}>Profile</Tab>
        <Tab startDecorator={<SecurityIcon />}>Security</Tab>
        <Tab startDecorator={<NotificationsIcon />}>Notifications</Tab>
      </TabList>
      <TabPanel value={0}>
        <ProfileSettings />
      </TabPanel>
      <TabPanel value={1}>
        <SecuritySettings />
      </TabPanel>
      <TabPanel value={2}>
        <NotificationSettings />
      </TabPanel>
    </Tabs>
  );
}
```

### Dashboard Time Range

```tsx
function DashboardTabs() {
  const [range, setRange] = useState('week');

  return (
    <Tabs value={range} onChange={(e, val) => setRange(val as string)}>
      <TabList>
        <Tab value="day">Today</Tab>
        <Tab value="week">This Week</Tab>
        <Tab value="month">This Month</Tab>
      </TabList>
      <TabPanel value="day">
        <DailyStats />
      </TabPanel>
      <TabPanel value="week">
        <WeeklyStats />
      </TabPanel>
      <TabPanel value="month">
        <MonthlyStats />
      </TabPanel>
    </Tabs>
  );
}
```

### Vertical Sidebar

```tsx
function SidebarTabs() {
  return (
    <Tabs orientation="vertical" defaultValue={0} sx={{ minWidth: 300 }}>
      <TabList>
        <Tab startDecorator={<DashboardIcon />}>Dashboard</Tab>
        <Tab startDecorator={<AnalyticsIcon />}>Analytics</Tab>
        <Tab startDecorator={<SettingsIcon />}>Settings</Tab>
      </TabList>
      <TabPanel value={0}>Dashboard content</TabPanel>
      <TabPanel value={1}>Analytics content</TabPanel>
      <TabPanel value={2}>Settings content</TabPanel>
    </Tabs>
  );
}
```

## Props and Customization

### Tabs Props

| Prop           | Type                                                           | Default        | Description                       |
| -------------- | -------------------------------------------------------------- | -------------- | --------------------------------- |
| `children`     | `ReactNode`                                                    | -              | TabList and TabPanel elements     |
| `value`        | `string \| number`                                             | -              | Active tab value (controlled)     |
| `defaultValue` | `string \| number`                                             | -              | Initial active tab (uncontrolled) |
| `onChange`     | `(event, value) => void`                                       | -              | Callback when active tab changes  |
| `orientation`  | `'horizontal' \| 'vertical'`                                   | `'horizontal'` | Tab layout direction              |
| `size`         | `'sm' \| 'md' \| 'lg'`                                         | `'md'`         | Tab size                          |
| `variant`      | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                   | `'plain'`      | Visual style                      |
| `color`        | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'` | `'neutral'`    | Color scheme                      |
| `sx`           | `SxProps`                                                      | -              | Custom styles                     |

### Tab Props

| Prop             | Type               | Default | Description             |
| ---------------- | ------------------ | ------- | ----------------------- |
| `value`          | `string \| number` | -       | Tab identifier          |
| `startDecorator` | `ReactNode`        | -       | Content before tab text |
| `endDecorator`   | `ReactNode`        | -       | Content after tab text  |
| `disabled`       | `boolean`          | `false` | Disables the tab        |

> **Note**: Tabs, Tab, TabList, and TabPanel accept all their respective Joy UI props.

## Best Practices

- **Use clear, concise tab labels.** Each label should describe the content it reveals. Avoid generic names like "Tab 1" or "Other".

  ```tsx
  {/* ✅ Good: Descriptive labels */}
  <Tab>Account Settings</Tab>
  <Tab>Notifications</Tab>

  {/* ❌ Bad: Vague labels */}
  <Tab>Tab 1</Tab>
  <Tab>Other</Tab>
  ```

- **Limit the number of tabs.** Aim for 3-6 tabs. More than 6 tabs become difficult to scan and navigate. If you need more sections, consider a sidebar navigation, nested navigation, or a different layout pattern.

- **Order tabs by importance.** Place the most frequently used or most important tab first. Users expect the default (leftmost or topmost) tab to contain the primary content.

- **Do not use Tabs for sequential workflows.** Tabs are for parallel content sections that can be accessed in any order. For ordered multi-step processes, use a Stepper component instead.

  ```tsx
  {/* ❌ Bad: Sequential steps as tabs */}
  <Tab>Step 1: Info</Tab>
  <Tab>Step 2: Payment</Tab>
  <Tab>Step 3: Confirm</Tab>

  {/* ✅ Good: Independent content sections */}
  <Tab>Profile</Tab>
  <Tab>Security</Tab>
  <Tab>Billing</Tab>
  ```

- **Keep styling consistent.** All tabs in a group should share the same variant and color. Mixing styles within the same TabList creates visual inconsistency.

## Accessibility

- **WAI-ARIA Tabs pattern**: TabList receives `role="tablist"`, each Tab receives `role="tab"`, and each TabPanel receives `role="tabpanel"`. The `aria-selected`, `aria-controls`, and `aria-labelledby` attributes are set automatically.
- **Keyboard navigation**: Arrow Left/Right moves between tabs in horizontal orientation. Arrow Up/Down moves between tabs in vertical orientation. Home jumps to the first tab, End to the last. Enter or Space activates the focused tab.
- **Tab key behavior**: Pressing Tab from outside the tab list focuses the active tab. Pressing Tab again moves focus into the active panel content, not to the next tab button.
- **Provide an accessible label**: Use `aria-label` on the Tabs container to give screen readers context for the tab group (e.g., `aria-label="Account settings"`).
