# Tab

> 一个对象在一格的 TabBar 里的标签页：它的图标和标题、它的 ×，以及像浏览器标签页那样打开的菜单；固定、只剩标题、新东西、等待登录、任务的写法和网页的标题。

Source: https://raft-design.botiverse.build/zh/components/tab. Translated from https://raft-design.botiverse.build/components/tab.md; where the two differ, the English page is right.

Tab 是开在一格里的一个对象，放在这一格的 [TabBar](https://raft-design.botiverse.build/zh/components/tab-bar.md) 里：它说明对象是什么，即图标和标题，不放对象的任何动作。前面那个标签页的 × 一直显示；后面的标签页的 × 悬停时才出现。和浏览器一样，标签页菜单没有按钮：右键、触屏按住再抬手、Shift+F10 或菜单键打开它。它建在 `TabsTab` 上，所以整个标签栏是一个 tablist，带漫游焦点和方向键（[标签页与分栏](https://raft-design.botiverse.build/zh/patterns/tabs-and-panes.md)）。

```tsx
import { FileText, Globe, LayoutDashboard } from "lucide-react";
import { TaskStatusIcon } from "@botiverse/raft-ui";
import { Tab, TabBar } from "@botiverse/raft-ui/wip";
import { TabMenuItems } from "../tab-menu.tsx";
import { useExampleTabs } from "../use-example-tabs.ts";

const TABS = ["docs", "notes", "task", "brief", "board"];

/**
 * A pane's tabs: a pinned site, the front document with its ×, a task with
 * two new replies, a document with a new version, and an App waiting to sign
 * in. As in a browser a tab's menu has no button: right-click a tab, or press
 * Shift+F10 on it, for its menu.
 */
export function TabPreviewContent() {
  const tabs = useExampleTabs(TABS, "notes");
  return (
    <div className="w-full max-w-3xl overflow-hidden rounded-container border border-line-hairline">
      <TabBar label="Tabs" value={tabs.front} onValueChange={tabs.setFront}>
        <Tab
          value="docs"
          source="site"
          icon={<Globe />}
          title="docs.example.com"
          pageTitle="Example Docs"
          pinned
          menu={<TabMenuItems value="docs" pinned onCloseOthers={tabs.closeOthers} />}
        />
        {tabs.isOpen("notes") ? (
          <Tab
            value="notes"
            source="object"
            icon={<FileText />}
            title="Launch notes"
            onClose={tabs.close}
            menu={
              <TabMenuItems value="notes" onClose={tabs.close} onCloseOthers={tabs.closeOthers} />
            }
          />
        ) : null}
        {tabs.isOpen("task") ? (
          <Tab
            value="task"
            source="object"
            icon={<TaskStatusIcon status="in-progress" />}
            prefix="#launch-week:#25"
            title="Ship the tab bar"
            unread={tabs.front === "task" ? undefined : 2}
            onClose={tabs.close}
            menu={
              <TabMenuItems value="task" onClose={tabs.close} onCloseOthers={tabs.closeOthers} />
            }
          />
        ) : null}
        {tabs.isOpen("brief") ? (
          <Tab
            value="brief"
            source="object"
            icon={<FileText />}
            title="Press brief"
            unreadDot={tabs.front !== "brief"}
            onClose={tabs.close}
            menu={
              <TabMenuItems value="brief" onClose={tabs.close} onCloseOthers={tabs.closeOthers} />
            }
          />
        ) : null}
        {tabs.isOpen("board") ? (
          <Tab
            value="board"
            source="app"
            icon={<LayoutDashboard />}
            title="Board"
            waitingSignIn={tabs.front !== "board"}
            onClose={tabs.close}
            menu={
              <TabMenuItems value="board" onClose={tabs.close} onCloseOthers={tabs.closeOthers} />
            }
          />
        ) : null}
      </TabBar>
    </div>
  );
}
```

## 何时使用
| 场景 | 使用 |
| --- | --- |
| 开在对话旁边一格里的对象：文档、任务、文件、App、网页 | Tab，放在 TabBar 里 |
| 同一页面的各个部分，每个部分有自己的内容 | [Tabs](https://raft-design.botiverse.build/zh/components/tabs.md) |
| 同一内容的另一种视图 | [SegmentedControl](https://raft-design.botiverse.build/zh/components/segmented-control.md) |
| 从要一条条处理的列表里打开的东西 | 不是标签页：开在旁边那一格（[标签页与分栏](https://raft-design.botiverse.build/zh/patterns/tabs-and-panes.md#opening-an-object)） |

## 结构
- 标签页：一个 `TabsTab`，名字是标签页的完整名字（`tabAccessibleName`），里面有：
  - 图标（16px），由 `icon` 给出；
  - 标题：对象是 `prefix` 和 `title`；`site` 是它的 `pageTitle`，还没有标题时是 [SiteDomain](https://raft-design.botiverse.build/components/site-domain.md) 画的域名，http 时前面有「不安全」；
  - 新东西，以及等待登录时的钥匙。
- 它的 ×，叠在标签页末端，不在 Tab 顺序里。
- 标签页菜单：`menu`，[ContextMenu](https://raft-design.botiverse.build/zh/components/context-menu.md) 的菜单项。

## 状态
| 状态 | 属性 | 显示什么 |
| --- | --- | --- |
| 在前面 | TabBar 的 `value` | 选中的贴纸；× 一直显示。宽度和在后面时一样。 |
| 在后面 | | 悬停时显示 ×。 |
| 固定 | `pinned` | 排在最前、只剩图标、图标居中、没有 ×。读作「…，已固定」。工作区的标签页眼下不用它。 |
| 新东西 | `unread`、`unreadDot` | 数得清的用未读数（[Badge](https://raft-design.botiverse.build/zh/components/badge.md)，`accent`）；数不清的用提醒点（[Status](https://raft-design.botiverse.build/zh/components/status.md)，`accent`）。 |
| 等待登录 | `waitingSignIn` | 标题后面一把钥匙，直到标签页显示出来：后台的页面从不自己完成登录。 |
| 只剩标题 | TabBar 的 `density="compact"` | 不到 280px 的一格里，每个标签页不管在不在前面都只剩标题，没有图标；× 叠在标题末端。 |
| 拖动中 | `dragging`、`shift` | 原位留着、透明度 40%（`data-drag-origin`）；放下点之后的标签页让开 `TAB_DROP_GAP`。 |
| 被挤掉的 | `squeezed` | 标签栏那一格最大化时别的格的标签页，跟在分隔线后面，变淡：没有 ×，从不到前面；菜单照样能打开。 |
| 并入的 | TabBar `merged` | 窄窗口里并进标签栏的那一列的标签页，跟在分隔线后面：完整的标签页，和标签栏自己的一样（`data-merged`）。 |
| 再次打开 | `located` | 定位高亮，一次（[状态](https://raft-design.botiverse.build/zh/foundations/states.md#located)）。 |

```tsx
import { FileText, Globe } from "lucide-react";
import { Tab, TabBar } from "@botiverse/raft-ui/wip";
import { TabMenuItems } from "../tab-menu.tsx";
import { useExampleTabs } from "../use-example-tabs.ts";

const TABS = ["notes", "layout", "vite"];

/**
 * In a pane under 280px every tab keeps only its title, the front one too, so
 * choosing a tab changes no width. Its × is laid over the title's end: the
 * front tab's always, another's on hover.
 */
export function TabTitleOnlyContent() {
  const tabs = useExampleTabs(TABS, "notes");
  return (
    <div className="w-[250px] max-w-full overflow-hidden rounded-container border border-line-hairline">
      <TabBar label="Tabs" value={tabs.front} onValueChange={tabs.setFront} density="compact">
        {tabs.isOpen("notes") ? (
          <Tab
            value="notes"
            source="object"
            icon={<FileText />}
            title="Launch notes"
            onClose={tabs.close}
            menu={
              <TabMenuItems value="notes" onClose={tabs.close} onCloseOthers={tabs.closeOthers} />
            }
          />
        ) : null}
        {tabs.isOpen("layout") ? (
          <Tab
            value="layout"
            source="object"
            icon={<FileText />}
            title="layout-notes.md"
            onClose={tabs.close}
            menu={
              <TabMenuItems value="layout" onClose={tabs.close} onCloseOthers={tabs.closeOthers} />
            }
          />
        ) : null}
        {tabs.isOpen("vite") ? (
          <Tab
            value="vite"
            source="site"
            icon={<Globe />}
            title="vite.dev"
            pageTitle="Getting started"
            onClose={tabs.close}
            menu={
              <TabMenuItems value="vite" onClose={tabs.close} onCloseOthers={tabs.closeOthers} />
            }
          />
        ) : null}
      </TabBar>
    </div>
  );
}
```

```tsx
import { FileText, Globe, LayoutDashboard } from "lucide-react";
import { TAB_DROP_GAP, Tab, TabBar } from "@botiverse/raft-ui/wip";
import { useExampleTabs } from "../use-example-tabs.ts";

const TABS = ["notes", "vite", "board"];

/**
 * Board is being dragged to the second place in its bar: it stays where it
 * was at 40% as the drag's origin, the tabs from the insertion point on
 * slide apart, and a line marks where it will land.
 */
export function TabDraggingContent() {
  const tabs = useExampleTabs(TABS, "notes");
  return (
    <div className="w-full max-w-2xl overflow-hidden rounded-container border border-line-hairline">
      <TabBar label="Tabs" value={tabs.front} onValueChange={tabs.setFront} insertAt={1}>
        <Tab value="notes" source="object" icon={<FileText />} title="Launch notes" />
        <Tab
          value="vite"
          source="site"
          icon={<Globe />}
          title="vite.dev"
          pageTitle="Getting started"
          shift={TAB_DROP_GAP}
        />
        <Tab
          value="board"
          source="app"
          icon={<LayoutDashboard />}
          title="Board"
          dragging
          shift={TAB_DROP_GAP}
        />
      </TabBar>
    </div>
  );
}
```

## 行为
- `source` 说明标题和图标从哪里来：`app` 是 App 注册的名字和图标；`site` 是一个网页；`object` 是 App 自己的对象。和浏览器一样，网页的标签页用页面标题（`pageTitle`）命名，前面后面的标签页都一样；只有页面还没有标题时，标签页才显示它的域名（`title`）。完整网址从不出现在标签页上：它在地址行里。
- 标题和图标从对象实时取，所以对象改名或任务换状态会立刻显示出来。
- 任务的标签页是它的写法加标题。标签页变窄时位置先让（`#launch-week:`、`dm:@Martin:`），编号和标题的开头一直在；被挤成残段的位置直接去掉。

```tsx
import { TaskStatusIcon } from "@botiverse/raft-ui";
import { Tab, TabBar } from "@botiverse/raft-ui/wip";
import { TabMenuItems } from "../tab-menu.tsx";
import { useExampleTabs } from "../use-example-tabs.ts";

const DM_TASK = ["dm-3"];
const CHANNEL_TASK = ["task-9"];

/**
 * A task's tab names it by its reference and its title. In a narrow pane the
 * place gives way first, and the number stays with the start of the title.
 */
export function TabTaskReferenceContent() {
  const wide = useExampleTabs(DM_TASK, "dm-3");
  const narrow = useExampleTabs(CHANNEL_TASK, "task-9");
  return (
    <div className="flex w-full flex-col gap-3">
      <div className="w-[420px] max-w-full overflow-hidden rounded-container border border-line-hairline">
        <TabBar label="Tabs" value={wide.front} onValueChange={wide.setFront}>
          <Tab
            value="dm-3"
            source="object"
            icon={<TaskStatusIcon status="todo" />}
            prefix="dm:@Martin:#3"
            title="Send me the checklist for the fixed layer"
            onClose={wide.close}
            menu={
              <TabMenuItems value="dm-3" onClose={wide.close} onCloseOthers={wide.closeOthers} />
            }
          />
        </TabBar>
      </div>
      <div className="w-[250px] max-w-full overflow-hidden rounded-container border border-line-hairline">
        <TabBar label="Tabs" value={narrow.front} onValueChange={narrow.setFront}>
          <Tab
            value="task-9"
            source="object"
            icon={<TaskStatusIcon status="in-progress" />}
            prefix="#launch-week:#9"
            title="The way into a tab's menu"
            onClose={narrow.close}
            menu={
              <TabMenuItems
                value="task-9"
                onClose={narrow.close}
                onCloseOthers={narrow.closeOthers}
              />
            }
          />
        </TabBar>
      </div>
    </div>
  );
}
```

- 页面还没有标题时显示的域名由 SiteDomain 画，从开头截，从不截结尾；页面标题和其他标题一样从结尾截。http 时，「不安全」放在标签页显示的内容前面；只剩标题的标签页（compact 的标签栏）把它留给地址行。

```tsx
import { Globe } from "lucide-react";
import { Tab, TabBar } from "@botiverse/raft-ui/wip";
import { TabMenuItems } from "../tab-menu.tsx";
import { useExampleTabs } from "../use-example-tabs.ts";

const PREVIEW = ["preview"];
const ONCALL = ["oncall", "vite"];

/**
 * A web page's tab is named by its page title, as in a browser. While the
 * page has no title yet its domain stands in, cut at its start when it
 * doesn't fit. An http site says Not secure before whatever the tab shows.
 */
export function TabSiteContent() {
  const preview = useExampleTabs(PREVIEW, "preview");
  const oncall = useExampleTabs(ONCALL, "oncall");
  return (
    <div className="flex w-full flex-col gap-3">
      <div className="w-[340px] max-w-full overflow-hidden rounded-container border border-line-hairline">
        <TabBar label="Tabs" value={preview.front} onValueChange={preview.setFront}>
          <Tab
            value="preview"
            source="site"
            icon={<Globe />}
            title="pr-1842.preview.staging.deploy.example-preview.dev"
            onClose={preview.close}
            menu={
              <TabMenuItems
                value="preview"
                onClose={preview.close}
                onCloseOthers={preview.closeOthers}
              />
            }
          />
        </TabBar>
      </div>
      <div className="w-[520px] max-w-full overflow-hidden rounded-container border border-line-hairline">
        <TabBar label="Tabs" value={oncall.front} onValueChange={oncall.setFront}>
          {oncall.isOpen("oncall") ? (
            <Tab
              value="oncall"
              source="site"
              icon={<Globe />}
              title="oncall.example.internal"
              pageTitle="Pager · On call"
              insecure
              onClose={oncall.close}
              menu={
                <TabMenuItems
                  value="oncall"
                  onClose={oncall.close}
                  onCloseOthers={oncall.closeOthers}
                />
              }
            />
          ) : null}
          {oncall.isOpen("vite") ? (
            <Tab
              value="vite"
              source="site"
              icon={<Globe />}
              title="vite.dev"
              pageTitle="Getting started"
              onClose={oncall.close}
              menu={
                <TabMenuItems
                  value="vite"
                  onClose={oncall.close}
                  onCloseOthers={oncall.closeOthers}
                />
              }
            />
          ) : null}
        </TabBar>
      </div>
    </div>
  );
}
```

- 和浏览器标签页的菜单一样，任何标签页的菜单都没有按钮。打开方式：右键（开在指针那里）、Shift+F10 或菜单键（开在标签页下面，第一项高亮），以及触屏按住 500ms 不动再抬手（开在标签页下面）。按住后移动是拖动；手指没抬起时菜单从不打开，这一点和其他带菜单的项不同（[状态](https://raft-design.botiverse.build/zh/foundations/states.md#dragging-on-a-touch-screen)）。菜单关上后焦点回到标签页，除非 `menuHandsFocus` 说选中的项把焦点交给了别处。
- `onClose` 收到标签页的 `value`：×、有焦点时按 Delete 或 Backspace，以及中键点击。固定的和被挤掉的标签页不能关；并入的和标签栏里任何标签页一样能关。
- 和浏览器一样，选中一个标签页从不改变任何宽度，它自己的和别的都不变：标签页在不在前面排法都一样。显示的东西一样，不管 × 显不显示都留着 × 的位置（后面的标签页悬停时 × 出现在这个位置），前面那个的字重和其他的一样；变的只有颜色。
- 前面那个标签页总留在标签栏里：宽度规则从不把它收起来，也从不另给它一个宽度。
- `pinned` 留着等以后再定：工作区的标签页眼下没有固定，所以外壳从不传它（[标签页与分栏](https://raft-design.botiverse.build/zh/patterns/tabs-and-panes.md#one-kind-of-tab)）。
- 每个标签页的宽度、哪个在前面（它的 `value`）、显示多少（它的 `density`）都由 TabBar 决定；Tab 自己最宽只到上限（200px，只剩标题时 120px）。

## 内容
- 标题是对象自己的：文档或任务的标题、App 注册的名字、网页的标题。网站的主机名照 `URL.host` 给出的样子作为 `title` 传入：只在页面还没有标题时显示。
- 任务的写法照 App 的写法：`#launch-week:#25`、`dm:@Martin:#3`。
- 译文通过 `messages` 传入；默认是英文（[UI 文案](https://raft-design.botiverse.build/zh/content/writing.md)）。

## 无障碍
- 每个标签页是标签栏 tablist 里的一个 `role="tab"`（[Tabs](https://raft-design.botiverse.build/zh/components/tabs.md#accessibility)）；它的无障碍名称和提示是它显示的内容加上状态：「#launch-week:#25 Ship the tab bar, 2 new」「Example Docs, pinned」（页面的标题；还没有标题时是域名）、「Board, waiting to sign in」。
- ← 和 → 在标签页之间移动，Enter 或空格把它放到前面，Delete 或 Backspace 关掉它，Shift+F10 或菜单键打开它的菜单。× 不在 Tab 顺序里：键盘通过标签页本身用到它（Delete）。
- 关掉有焦点的标签页会移除它：由 App 把焦点移到到了前面的那个标签页。
- × 名叫 Close 加标签页的名字。菜单以标签页命名。
- 未读数、提醒点和钥匙对辅助技术隐藏：标签页的名字已经说出它们。
- 标签栏自己没有 tab panel。要把标签页和它显示的内容连起来，给 Tab 一个 `id` 和 `aria-controls`，给那一格的 `PaneBody` 加 `role="tabpanel"`、作为 `aria-controls` 指向的目标，并用 `aria-labelledby` 指向标签页的 `id`（[Pane](https://raft-design.botiverse.build/zh/components/pane.md#accessibility)）。Tab 把 `id`、`aria-controls` 和 `aria-describedby` 放在它的 `role="tab"` 元素上；其他属性放在外层。

## 规则
- 标签页上只放对象的图标和标题：对象的动作放在它那一行的 ⋯ 里，网页和 App 的也一样。标签页菜单只放标签页和这一格的动作。
- 不给任何标签页放打开菜单的按钮：它像浏览器标签页那样打开。
- 触屏按住时，手指没抬起就不要打开菜单：抬起时才打开，这样按住后移动可以是拖动。
- 网页的标签页用页面标题命名，只在还没有标题时用域名；从不用网址。它是哪个网站，由地址行里的 SiteDomain 说明。

## API

### Tab

One tab of a pane's TabBar, built on `TabsTab` inside the bar's `Tabs` › `TabsList` (roving focus, arrow keys, `role="tab"`). It says what the object is, its icon and title, and holds none of the object's actions. TabBar sets its width, and says whether it is in front (the bar's `value`) and how much it shows (the bar's `density`).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `dragging` | `boolean` |  | The tab a drag came from: it stays in place at 40%. |
| `icon` (required) | `ReactNode` |  | 16px. The App's registered icon, a globe for a site, the object's icon. |
| `insecure` | `boolean` |  | A `site` over http: Not secure before its title or domain; a title-only tab leaves it to the address row. |
| `located` | `string \| number` |  | A new value each time the tab is opened again and shown in place of a second copy: the located highlight, once (states.md#located). |
| `menu` | `ReactNode` |  | The tab's menu: `ContextMenu` items for what is done to the tab and its pane. As in a browser it has no button: it opens on right-click, on Shift+F10 or the context-menu key while the tab has focus, and on a touch screen when a 500ms hold ends without moving (a hold that moves is a drag). |
| `menuHandsFocus` | `() => boolean` |  | Read once as the menu closes: true when the item chosen moved focus on (a dialog); otherwise focus returns to the tab. |
| `messages` | `Partial<TabMessages>` |  |  |
| `onClose` | `(value: string) => void` |  | Closes the tab, given its `value`: the ×, Delete or Backspace on the focused tab, and a middle click. Not on a pinned tab. One handler can serve every tab of a bar. |
| `pageTitle` | `string` |  | A `site`'s page title: the tab's name, as in a browser. Empty while the page has none, and the tab shows the domain. Never the URL. |
| `pinned` | `boolean` |  | Pinned: first in the bar, its icon only, centred, no ×. |
| `prefix` | `string` |  | Before the title, in the regular weight: a task's reference (`#launch-week:#25`). In a narrow tab the place gives way first; the number and the start of the title always stay. |
| `shift` | `number` |  | Px the tab slides right while a drop opens a gap before it (`TAB_DROP_GAP`). |
| `source` (required) | `TabSource` |  | `app`: the App's registered name and icon; `site`: a web page, named by its `pageTitle`, or by its domain (drawn with SiteDomain) while it has none; `object`: the app's own object (a document, a task, a file). |
| `squeezed` | `boolean` |  | Another pane's tab while the bar's pane is maximized (the bar's `squeezed`, after its divider): dimmed, no ×, given only the room the bar's own tabs leave (then listed in All tabs), never the front tab; choosing it is the app's, to restore with. Its `menu` still opens. A tab merged into the bar in a narrow window is not squeezed: it is a whole tab, in the bar's `merged`. |
| `title` (required) | `string` |  | The App's name, the site's host (`URL.host`), or the object's title. A `site` shows it only while it has no `pageTitle`. |
| `unread` | `number` |  | New things that can be counted (new replies, new comments): a count after the title. |
| `unreadDot` | `boolean` |  | A new thing that can't be counted (a new version): an attention dot after the title. |
| `value` (required) | `string` |  | The tab's value in its `Tabs` (the tab's id). It is in front when it is its TabBar's `value`. |
| `waitingSignIn` | `boolean` |  | The page asked to sign in while hidden: a key after the title until the tab is shown. |

Also takes the attributes of its element.
