# Tabs

> A tab list that switches between related panels in place.

```tsx title="hero.tsx"
import { Tabs, TabsIndicator, TabsList, TabsPanel, TabsTab } from '@langgenius/dify-ui/tabs'

const stats = [
  { label: 'Conversations', value: '1,284' },
  { label: 'Avg. response', value: '1.8 s' },
  { label: 'Satisfaction', value: '92%' },
]

const conversations = [
  { title: 'Refund for an annual plan', time: '2 min ago' },
  { title: 'Change the billing email', time: '18 min ago' },
]

export function TabsHeroDemo() {
  return (
    <Tabs defaultValue="overview" className="w-full max-w-96">
      <TabsList aria-label="App sections" className="border-b border-divider-subtle">
        <TabsTab value="overview">Overview</TabsTab>
        <TabsTab value="logs">Logs</TabsTab>
        <TabsTab value="annotations">Annotations</TabsTab>
        <TabsIndicator />
      </TabsList>
      <TabsPanel value="overview" className="mt-1 rounded-lg px-2 py-3.5">
        <dl className="grid grid-cols-3 gap-3">
          {stats.map((stat) => (
            <div key={stat.label} className="flex min-w-0 flex-col gap-1">
              <dt className="truncate system-xs-regular text-text-tertiary">{stat.label}</dt>
              <dd className="system-xl-semibold text-text-primary tabular-nums">{stat.value}</dd>
            </div>
          ))}
        </dl>
      </TabsPanel>
      <TabsPanel value="logs" className="mt-1 rounded-lg px-2 py-1.5">
        <ul className="flex flex-col">
          {conversations.map((conversation) => (
            <li
              key={conversation.title}
              className="flex items-center justify-between gap-3 py-2 system-sm-regular"
            >
              <span className="truncate text-text-secondary">{conversation.title}</span>
              <span className="shrink-0 system-xs-regular text-text-tertiary">
                {conversation.time}
              </span>
            </li>
          ))}
        </ul>
      </TabsPanel>
      <TabsPanel value="annotations" className="mt-1 flex flex-col gap-1 rounded-lg px-2 py-3.5">
        <p className="system-xl-semibold text-text-primary tabular-nums">38 replies</p>
        <p className="system-xs-regular text-text-tertiary">
          Answer matching questions without calling the model.
        </p>
      </TabsPanel>
    </Tabs>
  )
}
```

## Usage guidelines

- Use `Tabs` when the choice swaps the panel it controls, whatever the control looks like. Use `SegmentedControl` for a mode or filter applied to content that stays in place, and links for navigation between pages.
- Give `TabsList` an accessible name with `aria-label` or `aria-labelledby` when the page has more than one tab list or no heading introduces it. Each `TabsPanel` takes its name from its tab.
- Place `TabsIndicator` inside `TabsList`. It draws the active underline and slides between tabs; tabs do not draw their own. The list has no baseline of its own; add `border-b border-divider-subtle` when it sits directly above its panels.
- Keep tab labels short and on one line. Disable a tab that is temporarily unavailable rather than removing it, so the set of sections stays stable.
- `TabsPanel` has no padding. Add spacing on the panel so its inset focus ring keeps clear of the content.

## Anatomy

```tsx
import { Tabs, TabsIndicator, TabsList, TabsPanel, TabsTab } from '@langgenius/dify-ui/tabs'
```

```tsx
<Tabs>
  <TabsList>
    <TabsTab />
    <TabsIndicator />
  </TabsList>
  <TabsPanel />
</Tabs>
```

## Examples

### Disabled tab

`disabled` on `TabsTab` keeps the tab in the list at reduced opacity. Arrow keys still move focus to it, but it cannot be activated.

