Use tabs to organize and navigate between different types of related content at the same level of hierarchy, in the same viewing area on a page.
Usage
The preview has been updated.
Tab headers will not line-wrap if there is not enough space to display them all. Instead, the user will be able scroll them horizontally. A slight gradient will appear at the right edge to indicate there is hidden content.
Tabs consist of two main parts:
- the "headers" (the clickable "tabs" themselves)
- the "contents" (the content for each tab)
React Usage
Tabsconsists ofTabsHeadersandTabsContents.- Inside of the
TabsHeaders, we have individualTabHeadercomponents, and inside of theTabsContentswe have individualTabContentcomponents. - Each
TabHeadermatches up with a singleTabContentvia thetabIdprop. EveryTabHeaderandTabContentmust have atabIdin order forTabsto be able to tell whichTabContentto show when a certainTabHeaderis clicked. - A
defaultSelectedTabIdcan be provided in order to specify the initially-selected tab. If you want to control the state beyond just the initial selection, check out the managing state section.
The preview has been updated.
Vanilla Usage
In vanilla, Tabs headers are a series of radio buttons and labels.
- The
tabs-headerselement that wraps the headers must haverole="tablist" - Each radio button needs a
valueproperty whose value is the selector of the contents element associated with that radio button. For instance, if the contents panel has an id ofexampleTab1, then the input should havevalue="#exampleTab1" - Each radio button also must have
role="tab", and anaria-controlsproperty whose value is theidof the tab panel that it goes with. - The radio button associated with the default tab should have the property
checked="checked". - You can disable a tab by putting the
disabledproperty onto the radio button associated with that tab. The Tabs contents are a series of divs that are associated with those radio buttons. - Each content panel must have
role="tabpanel"and anaria-labelledbyattributes whose value is theidof the<label>in the tabs headers that the tab panel goes with.
Disabled
disabled is deprecated in SWAN v3.24
Individual tabs should not be disabled, as doing so prevents users from accessing them and leaves them uncertain about the reason for their unavailability. Please remove any uses.
Use disabled on TabHeader to prevent a user from navigating to that section, when a tab's content is completely inaccessible to the user.
The preview has been updated.
Centered Headers
Use this option if you want the headers to be centered instead of left-aligned.
The preview has been updated.
Dividing Line
You can hide the dividing line between the tabs headers and the content of the tabs.
The preview has been updated.
Sticky Headers
This option makes the headers sticky to the top of the screen if the tabs content is tall. This option will also automatically hide the dividing line between the tab headers and the tab content.
The preview has been updated.
With Icons
To add Icons to the headers you can add them directly to the TabHeader.
The preview has been updated.
Wrap Headers
wrapHeaders is deprecated in SWAN v3.3
No direct replacement for better semantic roles on SWAN components. Use the default behaviour.
This option makes the headers wrap to the next line if there isn't enough room.
The preview has been updated.
Managing State in React
Most often with Tabs it is not necessary for you to take ownership of the state (the currently-selected tab). The defaultSelectedTabId prop is available if you'd like to specify which tab should be selected initially without committing to fully managing the Tabs's state.
If you do want to control the state of the Tabs yourself (e.g. other parts of your app care about which tab is currently selected), you can provide the selectedTabId and onRequestTabChange props.
onRequestTabChange is invoked with the requested tabId and it is also passed a second arg which is the event that triggered the change in case you need it.
The preview has been updated.
Accessibility
Tabs requires some aria attributes set for accessibility compliance:
TabsHeadersshould have an aria label attached to the component. This can be done with thearia-labelattribute for a string value or with thearia-labelledbyattribute to give the id of another HTML element acting as the label. Ensure this is localized as it will be read out to the user.