Tonic UI
Getting StartedColor ModeColor StyleVersionsContributing
THEME
BordersBreakpointsColorsOutlinesPositionsSpacingSizesShadowsTypography
BUILDING BLOCKS
BoxControlBox
UTILITIES

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 orientation prop to set the orientation of the tabs. It accepts horizontal and vertical as values.
  • Use the variant prop to set the variant of the tabs. It accepts default, filled, and unstyled as 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

NameTypeDefaultDescription
childrenReactNode | ({ getTabProps, disabled, index, isSelected }) => ReactNodeA 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.
disabledbooleanWhether the tab is disabled.
indexnumber | stringThe index of the tab. An index number starting from 0 will be provided if not specified.
onClick(event) => voidA callback for when the tab is clicked.

Tabs

NameTypeDefaultDescription
childrenReactNode | (context) => ReactNodeA function child can be used intead of a React element. This function is called with the context object.
defaultIndexnumber | string0The default index of the tab to be selected in uncontrolled mode.
disabledbooleanWhether the tabs should be disabled.
indexnumber | stringThe index of the tab to be selected in controlled mode.
onChange(index: number|string) => voidA callback function that is called when the index changes.
orientationstring'horizontal'The orientation of the tabs. One of: 'horizontal', 'vertical'
variantstring'default'The variant of the tabs. One of: 'default', 'filled', 'unstyled'

TabList

NameTypeDefaultDescription
aria-labelstringA label for the tab list.
childrenReactNode

TabPanel

NameTypeDefaultDescription
childrenReactNode | ({ getTabPanelProps, index, isSelected }) => ReactNodeA 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.
indexnumber | stringThe index of the tab panel. An index number starting from 0 will be provided if not specified.

TabPanels

NameTypeDefaultDescription
childrenReactNode

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 tab and is contained within the element with role tablist.
  • Each element that contains the content panel for a tab has role tabpanel.
  • If the tab list has a visible label, the element with role tablist has aria-labelledby set to a value that refers to the labelling element. Otherwise, the tablist element has a label provided by aria-label.
  • Each element with role tab has the property aria-controls referring to its associated tabpanel element.
  • The active tab element has the state aria-selected set to true and all other tab elements have it set to false.
  • Each element with role tabpanel has the property aria-labelledby referring to its associated tab element.
  • If a tab element has a popup menu, it has the property aria-haspopup set to either menu or true.
  • If the tablist element is vertically oriented, it has the property aria-orientation set to vertical. The default value of aria-orientation for a tablist element is horizontal.