```tsx title="disabled.tsx"
import { Tabs, TabsIndicator, TabsList, TabsPanel, TabsTab } from '@langgenius/dify-ui/tabs'

export function TabsDisabledDemo() {
  return (
    <Tabs defaultValue="documents" className="w-full max-w-96">
      <TabsList aria-label="Knowledge base sections" className="border-b border-divider-subtle">
        <TabsTab value="documents">Documents</TabsTab>
        <TabsTab value="retrieval">Retrieval testing</TabsTab>
        <TabsTab value="pipeline" disabled>
          Pipeline
        </TabsTab>
        <TabsIndicator />
      </TabsList>
      <TabsPanel
        value="documents"
        className="mt-1 rounded-lg px-2 py-3 system-sm-regular text-text-secondary"
      >
        24 documents, 3 still indexing.
      </TabsPanel>
      <TabsPanel
        value="retrieval"
        className="mt-1 rounded-lg px-2 py-3 system-sm-regular text-text-secondary"
      >
        Run a query against the index and inspect the chunks it returns.
      </TabsPanel>
    </Tabs>
  )
}
```

### Activate on focus

By default the arrow keys only move focus, and `Enter` or `Space` activates the focused tab. Set `activateOnFocus` on `TabsList` when panels are cheap to render, so moving focus also switches the panel.

```tsx title="activate-on-focus.tsx"
import { Tabs, TabsIndicator, TabsList, TabsPanel, TabsTab } from '@langgenius/dify-ui/tabs'

const languages = [
  {
    value: 'python',
    label: 'Python',
    code: "client.create_chat_message(inputs={}, query='Hello', user='abc-123')",
  },
  {
    value: 'node',
    label: 'Node.js',
    code: "await client.createChatMessage({}, 'Hello', 'abc-123')",
  },
  {
    value: 'curl',
    label: 'cURL',
    code: "curl -X POST 'https://api.dify.ai/v1/chat-messages' -H 'Authorization: Bearer {api_key}'",
  },
]

export function TabsActivateOnFocusDemo() {
  return (
    <Tabs defaultValue="python" className="w-full max-w-96">
      <TabsList
        aria-label="Request example language"
        className="border-b border-divider-subtle"
        activateOnFocus
      >
        {languages.map((language) => (
          <TabsTab key={language.value} value={language.value}>
            {language.label}
          </TabsTab>
        ))}
        <TabsIndicator />
      </TabsList>
      {languages.map((language) => (
        <TabsPanel key={language.value} value={language.value} className="mt-1 rounded-lg p-2">
          <pre className="min-h-14 rounded-lg bg-background-section px-3 py-2.5 code-xs-regular wrap-break-word whitespace-pre-wrap text-text-secondary">
            <code>{language.code}</code>
          </pre>
        </TabsPanel>
      ))}
    </Tabs>
  )
}
```

### Controlled value

Control the active tab with `value` and `onValueChange` when something outside the tab list changes it, such as a button elsewhere on the page.

```tsx title="controlled.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Tabs, TabsIndicator, TabsList, TabsPanel, TabsTab } from '@langgenius/dify-ui/tabs'
import * as React from 'react'

type Version = 'draft' | 'published'

export function TabsControlledDemo() {
  const [version, setVersion] = React.useState<Version>('draft')

  return (
    <div className="flex w-full max-w-96 flex-col items-start gap-3">
      <Tabs value={version} onValueChange={setVersion} className="w-full">
        <TabsList aria-label="Workflow versions" className="border-b border-divider-subtle">
          <TabsTab value="draft">Draft</TabsTab>
          <TabsTab value="published">Published</TabsTab>
          <TabsIndicator />
        </TabsList>
        <TabsPanel
          value="draft"
          className="mt-1 rounded-lg px-2 py-3 system-sm-regular text-text-secondary"
        >
          The draft has 2 unpublished changes.
        </TabsPanel>
        <TabsPanel
          value="published"
          className="mt-1 rounded-lg px-2 py-3 system-sm-regular text-text-secondary"
        >
          Version 14 is live for all users of this app.
        </TabsPanel>
      </Tabs>
      <Button
        size="small"
        variant={version === 'draft' ? 'primary' : 'secondary'}
        onClick={() => setVersion(version === 'draft' ? 'published' : 'draft')}
      >
        {version === 'draft' ? 'View published version' : 'Return to draft'}
      </Button>
    </div>
  )
}
```

## Keyboard

