Tabs
Tabs make it easy to explore and switch between different views.
Introduction
Joy UI provides four tabs-related components:
Tabs
: A context provider that synchronizes the selectedTab
with the correspondingTabPanel
.TabList
: A container that consists ofTab
items.Tab
: A button to toggle a selected tab.TabPanel
: A pane that displays on the screen when its value matches with the selected tab.
<Tabs>
<TabList variant="outlined" color="neutral">
<Tab>...</Tab>
</TabList>
</Tabs>
Playground
Component
After installation, you can start building with this component using the following basic elements:
<span class="token keyword">import</span> Tabs <span class="token keyword">from</span> <span class="token string">'@mui/joy/Tabs'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> TabList <span class="token keyword">from</span> <span class="token string">'@mui/joy/TabList'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> Tab <span class="token keyword">from</span> <span class="token string">'@mui/joy/Tab'</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">MyApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">(</span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Tabs</span></span> <span class="token attr-name">defaultValue</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">TabList</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Tab</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>Tab <span class="token constant">A</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Tab</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Tab</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>Tab <span class="token constant">B</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Tab</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Tab</span></span> <span class="token attr-name">value</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">}</span></span><span class="token punctuation">></span></span>Tab <span class="token constant">C</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Tab</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">TabList</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Tabs</span></span><span class="token punctuation">></span></span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
Basic usage
The tabs structure follows WAI ARIA design pattern.
To target the initially selected tab, specify the value
prop to the TabPanel
and use Tabs
's defaultValue
.
Variants
Both TabList
and Tab
accept global variant values, so you can mix and match to get the desired result.
Vertical
To set the tabs orientation to vertical, use the orientation="vertical"
on the Tabs
component.
Keyboard navigation (e.g. arrow keys) will adapt automatically to the used orientation.
Icon
Since TabList
uses the same style as the List
component, you can use the icon directly as a child or use ListItemDecorator
with a text.
Accessibility
For ensuring proper accessibility, it's recommended by ARIA Authoring Practices Guide to associate a label to the Tabs component. To do that, there are two options:
Option one
Render a text element with an id
and provide aria-labelledby="$is"
to the Tabs component.
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Typography</span></span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>tabs-accessibility-label<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Meaningful label<span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Typography</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Tabs</span></span> <span class="token attr-name">aria-labelledby</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>tabs-accessibility-label<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token operator">...</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Tabs</span></span><span class="token punctuation">></span></span>
Option two
When not displaying a text element on the screen, use aria-label
directly on the Tabs component.
Screen readers will then properly announce the label.
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Tabs</span></span> <span class="token attr-name">aria-label</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Meaningful label<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token operator">...</span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span><span class="token class-name">Tabs</span></span><span class="token punctuation">></span></span>
CSS Variables
Play around with all the CSS variables available in the slider component to see how the design changes.
You can use those to customize the component on both the sx
prop and the theme.
<Tabs >
CSS Variables
px
Get started with the industry-standard React UI library, MIT-licensed.
$0 - Free forever