Tabs
Tabs is used to group and display a set of related elements.
Import
Usage
Basic tabs
Here is a basic example of tabs.
EDITABLE EXAMPLE
TabList and TabPanels are used to group the tabs and tab panels. If you don't need the tabs to be grouped, you can use Tab and TabPanel directly.
Controlled and uncontrolled tabs
The index prop is used to control which tab is selected. The onChange callback returns the selected tab index whenever the user changes the tab. If you intend to control the tabs programmatically, use the onChange callback to update the index prop.
EDITABLE EXAMPLE
For uncontrolled tabs, you can use the defaultIndex prop to set the initial selected tab.
EDITABLE EXAMPLE
Orientation and variants
Tabs can either be oriented horizontally or vertically, and can also be styled with different variants.
- Use the
orientationprop to set the orientation of the tabs. It acceptshorizontalandverticalas values. - Use the
variantprop to set the variant of the tabs. It acceptsdefault,filled, andunstyledas values.
EDITABLE EXAMPLE
Decorate horizontal tabs with a bottom border
To decorate horizontal tabs with a bottom border, you can pass __after prop to TabList to render a bottom border.
EDITABLE EXAMPLE
Access the state of tabs
Sometimes you need to access the state of tabs. For example, you may want to lazily load the content of a tab when that tab is selected.
Tabs, Tab, and TabPanel components expose internal state using Function as Children (FaCC). This is not a recommended approach, but it is useful for this use case.
EDITABLE EXAMPLE
Tabs with dropdown menus
The tab with the dropdown menu is usually the last tab. You can use event.preventDefault() to control whether the tab will be selected when the user clicks the dropdown menu.
EDITABLE EXAMPLE
Draggable tabs with dropdown menus
Use react-beautiful-dnd to make tabs draggable.
EDITABLE EXAMPLE
Scrollable tabs
Scrollable tabs are useful when the tab list is longer than the tab container. You can use the following code to create a scrollable tabs.
This example teaches you how to create a scrollable tabs that supports drag-to-scroll behavior. Scrolling tabs is currently not part of this component library.
EDITABLE EXAMPLE
Customization
Tabs also come with an unstyled variant. It's useful for doing customizations.
EDITABLE EXAMPLE
Props
Tab
| Name | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | ({ getTabProps, disabled, index, isSelected }) => ReactNode | A function child can be used intead of a React element. This function is called with the following props: • The getTabProps function returns the props for the tab.• The disabled prop indicates whether the tab is disabled.• The index prop is the index of the tab.• The isSelected prop is a boolean value indicating whether the tab is selected. | |
| disabled | boolean | Whether the tab is disabled. | |
| index | number | string | The index of the tab. An index number starting from 0 will be provided if not specified. | |
| onClick | (event) => void | A callback for when the tab is clicked. |
Tabs
| Name | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | (context) => ReactNode | A function child can be used intead of a React element. This function is called with the context object. | |
| defaultIndex | number | string | 0 | The default index of the tab to be selected in uncontrolled mode. |
| disabled | boolean | Whether the tabs should be disabled. | |
| index | number | string | The index of the tab to be selected in controlled mode. | |
| onChange | (index: number|string) => void | A callback function that is called when the index changes. | |
| orientation | string | 'horizontal' | The orientation of the tabs. One of: 'horizontal', 'vertical' |
| variant | string | 'default' | The variant of the tabs. One of: 'default', 'filled', 'unstyled' |
TabList
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | A label for the tab list. | |
| children | ReactNode |
TabPanel
| Name | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | ({ getTabPanelProps, index, isSelected }) => ReactNode | A function child can be used intead of a React element. This function is called with the following props: • The getTabPanelProps function returns the props for the tab panel.• The index prop is the index of the tab panel.• The isSelected prop is a boolean value indicating whether the corresponding tab is selected. | |
| index | number | string | The index of the tab panel. An index number starting from 0 will be provided if not specified. |
TabPanels
| Name | Type | Default | Description |
|---|---|---|---|
| children | ReactNode |
Further Reading
ARIA: tab role
This example combines the role tab with tablist and elements with tabpanel to create an interactive group of tabbed content. Here we are enclosing our group of content in a div, with our tablist having an aria-label which labels it for assistive technology. Each tab is a button with the attributes previously mentioned. The first tab has both tabindex="0" and aria-selected="true" applied. These two attributes must always be coordinated as such—so when another tab is selected, it will then have tabindex="0" and aria-selected="true" applied. All unselected tabs must have aria-selected="false" and tabindex="-1".
All of the tabpanel elements have tabindex="0" to make them tabbable, and all but the currently active one have the hidden attribute. The hidden attribute will be removed when a tabpanel becomes visible with JavaScript. There is some basic styling applied that restyles the buttons and changes the z-index of tab elements to give the illusion of it connecting to the tabpanel for active elements, and the illusion that inactive elements are behind the active tabpanel.
WAI-ARIA Roles, States, and Properties
- The element that serves as the container for the set of tabs has role
tablist. - Each element that serves as a tab has role
taband is contained within the element with roletablist. - Each element that contains the content panel for a
tabhas roletabpanel. - If the tab list has a visible label, the element with role
tablisthasaria-labelledbyset to a value that refers to the labelling element. Otherwise, thetablistelement has a label provided byaria-label. - Each element with role
tabhas the propertyaria-controlsreferring to its associatedtabpanelelement. - The active
tabelement has the statearia-selectedset totrueand all othertabelements have it set tofalse. - Each element with role
tabpanelhas the propertyaria-labelledbyreferring to its associatedtabelement. - If a
tabelement has a popup menu, it has the propertyaria-haspopupset to eithermenuortrue. - If the
tablistelement is vertically oriented, it has the propertyaria-orientationset tovertical. The default value ofaria-orientationfor atablistelement ishorizontal.