Tabs

React tabs built with the Tabs components for folder, flat, and segmented styles, count badges, scrolling strips, buttons beside the tabs, tabs that open and close, a sticky tab row, manual selection, disabled tabs, and URL routes with React Router and the Next.js App Router.

Examples

Tabs

The website redesign is on track for the June release.
  • Login button overlaps the logo
  • Export fails on large reports
  • Search misses archived items
Your name and photo show on your comments.
Visits
8,912
Sign-ups
241
The Overview page. Use the arrow keys to bring the next tabs into view.

The spring catalog shows 40 products on 24 pages.

Oat milk, rye bread, and two lemons.

Sales grew 12% in the last quarter.

Your team has six members.

Audit log is part of the Business plan.

Route: /inbox

Three new messages from your team.

Tabs with the Next.js App Router

Overview

These examples build tabs with the Ariakit UI Tabs components. Tabs show one panel of related content at a time, in the same place on the page. The arrow keys move between tabs, and the selected tab shows its panel.

The gallery starts with folder tabs, where the selected tab joins its panel. Then it shows tabs that filter one list, underlined tabs, a segmented control, and equal-width tabs around forms. The other examples show a strip that scrolls, buttons beside the tabs, tabs that open and close, a tab row that stays in view while the page scrolls, manual selection for slow panels, and a disabled tab with a reason. The last two examples keep the selected tab in the URL, with React Router and with the Next.js App Router.

Components

Tabs holds the selection and draws the frame around the tabs and panels. TabList is the strip. It takes a tabs prop with a record or an array, or Tab elements as children. TabPanels is the surface around the panels, and each TabPanel names its tab with tabId.

By default, the tabs sit on the edge of that frame. $p adds space between the edge and the tabs, as in the Filtered List example. $outerEdge={false} hides the edge of the frame, so only the selected tab and the panels draw one, and $darken={false} gives the strip the color around the frame, as in the React Router and Next.js examples.

Give each tab an id that is unique on the page, and pass defaultSelectedId or selectedId. Without one of these props, no tab is selected until the tabs mount, so the first paint on the server shows no selected tab.

TabPanel with the single prop follows the selected tab, so one panel can show the content of each tab. Use it when the tabs are views of the same data, as in the Filtered List example. Otherwise, use one panel for each tab. Hidden panels stay mounted, so they keep their state, as in the Equal-Width Tabs example.

The $kind prop of Tab sets the look of the tab: folder, flat, or bevel. The bar prop adds a bar that marks a selected flat or bevel tab. TabList with $kind="segmented" is a control that stands on its own, and it goes in a TabProvider instead of Tabs. Tabs written as children need the same $kind.

A strip that overflows scrolls sideways, as in the Scrolling Tab Strip example. Its tabs fade at each end while more tabs lie that way, and with a fine pointer, “Previous tabs” and “Next tabs” buttons show beside the strip. TabList renders the buttons and the strip in a wrapper, and the strip color stays behind the faded tabs. A border on TabList, such as the divider of the Underlined Tabs example, fades with the tabs. The panel keeps its round corners, because the fades keep each end of the strip clear of them, with or without the buttons. The corner under a selected first tab is square, and it rounds as that tab scrolls out. The corner beside a selected last tab squares while that tab leaves the end, and it rounds again once the tab fades out. $fade={false} turns off the fades, buttons={false} removes the buttons, and previousButton and nextButton take props, such as another label. Without the fades, the panel squares a corner while tabs pass over it. The strip works like a row of the Carousel recipe.

A segmented TabList with padding scrolls in the same way when it doesn’t fit, with the same buttons and props. Its wrapper paints the surface of the list behind the faded tabs and takes className and style.

TabRow puts controls on the row of the tabs, as in the Buttons Beside the Tabs example. It takes the place of the wrapper that TabList renders, so it holds the tab list, the scroll buttons, and the controls, and it paints the strip color behind all of them. The tab list takes the width of its tabs and scrolls when the row is full, so a control after it stays next to the last tab. An element with the grow class takes the free space, so the controls after it sit at the end of the row.

