# Tab

> One object's tab in a pane's TabBar — its icon and title, its ×, and a menu that opens as a browser tab's does; pinned, title only, new things, waiting to sign in, a task's reference and a web page's title.

Source: https://raft-design.botiverse.build/components/tab. Generated from the current release; where it differs from what you remember, this page is right.

A Tab is one object open in a pane, in the pane's [TabBar](https://raft-design.botiverse.build/components/tab-bar.md): it says what the object is, its icon and its title, and holds none of the object's actions. The front tab's × always shows; a background tab's shows on hover. As in a browser, the tab's menu has no button: it opens on a right-click, a touch hold lifted, or Shift+F10 or the context-menu key. It is built on `TabsTab`, so the bar is one tablist with roving focus and the arrow keys ([Tabs and panes](https://raft-design.botiverse.build/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>
  );
}
```

## When to use

| Situation                                                                                      | Use                                                                                                                  |
| ---------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| An object open in a pane beside a conversation: a document, a task, a file, an App, a web page | Tab, in a TabBar                                                                                                     |
| The sections of one page, each with its own content                                            | [Tabs](https://raft-design.botiverse.build/components/tabs.md)                                                                                              |
| Another view of the same content                                                               | [SegmentedControl](https://raft-design.botiverse.build/components/segmented-control.md)                                                        |
| What was opened from a list worked through one item at a time                                  | Not a tab: the pane beside it ([Tabs and panes](https://raft-design.botiverse.build/patterns/tabs-and-panes.md#opening-an-object)) |

## Anatomy

- The tab: a `TabsTab` named by the tab's whole name (`tabAccessibleName`), holding:
  - its icon (16px), given by `icon`;
  - its title: `prefix` and `title` for an object; for a `site`, its `pageTitle`, or while it has none its domain drawn by [SiteDomain](https://raft-design.botiverse.build/components/site-domain.md), after Not secure on http;
  - what is new, a key while it waits to sign in.
- Its ×, laid over the tab's end, out of the Tab order.
- The tab's menu: `menu`, the items of a [ContextMenu](https://raft-design.botiverse.build/components/context-menu.md).

## States

| State              | Prop                       | What shows                                                                                                                                                 |
| ------------------ | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| In front           | TabBar `value`             | The selected sticker; the × always shown. The same width as in the back.                                                                                   |
| In the back        |                            | The × on hover.                                                                                                                                            |
| Pinned             | `pinned`                   | First in the bar, its icon alone, centred, no ×. Read as "…, pinned". The workspace tabs don't use it for now.                                             |
| New things         | `unread`, `unreadDot`      | An unread count ([Badge](https://raft-design.botiverse.build/components/badge.md), `accent`) for what can be counted; an attention dot ([Status](https://raft-design.botiverse.build/components/status.md), `accent`) for what can't. |
| Waiting to sign in | `waitingSignIn`            | A key after the title until the tab is shown: a page in the back never signs in on its own.                                                                |
| Title only         | TabBar `density="compact"` | In a pane under 280px every tab, in front or not, keeps its title alone, no icon; its × is laid over the title's end.                                      |
| Dragged            | `dragging`, `shift`        | Its place stays at 40% (`data-drag-origin`); tabs after a drop point slide by `TAB_DROP_GAP`.                                                              |
| Squeezed           | `squeezed`                 | Another pane's tab while the bar's pane is maximized, after the bar's divider, dimmed: no ×, never in front; its menu still opens.                         |
| Merged             | TabBar `merged`            | A tab of the column merged into the bar in a narrow window, after its divider: a whole tab, as the bar's own are (`data-merged`).                          |
| Opened again       | `located`                  | The located highlight, once ([States](https://raft-design.botiverse.build/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>
  );
}
```

## Behavior

- `source` says where the title and icon come from: `app`, the App's registered name and icon; `site`, a web page; `object`, the app's own object. A web page's tab is named by its page title (`pageTitle`), as in a browser, on every tab, front or not; only while the page has no title does the tab show its domain (`title`). The full URL never shows in a tab: it is in the address row.
- The title and icon come from the object live, so a renamed object or a task's new status shows at once.
- A task's tab is its reference and its title. In a narrow tab the place gives way first (`#launch-week:`, `dm:@Martin:`), and the number stays with the start of the title; a place squeezed to a stub is dropped.