| Key                       | Action                                                                                   |
| ------------------------- | ---------------------------------------------------------------------------------------- |
| `ArrowRight`, `ArrowLeft` | Moves focus to the next or previous tab, wrapping at the ends. Disabled tabs take focus. |
| `Home`, `End`             | Moves focus to the first or last tab.                                                    |
| `Enter`, `Space`          | Activates the focused tab and shows its panel.                                           |
| `Tab`                     | Moves focus from the tab list to the active panel.                                       |

## API reference

### Tabs

Groups a tab list and its corresponding tab panels. Renders a `<div>` element.

Inherits `className`, `render`, `style`, `value`, `defaultValue`, `orientation`, `onValueChange` from [Base UI Tabs.Root](https://base-ui.com/react/components/tabs#root).

**Data attributes**

| Attribute                   | Description                                                                   |
| --------------------------- | ----------------------------------------------------------------------------- |
| `data-activation-direction` | Indicates the direction of the activation (based on the previous active tab). |
| `data-orientation`          | Indicates the orientation of the tabs.                                        |

### TabsIndicator

Draws an active underline inside `TabsList` that slides between tabs. Renders a `<span>` element.

Inherits `renderBeforeHydration`, `className`, `render`, `style` from [Base UI Tabs.Indicator](https://base-ui.com/react/components/tabs#indicator).

**Data attributes**

| Attribute                   | Description                                                                   |
| --------------------------- | ----------------------------------------------------------------------------- |
| `data-activation-direction` | Indicates the direction of the activation (based on the previous active tab). |
| `data-orientation`          | Indicates the orientation of the tabs.                                        |

**CSS variables**

| Variable              | Type     | Description                                                                                 |
| --------------------- | -------- | ------------------------------------------------------------------------------------------- |
| `--active-tab-left`   | `number` | Indicates the distance on the left side from the parent's container if the tab is active.   |
| `--active-tab-right`  | `number` | Indicates the distance on the right side from the parent's container if the tab is active.  |
| `--active-tab-top`    | `number` | Indicates the distance on the top side from the parent's container if the tab is active.    |
| `--active-tab-bottom` | `number` | Indicates the distance on the bottom side from the parent's container if the tab is active. |
| `--active-tab-width`  | `number` | Indicates the width of the tab if it is active.                                             |
| `--active-tab-height` | `number` | Indicates the height of the tab if it is active.                                            |

### TabsList

Groups the individual tab buttons. Renders a `<div>` element.

Inherits `activateOnFocus`, `loopFocus`, `className`, `render`, `style` from [Base UI Tabs.List](https://base-ui.com/react/components/tabs#list).

**Data attributes**

| Attribute                   | Description                                                                   |
| --------------------------- | ----------------------------------------------------------------------------- |
| `data-activation-direction` | Indicates the direction of the activation (based on the previous active tab). |
| `data-orientation`          | Indicates the orientation of the tabs.                                        |

### TabsPanel

A panel displayed when the corresponding tab is active. Renders a `<div>` element.

Inherits `value`, `keepMounted`, `className`, `render`, `style` from [Base UI Tabs.Panel](https://base-ui.com/react/components/tabs#panel).

**Data attributes**

| Attribute                   | Description                                                                   |
| --------------------------- | ----------------------------------------------------------------------------- |
| `data-index`                | Indicates the index of the tab panel.                                         |
| `data-activation-direction` | Indicates the direction of the activation (based on the previous active tab). |
| `data-orientation`          | Indicates the orientation of the tabs.                                        |
| `data-hidden`               | Present when the panel is hidden.                                             |
| `data-starting-style`       | Present when the panel begins animating in.                                   |
| `data-ending-style`         | Present when the panel is animating out.                                      |

### TabsTab

An individual interactive tab button that toggles the corresponding panel. Renders a `<button>` element.

Inherits `className`, `render`, `value`, `disabled`, `style`, `nativeButton` from [Base UI Tabs.Tab](https://base-ui.com/react/components/tabs#tab).

**Data attributes**

| Attribute                   | Description                                                                   |
| --------------------------- | ----------------------------------------------------------------------------- |
| `data-activation-direction` | Indicates the direction of the activation (based on the previous active tab). |
| `data-orientation`          | Indicates the orientation of the tabs.                                        |
| `data-disabled`             | Present when the tab is disabled.                                             |
| `data-active`               | Present when the tab is active.                                               |