TabRowButton is a button in a TabRow that looks like a tab that is not selected. It has the height, the hover surface, and the corners of the tabs, so give it the same $kind. Its separator prop draws a pipe before it, like the separator prop of Tab. Beside a control, the outer tab is not at the corner of the frame. A selected folder then keeps its full curve, and the panel keeps its round corner.

TabRowFrame is a static part of a TabRow, such as a logo or a title. It has the box of a folder tab that is not selected, but no hover surface and no focus. With the same $p, its TabSlot and TabLabel line up with those of folder tabs and buttons in the row. It also takes a separator prop.

onRemove makes a Tab one that can close, as in the Tabs That Open and Close example. The tab shows a TabRemove mark, and it closes with a press on the mark and with the Delete and Backspace keys. Outside a stretched TabList, a tab with the mark is at least 8em wide, so a press on a short tab doesn’t land on the mark by accident. First, the next tab takes the selection and the focus of the closed tab, or the previous tab when the last tab closes. Then the tab leaves, and onRemove runs, so your function only removes the tab and its panel. Without another tab, nothing takes the selection, so leave out onRemove for the last tab, or move the selection and the focus yourself. The remove prop takes TabRemove props, such as a class that shows the mark only on hover, and false hides the mark. A disabled tab doesn’t close.

A tab that you add to a TabList grows in, and a tab that closes shrinks before onRemove runs. Browsers that can’t animate a width to auto and people who prefer reduced motion get the change at once. A tab that enters selected, such as a new tab that a button opens, comes into view of a strip that scrolls. The strip first scrolls to the room of the grown tab, and the tab then grows there. With reduced motion, the strip scrolls at once. A selected tab that shows whole stays whole in view while its strip shrinks, such as beside a sidebar that opens.

onReorder on TabList lets people move the tabs, as in the Tabs That Open and Close example and the Sticky Tab Row Beside a Sidebar example. A tab follows the pointer of a drag along the strip, the other tabs make room, and the strip scrolls while the pointer rests near an end. A press becomes a drag after 10 pixels, and the tab takes the selection when the drag starts. On a touch screen, a long press picks the tab up, and a swipe still scrolls the strip. The Escape key puts the tab back. The list doesn’t change the order itself: the drop calls onReorder once with the IDs of the tabs in their new order, so give each tab an id and change your state there. A disabled tab doesn’t move, and the other tabs can pass it. The controls of a TabRow beside the list stay in place. While a selected folder tab moves, its curves and the top corners of the panel follow the place where the tab shows. With a Glider in the list, the box of the glider goes with the tab. The separators beside the place that the dragged tab takes hide, as they do beside a selected tab at rest. People who prefer reduced motion get each change of place at once.

$sticky on TabRow keeps the row at the top of the page, as in the Sticky Tab Row Beside a Sidebar example. The page scrolls the panel content, not a scroll area inside the panel. A frame below the row keeps the edge and the round corners of the panel in view, down to the end of the page, and the strip color covers the content that scrolls past the frame. Use it for tabs that fill the page, with $outerEdge={false} and $darken={false}. To keep a space around the row and the frame, set $m on Tabs. Inside a Shell, the margin also makes the corners of the panel concentric with the shell. That space takes presses above the row and below the frame, so a press there doesn’t reach the content that the strip color covers. The frame also covers the edge of the panel, where the focus ring of a focused tab panel would be. Ariakit makes a tab panel focusable only when it has no tab stop inside, so give each tab panel one, such as a field: in that example, each note starts with a title field that renames its tab. The row covers the start of the page, so give the page a start scroll padding a little taller than the row and the space above it, with room for the focus ring and for the border and the padding of a field, as that example does. With $m, also give the page an end scroll padding as tall as that margin. The browser then scrolls a focused control into the frame.

