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.When to use
- 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.
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.
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 /> },
]}
/>;Navigation mode
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,tabwitharia-selected, and atabpanellabeled by the active tab.Tabenters the set as one stop;←/→move and select (wrapping);Home/Endjump to the ends. - Do not override
roleortabIndexthroughbuttonProps— it breaks keyboard navigation. - Nav mode is a
navlandmark of links in the normal tab order. The current item hasaria-current="page"; every item is at least 44px tall. Name eachnavwith a distinctaria-label.
Properties
aria-label *stringnonecolorstringnonecontainmentstringnonelegibility"on-photo"nonelineClampnumbernonelinkAsElementType<any, keyof IntrinsicElements>'a'mode"nav""content"'content'onChange((index: number) => void)((index: number, event: MouseEvent<HTMLElement, MouseEvent>) => void)nonepageShellbooleannonereadablenumberbooleannoneselectedIndexnumber0snapbooleanfalsetabContentPropsDefaultFlexInterfacenonetabHeaderPropsDefaultFlexInterfacenonetabs *TabType[]TabsNavItem[]nonetransformstringnone