Document tabs
Tabs for open documents that close, reorder, overflow into a menu, and add a new one.
Demo
Usage
import { DocumentTabs, type DocumentTab } from '@tuttn/slant'
// The app owns the tabs: DocumentTabs asks to close, reorder and add, and you
// update items to carry it out.
const [files, setFiles] = useState<DocumentTab[]>([
{ id: 'readme', label: 'README.md', description: 'docs/README.md' },
{ id: 'app', label: 'App.tsx', dirty: true },
{ id: 'home', label: 'Home', closable: false },
])
const [active, setActive] = useState<string | null>('readme')
// Controlled. Closing the selected tab calls onValueChange with its neighbour
// first, then onClose. The children are the selected document, in one tab panel.
<DocumentTabs label="Open files" items={files} value={active} onValueChange={setActive}
onClose={id => setFiles(files => files.filter(file => file.id !== id))}
onAdd={() => { const file = newFile(); setFiles(files => [...files, file]); setActive(file.id) }}
onReorder={(from, to) => setFiles(files => move(files, from, to))}>
<Editor file={active} />
</DocumentTabs>
// Uncontrolled: DocumentTabs keeps the selection, starting from defaultValue
// (the first enabled tab without it). onValueChange still reports it.
<DocumentTabs items={files} defaultValue="app" onValueChange={open}
onClose={id => setFiles(files => files.filter(file => file.id !== id))} />
// The open-tabs menu: while the tabs don't fit (the default), always, or never.
<DocumentTabs items={files} value={active} onValueChange={setActive} overflow="always" size="sm" />Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly DocumentTab[] | — | The open documents, in order. The app owns them: DocumentTabs never adds or removes one itself. |
value | string | null | — | The selected tab's id, when controlled; `null` for none. An id that names no tab selects nothing. |
defaultValue | string | null | the first enabled tab | The tab selected first, when uncontrolled. |
onValueChange | (id: string) => void | — | Called with a tab's id when the user selects it, and with the neighbour's id when the selected tab closes. |
label | string | messages.openTabs | The tablist's accessible name. |
onClose | (id: string) => void | — | Asks the app to close a tab: from its close control, Delete or a middle click. Remove it from `items` to complete the close. Without it no tab shows a close control. |
onAdd | () => void | — | Adds a new-tab button after the tabs. The app adds the tab (and selects it, if it controls `value`). |
addLabel | string | messages.newTab | The new-tab button's accessible name. |
onReorder | (from: number, to: number) => void | — | Makes the tabs reorderable by drag and by Alt+Arrow keys. Called once per step with the moving tab's index and its new one, both in `items` as it stands; reorder `items` to match. |
overflow | 'auto' | 'always' | 'never' | 'auto' | When the open-tabs menu button shows: while the tabs don't fit (`auto`), whenever there are two or more tabs (`always`), or never. |
size | 'sm' | 'md' | 'md' | The bar's height: the tabs sit on the `sm` or `md` control height. |
children | ReactNode | — | The selected document's content, rendered in one tab panel under the bar and labelled by the selected tab. |
items[] | DocumentTab | — | `{ id, label, textValue?, icon?, description?, closable?, dirty?, disabled? }`. Give `textValue` when the label is not plain text: it names the tab in announcements and the menu's typeahead. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root `div.sl-document-tabs`, which holds the bar and the panel. |
Keyboard
| Tab | moves into the tabs, to the selected one (else the first enabled), and on to the open-tabs and new-tab buttons: the tabs are one tab stop |
| ← / → | move focus to the previous or next tab without selecting it; they wrap at the ends, skip disabled tabs and swap under right-to-left text |
| Home / End | move focus to the first or last tab |
| Enter / Space | select the focused tab |
| Delete | closes the focused tab, when it is closable and onClose is set; focus moves to the tab that takes its place. On a Mac keyboard, Fn+Delete |
| Alt+← / Alt+→ | move the focused tab one place, with onReorder; focus stays on it and the new place is announced |
| Alt+Home / Alt+End | move the focused tab to the first or last place |
| Escape | during a drag, puts the tab back where the drag began |
| Enter / Space / ↓ | on the open-tabs button, open the menu on the selected tab; ↑ opens it on the last. The menu then follows Menu's keys, and choosing a tab selects and focuses it |
Accessibility
- The bar is a
role="tablist"named bylabel("Open tabs" by default), each tab arole="tab"button witharia-selected. With children, onerole="tabpanel"follows the bar, labelled by the selected tab, which points at it witharia-controls; without children there is no panel. - Manual activation: the arrows move focus only, so a screen reader user hears each tab before opening it, and a slow document never opens on the way past.
- A tab's children are presentational, so the close control can't sit inside it. It sits beside the tab button in a
role="presentation"wrapper,aria-hiddenand out of the tab order (tabIndex={-1}, its mousedown held back so a press never moves focus): a pointer shortcut. Keyboard and screen reader users close with Delete, which closable tabs advertise witharia-keyshortcuts="Delete". - A dirty tab's name reads its label, then "Unsaved changes" (
messages.unsavedChanges). The dot is decorative. - Closing is announced politely once the app removes the tab ("App.tsx closed",
messages.tabClosed), and so is each move ("App.tsx, moved to 2 of 5"). When focus was on the closed tab, it moves to the tab that took its place; if you had already moved on, it stays where you put it. - The open-tabs button (
messages.openTabs) and the new-tab button (addLabel, elsemessages.newTab) are named icon buttons. The menu lists each tab as amenuitemradio, the selected one checked. - A tab's
descriptionis its tooltip, and its accessible description. A label cut short with no description shows its full text there instead.
The app owns the tabs
DocumentTabs never adds, removes or reorders a tab itself. onClose, onAdd and onReorder are requests: update items to carry them out, or don't (ask about unsaved changes first, say). When the selected tab closes, onValueChange hears the neighbour (the next tab, else the previous) before onClose hears the closed one, so the next document is chosen in the same event. onReorder(from, to) comes once per step, from each Alt+Arrow press and each neighbour a drag passes, with both indexes in items as it stands.
Pointer
A click selects. The close control shows on the selected tab and under the pointer; a dirty tab shows its dot there until hovered. A middle click closes a tab. Dragging a tab moves it live as the pointer passes each neighbour, and Escape during the drag puts it back; a drag that moved a tab doesn't select it, so reordering never opens a document.
Overflow
The tabs scroll sideways inside the bar, with no scrollbar, and the selected one scrolls into view whenever it changes. The open-tabs menu reaches every tab: with overflow="auto" its button shows while the tabs don't fit, with "always" whenever there are two or more, and never with "never". Labels truncate at about 180px; set --sl-document-tab-max on the root to change that.
Shortcuts
⌘W and ⌘T (Ctrl+W, Ctrl+T) belong to the browser, which closes and opens its own tabs before a page hears them. DocumentTabs binds neither: give your app's own close and new-tab commands keys the browser leaves alone, in a CommandBar or with KeyHints, and call your onClose and onAdd from them. In a desktop shell such as Electron or Tauri, the app can take them back.
Tabs or DocumentTabs
For a fixed set of sections, use Tabs: automatic activation, an indicator, a panel per tab. DocumentTabs is for a changing set of documents the user opens, closes and arranges, with one panel for the selected one.