Skip to content

Tabs ​

Several views of one thing in a narrow pane, each tab with a count: "Links 12", "Attachments 2". A compound component on Base UI's tabs. To pick a mode such as list or grid, use Segmented.

tsx
import { Tabs } from '@adecore/ui';
tsx
import { Tabs } from '@adecore/ui';
import { useState } from 'react';

const VIEWS = [
    { id: 'preview', label: 'Preview', count: 0 },
    { id: 'text', label: 'Plain text', count: 0 },
    { id: 'source', label: 'HTML source', count: 0 },
    { id: 'headers', label: 'Headers', count: 24 },
    { id: 'links', label: 'Links', count: 12 },
    { id: 'attachments', label: 'Attachments', count: 2 },
    { id: 'checks', label: 'Checks', count: 3 },
    { id: 'raw', label: 'Raw', count: 0 }
];

export default function TabsDemo() {
    const [view, setView] = useState('links');

    return (
        <Tabs.Root value={view} onValueChange={setView} className="w-full max-w-3xl rounded-lg border border-border bg-surface">
            <Tabs.List aria-label="Message views" className="px-4">
                {VIEWS.map((entry) => (
                    <Tabs.Tab key={entry.id} value={entry.id}>
                        {entry.label}
                        <Tabs.Count value={entry.count} />
                    </Tabs.Tab>
                ))}
            </Tabs.List>
            {VIEWS.map((entry) => (
                <Tabs.Panel key={entry.id} value={entry.id} className="p-4 text-xs text-text-muted">
                    {entry.count === 0 ? `${entry.label} of the message.` : `${entry.count} ${entry.label.toLowerCase()} in the message.`}
                </Tabs.Panel>
            ))}
        </Tabs.Root>
    );
}
tsx
<Tabs.Root value={view} onValueChange={setView}>
    <Tabs.List aria-label="Message views" className="px-4">
        <Tabs.Tab value="links">
            Links
            <Tabs.Count value={links.length} />
        </Tabs.Tab>
    </Tabs.List>
    <Tabs.Panel value="links">…</Tabs.Panel>
</Tabs.Root>

Parts ​

PartWhat it is
Tabs.RootHolds the picked tab: value and onValueChange(value), or defaultValue (0 when left out).
Tabs.ListThe strip, with a border along its bottom and a menu for the tabs that do not fit. Name it with aria-label. start holds what comes before the tabs, end actions after them, after actions right beside the last tab.
Tabs.TabOne tab, with its value. disabled keeps it out of reach. onClose makes it closable, pinned keeps it in the strip without a close button.
Tabs.CountThe number after a label, as a Pill, formatted for the region. Hidden at zero.
Tabs.PanelThe view of one tab, shown while that tab is picked.

The strip ​

The picked tab is underlined over the strip's border. The strip has no side padding, so its border spans the pane; inset the tabs with className on Tabs.List, such as px-4.

Tabs that do not fit ​

The strip never scrolls or wraps. Tabs that do not fit go into a "More" menu at its end, with their icons and counts. The picked tab always stays in the strip, replacing the last visible tab if needed. Drag the demo's corner to resize it.

tsx
import { Tabs } from '@adecore/ui';
import { useState } from 'react';

const VIEWS = [
    { id: 'preview', label: 'Preview', count: 0 },
    { id: 'text', label: 'Plain text', count: 0 },
    { id: 'source', label: 'HTML source', count: 0 },
    { id: 'headers', label: 'Headers', count: 24 },
    { id: 'links', label: 'Links', count: 12 },
    { id: 'attachments', label: 'Attachments', count: 2 },
    { id: 'checks', label: 'Checks', count: 3 },
    { id: 'raw', label: 'Raw', count: 0 }
];

/* A pane a person can drag narrower and wider by its corner, to watch tabs move into the menu and back. */
export default function TabsOverflowDemo() {
    const [view, setView] = useState('preview');

    return (
        <div className="w-80 max-w-full min-w-40 resize-x overflow-hidden rounded-lg border border-border bg-surface">
            <Tabs.Root value={view} onValueChange={setView}>
                <Tabs.List aria-label="Message views" className="px-3">
                    {VIEWS.map((entry) => (
                        <Tabs.Tab key={entry.id} value={entry.id}>
                            {entry.label}
                            <Tabs.Count value={entry.count} />
                        </Tabs.Tab>
                    ))}
                </Tabs.List>
                <p className="p-3 text-xs text-text-muted">Drag the corner of this pane to make it wider or narrower.</p>
            </Tabs.Root>
        </div>
    );
}

