Skip to the content

Tabs

Tabs is a row of tabs in two modes. Content mode (the default) switches panels in place and is controlled: you keep selectedIndex in state and update it from onChange. Nav mode (mode="nav") renders the same row as a nav of links to a section's pages.
Navigation

When to use

Use when
  • You switch between related views of one resource, such as profile, activity and settings.
  • Splitting a detail screen vertically would create excessive scroll.
  • mode="nav": you link the sibling pages of one section, under the top bar.
Avoid when
  • It is the app's top-level navigation — use NavBar on a phone.
  • The user must compare panels side by side — use Grid columns.
  • A content tab would change the URL — that is navigation; use mode="nav".

Usage

Each tab has a tabTitle (any ReactNode) and its content. onChange fires only when a different tab is chosen; an optional per-tab handleClick fires just before it, which suits event tracking. The selected tab draws a 2px indicator bar in its own color (primary by default); set a tab's color to highlight it.

Primary indicator (default).
import { Tabs } from '@apollion-dsi/core/data-display/tabs';

const [index, setIndex] = React.useState(0);

<Tabs
  selectedIndex={index}
  onChange={setIndex}
  tabs={[
    { tabTitle: 'First', content: <FirstPage /> },
    { tabTitle: 'Second', color: 'danger', content: <SecondPage /> },
  ]}
/>;

mode="nav" takes a required aria-label and items with href or to. Pass the router's link component once as linkAs, or per item as as; an item with only onClick renders a button. Derive current from the route: the current item gets the indicator and is scrolled into view. When current does not come from the route, move it in onChange(index, event), which fires for a non-current item. The row scrolls sideways with no visible scrollbar; snap settles the scroll on an item's start edge. Click an item below (the demo keeps current in state).

import { Link as RouterLink, useLocation } from 'react-router-dom';

const { pathname } = useLocation();

<Tabs
  mode="nav"
  aria-label="Settings sections"
  linkAs={RouterLink}
  snap
  tabs={sections.map(({ title, path }) => ({ tabTitle: title, to: path, current: pathname === path }))}
/>;

Accessibility

  • Content mode follows the ARIA Tabs pattern: tablist, tab with aria-selected, and a tabpanel labeled by the active tab. Tab enters the set as one stop; ← / → move and select (wrapping); Home / End jump to the ends.
  • Do not override role or tabIndex through buttonProps — it breaks keyboard navigation.
  • Nav mode is a nav landmark of links in the normal tab order. The current item has aria-current="page"; every item is at least 44px tall. Name each nav with a distinct aria-label.

Properties

See also