```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>
  );
}
```

- A domain, shown while the page has no title, is drawn by SiteDomain and cut at its start, never its end; a page title is cut at its end, as any title is. On http, Not secure goes before whichever the tab shows; a title-only tab (a compact bar) leaves it to the address row.

```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>
  );
}
```

- The menu has no button, on any tab, as a browser tab's menu has none. It opens on a right-click (at the pointer), Shift+F10 or the context-menu key (under the tab, its first item highlighted), and a 500ms touch hold lifted without moving (under the tab). A hold that moves is a drag; the menu never opens while the finger is down, unlike other items with a menu ([States](https://raft-design.botiverse.build/foundations/states.md#dragging-on-a-touch-screen)). When it closes, focus goes back to the tab, unless `menuHandsFocus` says the chosen item moved it on.
- `onClose` gets the tab's `value`: the ×, Delete or Backspace on the focused tab, and a middle click. A pinned or squeezed tab doesn't close; a merged one does, as any tab of the bar.
- Choosing a tab never changes a width, its own or another's, as in a browser: a tab is laid out the same in front or not. It shows the same things, keeps the ×'s room whether its × shows or not (a background tab's × shows on hover, in that room), and the front tab keeps the others' weight; only its colours change.
- The front tab always stays in the bar: the width fit never hides it, and never gives it a width of its own.
- `pinned` stays for a later decision: the workspace tabs have no Pin for now, so the shell never passes it ([Tabs and panes](https://raft-design.botiverse.build/patterns/tabs-and-panes.md#one-kind-of-tab)).
- TabBar sets each tab's width, which tab is in front (its `value`) and how much the tabs show (its `density`); a Tab never sizes itself beyond its caps (200px, 120px title only).

## Content

- The title is the object's own: a document's or a task's title, an App's registered name, a web page's title. Pass a site's host as `URL.host` gives it as `title`: it shows only until the page has a title.
- Write a task's reference as the app writes it: `#launch-week:#25`, `dm:@Martin:#3`.
- Pass translations as `messages`; the defaults are English ([UI writing](https://raft-design.botiverse.build/content/writing.md)).

## Accessibility

- Each tab is a `role="tab"` in the bar's tablist ([Tabs](https://raft-design.botiverse.build/components/tabs.md#accessibility)); its accessible name and tooltip are what it shows with its states: "#launch-week:#25 Ship the tab bar, 2 new", "Example Docs, pinned" (a page's title; its domain while it has none), "Board, waiting to sign in".
- ArrowLeft and ArrowRight move between tabs, Enter or Space brings one to the front, Delete or Backspace closes it, Shift+F10 or the context-menu key opens its menu. The × isn't in the Tab order: the keyboard reaches it through the tab (Delete).
- Closing the focused tab removes it: the app moves focus to the tab that comes to the front.
- The × is named Close and the tab's name. The menu is named after the tab.
- The count, the dot and the key are hidden from assistive technology: the tab's name says them.
- The bar has no tab panels of its own. To link a tab to what it shows, give the Tab an `id` and `aria-controls`, and the pane's `PaneBody` `role="tabpanel"`, that `id`'s target and `aria-labelledby` the tab's `id` ([Pane](https://raft-design.botiverse.build/components/pane.md#accessibility)). A Tab puts `id`, `aria-controls` and `aria-describedby` on its `role="tab"` element; its other props go on its wrapper.

## Rules

- Put nothing of the object's on its tab but its icon and title: its actions go in its row's ⋯, a web page's and an App's too. The tab's menu holds only the tab's and its pane's.
- Give no tab a button for its menu: it opens as a browser tab's does.
- Never open the menu on a touch hold while the finger is down: it opens on lift, so a hold that moves can be a drag.
- Name a web page's tab by its page title, its domain only until it has one; never the URL. Which site it is, the address row says, with 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.
