Skip to Content
NarakUI

Tab Strip

A scrolling row of pill tabs for open items, like the artifacts beside a chat, that people can switch between, close and add to.

Closable tabs

A tab is built from four parts. TabStripItem is the slot in the strip. TabStripTab is the pill. TabStripTrigger is the button that selects the tab, and TabStripClose is a sibling of the trigger so the two buttons never nest. Pass the selected tab's id to activeTabId on TabStrip, and isActive to each part of that tab. The selected background smoothly slides to the newly selected tab.

The close button shows on hover and focus, and always on the selected tab. Clicking it calls onClose without selecting the tab. Give square controls beside the strip, like the new tab button, TAB_STRIP_CONTROL_CLASS so they match the pills.

Without close buttons

Leave out TabStripClose when the set of tabs is fixed. Use aria-controls on TabStripTrigger to point at the panel it shows.

Overlay close button

With overlay, the close button floats over the end of the label instead of reserving space, and shows only on hover and focus, including on the selected tab. Tabs keep the same width whether or not they can close.

Overflow and keyboard

The strip scrolls sideways when its tabs do not fit, and fades its edges. When activeTabId changes, the selected tab scrolls into view. TabStripLabel truncates long titles and shows the full title in a native tooltip.

The strip is one Tab stop: Tab moves focus to the selected tab, the left and right arrow keys move between tabs and wrap at the ends, and Home and End jump to the first and last tab. Enter or Space selects the focused tab.

Installation

pnpm dlx shadcn@latest add @narakui/tab-strip

Add "@narakui": "https://ui.narakcode.dev/r/{name}.json" to components.json, or install directly from https://ui.narakcode.dev/r/tab-strip.json.

Usage

import {
  TabStrip,
  TabStripClose,
  TabStripItem,
  TabStripLabel,
  TabStripTab,
  TabStripTrigger,
} from '@/components/ui/tab-strip';

<TabStrip activeTabId={activeId} aria-label="Artifacts">
  {tabs.map((tab) => (
    <TabStripItem key={tab.id} tabId={tab.id} isActive={tab.id === activeId}>
      <TabStripTab isActive={tab.id === activeId}>
        <TabStripTrigger
          isActive={tab.id === activeId}
          onClick={() => select(tab.id)}
        >
          <TabStripLabel>{tab.name}</TabStripLabel>
        </TabStripTrigger>
        <TabStripClose
          label={`Close ${tab.name}`}
          onClose={() => close(tab.id)}
        />
      </TabStripTab>
    </TabStripItem>
  ))}
</TabStrip>;

TabStrip Properties

PropTypeDefaultDescription
aria-labelstring—Accessible name of the tab list. Pass this or aria-labelledby
activeTabIdstring | nullnullId of the selected tab. It scrolls into view when this changes, and becomes the strip's Tab stop again
classNamestring—Additional classes for the tab list. Other div props are passed through

TabStripItem Properties

PropTypeDefaultDescription
tabIdstringRequiredId of the tab, matched against activeTabId
isActivebooleanfalseWhether this is the selected tab
classNamestring—Additional classes for the slot. Other div props are passed through

TabStripTab Properties

PropTypeDefaultDescription
isActivebooleanfalseApplies the selected pill style
refRef<HTMLDivElement>—Forwarded to the pill, so it can be a context menu or drag target
classNamestring—Additional classes for the pill, such as a smaller max width. Other div props are passed through

TabStripTrigger Properties

PropTypeDefaultDescription
isActivebooleanfalseSets aria-selected
asChildbooleanfalseRenders the child element, such as a link, as the trigger instead of a button
classNamestring—Additional classes for the trigger. Other button props are passed through

TabStripLabel Properties

PropTypeDefaultDescription
childrenstringRequiredTab title. Truncates to one line, with the full title in a native tooltip
classNamestring—Additional classes for the label

TabStripClose Properties

PropTypeDefaultDescription
labelstringRequiredAccessible name and tooltip, such as Close Visibility report
onClose() => voidRequiredCalled on click, without selecting the tab
overlaybooleanfalseFloats the button over the end of the label on hover and focus instead of reserving space