The strip reads tabs from their props, so they must be direct children of Tabs.List, or an array of them.

Closable tabs ​

onClose on Tabs.Tab draws a close button and also fires on a middle click and on Delete or Backspace. It only asks. Remove the tab from your list yourself, and pick the next one if it was picked. The close button is out of the tab order. Tabs in the "More" menu have no close button.

overflow="scroll" on Tabs.List scrolls an overflowing row sideways instead of using "More". Hidden edges fade and the picked tab scrolls into view. The row carries the class tabs-scroll.

variant="pill" on Tabs.List draws each tab as a key in a bar, filled when picked. Use it for tabs in a toolbar, where an underline would read as the pane's tabs. TabsVariant is its type.

end on Tabs.List holds actions at the end of the strip and start before the tabs; both keep their width and the tabs fit in the rest. after sits right beside the last tab, for a plus that adds one.

tsx
import { IconButton, Tabs } from '@adecore/ui';
import { Plus } from 'lucide-react';
import { useState } from 'react';

interface Query {
    readonly id: number;
    readonly name: string;
}

export default function TabsClosableDemo() {
    const [queries, setQueries] = useState<readonly Query[]>([
        { id: 1, name: 'Orders' },
        { id: 2, name: 'Customers' },
        { id: 3, name: 'Revenue' }
    ]);
    const [picked, setPicked] = useState<number>(1);
    const [next, setNext] = useState(4);

    const add = (): void => {
        setQueries([...queries, { id: next, name: `Query ${next}` }]);
        setPicked(next);
        setNext(next + 1);
    };

    const close = (id: number): void => {
        const at = queries.findIndex((query) => query.id === id);
        const rest = queries.filter((query) => query.id !== id);

        setQueries(rest);

        if (picked === id) {
            setPicked(rest[Math.min(at, rest.length - 1)]?.id ?? 0);
        }
    };

    return (
        <Tabs.Root value={picked} onValueChange={(value) => setPicked(Number(value))} className="w-full max-w-md rounded-lg border border-border bg-surface">
            <Tabs.List aria-label="Queries" className="px-3" end={<IconButton icon={Plus} size="sm" label="New query" onClick={add} />}>
                {queries.map((query) => (
                    <Tabs.Tab key={query.id} value={query.id} onClose={() => close(query.id)}>
                        {query.name}
                    </Tabs.Tab>
                ))}
            </Tabs.List>
            {queries.map((query) => (
                <Tabs.Panel key={query.id} value={query.id} className="p-4 text-xs text-text-muted">
                    The editor of {query.name}.
                </Tabs.Panel>
            ))}
        </Tabs.Root>
    );
}

Pinned tabs ​

pinned on Tabs.Tab hides the close button and ignores onClose. Like the picked tab, a pinned tab never moves into "More". The element carries data-pinned. Put pinned tabs at the start of your list.

tsx
import { Icon, Tabs } from '@adecore/ui';
import { Database, FileCode, House } from 'lucide-react';
import { useState } from 'react';

interface Page {
    readonly id: string;
    readonly name: string;
    readonly pinned: boolean;
}

export default function TabsPinnedDemo() {
    const [pages, setPages] = useState<readonly Page[]>([
        { id: 'home', name: 'Home', pinned: true },
        { id: 'schema', name: 'Schema', pinned: true },
        { id: 'orders', name: 'orders.sql', pinned: false },
        { id: 'customers', name: 'customers.sql', pinned: false }
    ]);
    const [picked, setPicked] = useState('orders');

    const close = (id: string): void => {
        const rest = pages.filter((page) => page.id !== id);

        setPages(rest);

        if (picked === id) {
            setPicked(rest.at(-1)?.id ?? 'home');
        }
    };

    return (
        <Tabs.Root value={picked} onValueChange={(value) => setPicked(String(value))} className="w-full max-w-md">
            <Tabs.List aria-label="Pages" variant="pill">
                {pages.map((page) => (
                    <Tabs.Tab key={page.id} value={page.id} pinned={page.pinned} onClose={() => close(page.id)}>
                        <Icon icon={page.id === 'home' ? House : page.id === 'schema' ? Database : FileCode} size={14} /> {page.name}
                    </Tabs.Tab>
                ))}
            </Tabs.List>
        </Tabs.Root>
    );
}

