Unstyled Tabs
Tabs are UI elements for organizing and navigating between groups of related content.
Introduction
Tabs are implemented using a collection of related components:
<TabUnstyled />
- the tab element itself. Clicking on a tab displays its corresponding panel.<TabsListUnstyled />
- the container that houses the tabs. Responsible for handling focus and keyboard navigation between tabs.<TabPanelUnstyled />
- the card that hosts the content associated with a tab.<TabsUnstyled />
- the top-level component that wraps the Tabs List and Tab Panel components so that tabs and their panels can communicate with one another.
Components
Usage
After installation, you can start building with this component collection using the following basic elements:
import TabUnstyled from '@mui/base/TabUnstyled';
import TabsListUnstyled from '@mui/base/TabsListUnstyled';
import TabPanelUnstyled from '@mui/base/TabPanelUnstyled';
import TabsUnstyled from '@mui/base/TabsUnstyled';
export default function MyApp() {
return (
<TabsUnstyled>
<TabsListUnstyled>
<TabUnstyled>{/* tab one */}</TabUnstyled>
<TabUnstyled>{/* tab two */}</TabUnstyled>
</TabsListUnstyled>
<TabPanelUnstyled>{/* panel one */}</TabPanelUnstyled>
<TabPanelUnstyled>{/* panel two */}</TabPanelUnstyled>
</TabsUnstyled>
);
}
Basics
By default, Tab components and their corresponding panels are zero-indexed (i.e. the first tab has a value
of 0
, then 1
, 2
, etc.).
Clicking on a given Tab opens the panel with the same value
, which corresponds to the order in which each component is nested within its container.
Though not required, you can add the value
prop to the Tab and Tab Panel to take control over how these components are associated with one another.
The following demo omits the value
prop from the Tab components, and also defines 0
as the defaultValue
for the Tabs component, which sets the first Tab and Panel as the defaults:
The next demo shows how to apply custom styles to a set of tabs:
Anatomy
The Tab components are each composed of a root slot with no interior slots:
<div class="TabsUnstyled-root">
<div class="TabsListUnstyled-root">
<button class="TabUnstyled-root">First tab</button>
<button class="TabUnstyled-root">Second tab</button>
<button class="TabUnstyled-root">Third tab</button>
</div>
<div class="TabPanelUnstyled-root">First panel</div>
<div class="TabPanelUnstyled-root">Second panel</div>
<div class="TabPanelUnstyled-root">Third panel</div>
</div>
Slot props
Use the component
prop to override the root slot with a custom element:
<TabUnstyled component="span" />
If you provide a non-interactive element such as a <span>
, the Tab components will automatically add the necessary accessibility attributes.
Customization
Third-party routing library
A common use case for tabs is to implement client-side navigation that doesn't require an HTTP round-trip to the server.
The Tab component provides the component
prop to handle this use case—see the Material UI documentation on routing for more details.
Accessibility
(WAI-ARIA: https://www.w3.org/WAI/ARIA/apg/patterns/tabpanel/)
The following steps are necessary to make the Tab component suite accessible to assistive technology:
- Label
<TabsUnstyled />
witharia-label
oraria-labelledby
. - Connect each
<TabUnstyled />
to its corresponding<TabPanelUnstyled />
by setting the correctid
,aria-controls
andaria-labelledby
.
The demos below illustrate the proper use of ARIA labels.
Keyboard navigation
By default, when using keyboard navigation, the Tab components open via manual activation—that is, the next panel is displayed only after the user activates the tab with either Space, Enter, or a mouse click.
This is the preferable behavior for tabs in most cases, according to the WAI-ARIA authoring practices.
Alternatively, you can set the panels to be displayed automatically when their corresponding tabs are in focus—this behavior of the selection following the focus is known as automatic activation.
To enable automatic activation, pass the selectionFollowsFocus
prop to the <TabsUnstyled />
component:
/* Tabs where selection follows focus */
<TabsUnstyled selectionFollowsFocus />
The following demo pair illustrates the difference between manual and automatic activation. Move the focus to a tab in either demo and navigate with the arrow keys to observe the difference:
Selection following focus:
Selection independent of focus (default behavior):