In that example, the button that opens the sidebar and the logo of the app are not in the tab row of the notes, so they stay in place while the sidebar moves the tabs. They are a TabRowButton and a TabRowFrame in a second Tabs with its own TabRow, in a layer above the sidebar and the tabs, because the parts of a tab row take their styles from the tabs and the row around them. $layer="transparent" on that Tabs leaves out the strip color of its row, which would cover the first tab.

While the sidebar is closed, an invisible copy of the button and the logo holds their room before the first tab. Its width comes from the name of the logo, so no script measures it. The copy is hidden from screen readers and can’t take focus. The copy is a part of the row before the TabList, so the tabs scroll after it, and the scroll button and the fade of the strip start after it. A grid column folds the copy as the sidebar opens, and the first tab moves to the edge of the panel, where a selected first tab and the panel share a straight edge. A Glider counts the parts of the row in the place of the selected tab, so the corners of the panel follow the first tab while the copy folds.

$reveal on ShellSidebar keeps the sidebar content in place too, so only the tabs and the panel move, and its zoom value fades the content in and grows it from 95%. Each panel holds a ShellMainBody with $center="outer", which keeps the note on the center of the window while the sidebar opens.

TabSlot puts an icon or a badge next to a label in a TabLabel. Set separator on a Tab to draw a pipe before it, or pass TabSeparator props, such as $kind="chevron", to change the mark. Pass separator={!!index} when you map tabs, so each tab after the first gets one. A pipe hides beside the selected, hovered, or focused tab.

In the React Router example, each Tab renders a Link through the render prop, and Tabs reads the selected tab from useLocation. Only the links navigate, because a setSelectedId that also navigates adds a second history entry for each click. selectOnMove={false} keeps the arrow keys from adding history entries, so Enter or a click follows the focused link. The example uses a memory router, so the route stays in the preview and a link opened in a new tab has no page. An app can use BrowserRouter or HashRouter instead.

The Next.js example works in the same way, with a Next.js Link and usePathname. Each tab is a nested page that renders on the server, and the layout around it stays mounted, so only the panel content changes. Use the address bar above the preview to see the route change when you select a tab.

Accessibility

TabList needs a name, such as aria-label. A tab list can hold only tabs and separators hidden from screen readers. The separator prop hides its TabSeparator for you, so add aria-hidden only to a TabSeparator that you render yourself. A tab also can’t hold another control, so put other controls outside the list. The scroll buttons sit before and after the element with the tablist role, so they are not tabs.

A control beside the tabs is not a tab. TabRow keeps each TabRowButton outside the element with the tablist role, so the Tab key reaches it, and the arrow keys move only between tabs. A button that opens a tab is such a button, after the tab list.

The close mark of a tab is hidden from screen readers, because a tab can’t hold a button. The tab closes from the keyboard instead, with the Delete and Backspace keys, and nothing on the tab tells a screen reader about them, so name the keys in the help of your app. After a close, focus is on the tab that took the place of the closed tab. A touch screen reader has no such keys, so give people another way to close a tab there, such as a menu or a button in the panel.

With onReorder, Ctrl+Shift with the Left or Right Arrow key moves the focused tab one place, and with the Home or End key it moves the tab to an end of the list. Apple devices use the Command key in place of Ctrl. Focus stays on the tab, and a status text tells a screen reader the new place, such as “Workshop moved to position 3 of 5”. That text is in English. Nothing on the tab tells a screen reader about the keys, so name them in the help of your app. A mouse or a finger can move a tab only with a drag, which does not meet WCAG 2.2 success criterion 2.5.7, Dragging Movements, and a touch screen reader has no such keys. Where you need that, give people another way to move a tab, such as a menu with commands that move the tab left and right and change the same state.

By default, moving to a tab with the arrow keys selects it. Set selectOnMove to false when a selection starts slow work, so a person who presses an arrow key three times does not start three requests.

Disabled tabs stay focusable, so arrow keys can reach them and a tooltip can show the reason. Screen readers need the same reason, so point aria-describedby to visible text, as in the Disabled Tab example.