Dragging tabs ​

useTabDrag(options) lets a person reorder a strip of tabs by dragging them, and drag one out into a window of its own. Spread tabProps(key) on each Tabs.Tab, put strip on the element around the tabs, and draw an element with the class ade-tab-insertion at insertion pixels from the strip's left while it is not null. The strip stays put while a tab is held; the line says where the tab lands, and the release moves it once. A press on a button inside a tab, such as its close button, stays that button's.

TabDragOptions take keys() and landingOf(key, index), which says where among the other tabs a drop before the indexth of them lands, or null where it stays, so pinned tabs keep their own run. onReorder(key, landing) moves it. Leave out the rest for a strip that only reorders:

  • onTearOut(key, center, width): the tab leaves the strip while held, pulled 24 pixels above or below it or out of the window. For a shell that follows a held tab into a new window.
  • onTearOff(key, at): a release outside the window, for a shell that cannot follow while it is held; at is where the new window would stand on screen.
  • dragsWindow(key) and onWindowDrag(key, grab): dragging this tab moves its window instead, as for the only tab in a window.
  • onRelease(key, window): the pointer let go after a tear-out or a window drag.

tabDropIndex(strip, key, x) counts the other tabs whose middle is left of x, for a drop that comes from another window. TabDrag is the return type.

tsx
import { Tabs, useTabDrag } from '@adecore/ui';

function ProjectTabs({ projects, active, onSelect, onMove, onOpenWindow }: ProjectTabsProps) {
    const { strip, insertion, tabProps } = useTabDrag({
        keys: () => projects.map((project) => project.key),
        landingOf: (key, index) => index,
        onReorder: onMove,
        onTearOff: (key, at) => onOpenWindow(key, at)
    });

    return (
        <Tabs.Root ref={strip} value={active} onValueChange={(value) => onSelect(String(value))} className="relative flex min-w-0 grow">
            <Tabs.List variant="pill" overflow="scroll">
                {projects.map((project) => (
                    <Tabs.Tab key={project.key} value={project.key} {...tabProps(project.key)}>
                        {project.name}
                    </Tabs.Tab>
                ))}
            </Tabs.List>
            {insertion !== null && <span aria-hidden className="ade-tab-insertion" style={{ left: insertion }} />}
        </Tabs.Root>
    );
}

Panels ​

Tabs.Panel holds one tab's view and links it to the tab for screen readers. Only the picked panel is mounted, unless it has keepMounted. A screen that draws its own view below the strip can leave the panels out.

Icons ​

A tab takes an Icon before its label, at 14 pixels.

tsx
import { Icon, Tabs } from '@adecore/ui';
import { Eye, Link, Paperclip, ShieldCheck } from 'lucide-react';
import { useState } from 'react';

export default function TabsIconsDemo() {
    const [view, setView] = useState('preview');

    return (
        <Tabs.Root value={view} onValueChange={setView} className="w-full max-w-md">
            <Tabs.List aria-label="Message views">
                <Tabs.Tab value="preview">
                    <Icon icon={Eye} size={14} /> Preview
                </Tabs.Tab>
                <Tabs.Tab value="links">
                    <Icon icon={Link} size={14} /> Links
                    <Tabs.Count value={12} />
                </Tabs.Tab>
                <Tabs.Tab value="attachments">
                    <Icon icon={Paperclip} size={14} /> Attachments
                    <Tabs.Count value={2} />
                </Tabs.Tab>
                <Tabs.Tab value="checks" disabled>
                    <Icon icon={ShieldCheck} size={14} /> Checks
                </Tabs.Tab>
            </Tabs.List>
        </Tabs.Root>
    );
}

Keyboard ​

The arrow keys move between tabs and wrap. Home and End go to the first and last. Enter or Space picks the focused tab, or focus alone with activateOnFocus on Tabs.List. Disabled tabs are skipped.

Types ​

TabsRootProps, TabsListProps, TabsTabProps, TabsCountProps and TabsValue are exported types.

Licenses and third-party notices are listed in each package.